Module: RightRails::Helpers::Rails
- Defined in:
- lib/right_rails/helpers/rails.rb
Overview
This module contains the ruby-on-rails native (Prototype) helper replacements
Constant Summary collapse
- CALLBACKS =
ScriptaculousHelper substitute
Set.new([ :create, :uninitialized, :loading, :loaded, :interactive, :complete, :failure, :success ] + (100..599).to_a)
- AJAX_OPTIONS =
Set.new([ :before, :after, :condition, :url, :asynchronous, :method, :insertion, :position, :form, :with, :update, :script, :type ]).merge(CALLBACKS)
Instance Method Summary collapse
- #array_or_string_for_javascript(option) ⇒ Object
-
#button_to_function(name, *args, &block) ⇒ Object
Getting back Rails 3.0 kind of method that supports blocks.
- #draggable_element(element_id, options = {}) ⇒ Object
-
#draggable_element_js(element_id, options) ⇒ Object
replacing the draggables generator to make our autoscripts stuff working.
- #drop_receiving_element(element_id, options = {}) ⇒ Object
-
#drop_receiving_element_js(element_id, options = {}) ⇒ Object
replace the droppables generator to be used with RightJS.
-
#javascript_include_tag(first, *others) ⇒ Object
Overloading the rails method so it loaded RightJS by default.
-
#periodically_call_remote(options = {}) ⇒ Object
Replacing ‘periodically_call_remote` method.
-
#remote_function(options) ⇒ Object
Overloading the ‘remote_function` helper so it used the RightJS semantics.
- #sortable_element(element_id, options = {}) ⇒ Object
-
#sortable_element_js(id, options = {}) ⇒ Object
catching the sortables generator.
-
#submit_to_remote(name, value, options = {}) ⇒ Object
Overloading the ‘submit_to_remote` so it used RightJS instead.
Instance Method Details
#array_or_string_for_javascript(option) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/right_rails/helpers/rails.rb', line 127 def array_or_string_for_javascript(option) if option.kind_of?(Array) "['#{option.join('\',\'')}']" elsif !option.nil? "'#{option}'" end end |
#button_to_function(name, *args, &block) ⇒ Object
Getting back Rails 3.0 kind of method that supports blocks
61 62 63 64 65 66 67 68 |
# File 'lib/right_rails/helpers/rails.rb', line 61 def (name, *args, &block) = args..symbolize_keys function = block_given? ? update_page(&block) : args[0] || '' onclick = "#{"#{[:onclick]}; " if [:onclick]}#{function};" tag(:input, .merge(:type => 'button', :value => name, :onclick => onclick)) end |
#draggable_element(element_id, options = {}) ⇒ Object
135 136 137 |
# File 'lib/right_rails/helpers/rails.rb', line 135 def draggable_element(element_id, = {}) javascript_tag(draggable_element_js(element_id, ).chop!) end |
#draggable_element_js(element_id, options) ⇒ Object
replacing the draggables generator to make our autoscripts stuff working
82 83 84 85 |
# File 'lib/right_rails/helpers/rails.rb', line 82 def draggable_element_js(element_id, ) rightjs_require_module 'dnd' %(new Draggable(#{ActiveSupport::JSON.encode(element_id)}, #{()});) end |
#drop_receiving_element(element_id, options = {}) ⇒ Object
139 140 141 |
# File 'lib/right_rails/helpers/rails.rb', line 139 def drop_receiving_element(element_id, = {}) javascript_tag(drop_receiving_element_js(element_id, ).chop!) end |
#drop_receiving_element_js(element_id, options = {}) ⇒ Object
replace the droppables generator to be used with RightJS
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/right_rails/helpers/rails.rb', line 90 def drop_receiving_element_js(element_id, ={}) rightjs_require_module 'dnd' [:with] ||= "'id=' + encodeURIComponent(draggable.element.id)" [:onDrop] ||= "function(draggable){" + remote_function() + "}" .delete_if { |key, value| AJAX_OPTIONS.include?(key) } [:accept] = array_or_string_for_javascript([:accept]) if [:accept] [:hoverclass] = "'#{[:hoverclass]}'" if [:hoverclass] # Confirmation happens during the onDrop callback, so it can be removed from the options .delete(:confirm) if [:confirm] %(new Droppable(#{ActiveSupport::JSON.encode(element_id)}, #{()});) end |
#javascript_include_tag(first, *others) ⇒ Object
Overloading the rails method so it loaded RightJS by default
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/right_rails/helpers/rails.rb', line 10 def javascript_include_tag(first, *others) if (first == :defaults) = others.last.is_a?(Hash) ? others.pop : nil rightjs_require_module *others scripts = RightRails::Helpers.required_js_files(self) scripts << 'application' scripts << unless .nil? super *scripts else super first, *others end end |
#periodically_call_remote(options = {}) ⇒ Object
Replacing ‘periodically_call_remote` method
73 74 75 76 77 |
# File 'lib/right_rails/helpers/rails.rb', line 73 def periodically_call_remote(={}) frequency = [:frequency] || 10 # every ten seconds by default code = "function(){#{remote_function()}}.periodical(#{frequency * 1000})" javascript_tag(code) end |
#remote_function(options) ⇒ Object
Overloading the ‘remote_function` helper so it used the RightJS semantics
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/right_rails/helpers/rails.rb', line 29 def remote_function() # assigning the url address url = [:url] url = url.merge(:escape => false) if url.is_a?(Hash) [:url] = escape_javascript(url_for(url)) cmd = RightRails::Helpers.build_xhr_request() cmd = "#{[:before]};#{cmd}" if [:before] cmd << ";#{[:after]}" if [:after] cmd = "if(#{[:condition]}){#{cmd}}" if [:condition] cmd = "if(confirm('#{escape_javascript([:confirm])}')){#{cmd}}" if [:confirm] cmd end |
#sortable_element(element_id, options = {}) ⇒ Object
143 144 145 |
# File 'lib/right_rails/helpers/rails.rb', line 143 def sortable_element(element_id, = {}) javascript_tag(sortable_element_js(element_id, ).chop!) end |
#sortable_element_js(id, options = {}) ⇒ Object
catching the sortables generator
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/right_rails/helpers/rails.rb', line 109 def sortable_element_js(id, ={}) rightjs_require_module 'dnd', 'sortable' script = "new Sortable('#{id}'" # processing the options [:url] = escape_javascript(url_for([:url])) if [:url] = RightRails::Helpers.(, 'sortable') script << ",#{}" unless == '{}' script << ")" end |
#submit_to_remote(name, value, options = {}) ⇒ Object
Overloading the ‘submit_to_remote` so it used RightJS instead
49 50 51 52 53 54 55 56 |
# File 'lib/right_rails/helpers/rails.rb', line 49 def submit_to_remote(name, value, = {}) [:submit] ||= "#{RightRails::Helpers.prefix}$(this.form)" = .delete(:html) || {} [:name] = name (value, , ) end |