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

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 button_to_function(name, *args, &block)
  html_options = args.extract_options!.symbolize_keys

  function = block_given? ? update_page(&block) : args[0] || ''
  onclick = "#{"#{html_options[:onclick]}; " if html_options[:onclick]}#{function};"

  tag(:input, html_options.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, options = {})
  javascript_tag(draggable_element_js(element_id, options).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, options)
  rightjs_require_module 'dnd'
  %(new Draggable(#{ActiveSupport::JSON.encode(element_id)}, #{options_for_javascript(options)});)
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, options = {})
  javascript_tag(drop_receiving_element_js(element_id, options).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, options={})
  rightjs_require_module 'dnd'

  options[:with]     ||= "'id=' + encodeURIComponent(draggable.element.id)"
  options[:onDrop]   ||= "function(draggable){" + remote_function(options) + "}"
  options.delete_if { |key, value| AJAX_OPTIONS.include?(key) }

  options[:accept] = array_or_string_for_javascript(options[:accept]) if options[:accept]
  options[:hoverclass] = "'#{options[:hoverclass]}'" if options[:hoverclass]

  # Confirmation happens during the onDrop callback, so it can be removed from the options
  options.delete(:confirm) if options[:confirm]

  %(new Droppable(#{ActiveSupport::JSON.encode(element_id)}, #{options_for_javascript(options)});)
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)
    options = others.last.is_a?(Hash) ? others.pop : nil

    rightjs_require_module *others

    scripts = RightRails::Helpers.required_js_files(self)
    scripts << 'application'
    scripts << options unless options.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(options={})
  frequency = options[:frequency] || 10 # every ten seconds by default
  code = "function(){#{remote_function(options)}}.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(options)
  # assigning the url address
  url = options[:url]
  url = url.merge(:escape => false) if url.is_a?(Hash)
  options[:url] = escape_javascript(url_for(url))

  cmd = RightRails::Helpers.build_xhr_request(options)

  cmd =  "#{options[:before]};#{cmd}" if options[:before]
  cmd << ";#{options[:after]}"        if options[:after]

  cmd = "if(#{options[:condition]}){#{cmd}}" if options[:condition]
  cmd = "if(confirm('#{escape_javascript(options[:confirm])}')){#{cmd}}" if options[: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, options = {})
  javascript_tag(sortable_element_js(element_id, options).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, options={})
  rightjs_require_module 'dnd', 'sortable'

  script = "new Sortable('#{id}'"

  # processing the options
  options[:url] = escape_javascript(url_for(options[:url])) if options[:url]
  s_options = RightRails::Helpers.unit_options(options, 'sortable')
  script << ",#{s_options}" unless s_options == '{}'

  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, options = {})
  options[:submit] ||= "#{RightRails::Helpers.prefix}$(this.form)"

  html_options = options.delete(:html) || {}
  html_options[:name] = name

  button_to_remote(value, options, html_options)
end