Module: RailsJavaScriptHelpers
- Includes:
- ActionView::Helpers::JavaScriptHelper
- Defined in:
- lib/rails_javascript_helpers/version.rb,
lib/rails_javascript_helpers/rails_javascript_helpers.rb
Defined Under Namespace
Classes: Version
Constant Summary collapse
Instance Method Summary collapse
-
#format_id(id) ⇒ Object
- id
-
DOM ID of element Ensure that the DOM ID starts with a ‘#’ if the provided values is a string starting with a letter (Basically compat mode with prototype style).
-
#format_type_to_js(arg) ⇒ Object
- arg
-
Object Does a simple transition on types from Ruby to Javascript.
Instance Method Details
#format_id(id) ⇒ Object
- id
-
DOM ID of element
Ensure that the DOM ID starts with a ‘#’ if the provided values is a string starting with a letter (Basically compat mode with prototype style)
36 37 38 |
# File 'lib/rails_javascript_helpers/rails_javascript_helpers.rb', line 36 def format_id(id) id.to_s[0,1] =~ /[A-Za-z0-9]/ ? "##{id}" : id end |
#format_type_to_js(arg) ⇒ Object
- arg
-
Object
Does a simple transition on types from Ruby to Javascript.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/rails_javascript_helpers/rails_javascript_helpers.rb', line 7 def format_type_to_js(arg) case arg when Array "[#{arg.map{|value| format_type_to_js(value)}.join(',')}]" when Hash "{#{arg.map{ |key, value| k = key.is_a?(Symbol) ? key.to_s.camelize.sub(/^./, key.to_s[0,1].downcase) : format_type_to_js(key) "#{k}:#{format_type_to_js(value)}" }.join(',')}}" when Numeric arg.to_s when TrueClass arg.to_s when FalseClass arg.to_s when NilClass 'null' when RawJS arg.to_s else arg.to_s.gsub(/\s+/, ' ') =~ %r{^\s*function\s*\(} ? arg.to_s : "'#{escape_javascript(arg.to_s)}'" end end |