Module: JqrHelpers::Helpers
- Defined in:
- lib/jqr-helpers/helpers.rb
Defined Under Namespace
Classes: PanelRenderer
Class Method Summary (collapse)
-
+ (String) _random_string
Generate a random string for IDs.
Instance Method Summary (collapse)
-
- (String) button_to_ajax(body, url, options = {})
Create a button that fires off a jQuery Ajax request.
-
- (String) button_to_dialog(dialog_id, html_content, dialog_options = {}, html_options = {})
Add a button to create a jQuery dialog.
-
- (String) button_to_remote_dialog(url, html_content, dialog_options = {}, html_options = {})
Same as button_to_dialog, but loads content from a remote URL instead of using content already on the page.
-
- (Object) buttonset(name, values, selected = nil, html_options = {})
Print a button set.
-
- (Object) confirm_button(html_content, url, message, html_options = {})
Create a button that prompts a jQuery confirm dialog, which is nicer-looking than the default window.confirm() which is used by Rails.
-
- (String) date_picker_tag(name, value = Date.today, options = {}, html_options = {})
Create a date picker field.
-
- (String) form_for_ajax(record, options = {}, &block)
Identical to form_tag_ajax except that this passes the given model into form_for instead of form_tag.
-
- (String) form_tag_ajax(url, options = {}, &block)
Create a form tag that submits to an Ajax request.
-
- (String) link_to_ajax(body, url, options = {}, &block)
Create a link that fires off a jQuery Ajax request.
-
- (String) link_to_dialog(dialog_id, html_content = '', dialog_options = {}, html_options = {}, &block)
Add a link to create a jQuery dialog.
-
- (String) link_to_remote_dialog(url, html_content, dialog_options = {}, html_options = {}, &block)
Same as link_to_dialog, but loads content from a remote URL instead of using content already on the page.
-
- (Object) tab_container(options = {}, html_options = {}, &block)
Print a tab container.
-
- (String) will_paginate_ajax(collection, to_update, options = {})
Create a will_paginate pagination interface which runs via Ajax.
Class Method Details
+ (String) _random_string
Generate a random string for IDs.
313 314 315 |
# File 'lib/jqr-helpers/helpers.rb', line 313 def self._random_string SecureRandom.hex(16) end |
Instance Method Details
- (String) button_to_ajax(body, url, options = {})
Create a button that fires off a jQuery Ajax request. This does not use button_to, so it can be used inside forms.
188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/jqr-helpers/helpers.rb', line 188 def (body, url, ={}) # Specifically do not add data-remote [:data-method'] = .delete(:method) [:class'] ||= '' [:class'] << ' ujs-ajax-button' [:data-url'] = url if .key?(:confirm) [:data-confirm'] = .delete(:confirm) end .merge!(()) content_tag :button, body, end |
- (String) button_to_dialog(dialog_id, html_content, dialog_options = {}, html_options = {})
Add a button to create a jQuery dialog.
101 102 103 104 105 |
# File 'lib/jqr-helpers/helpers.rb', line 101 def (dialog_id, html_content, ={}, ={}) link_to_dialog(dialog_id, html_content, , .merge(:tag_name => 'button')) end |
- (String) button_to_remote_dialog(url, html_content, dialog_options = {}, html_options = {})
Same as button_to_dialog, but loads content from a remote URL instead of using content already on the page.
155 156 157 158 159 |
# File 'lib/jqr-helpers/helpers.rb', line 155 def (url, html_content, ={}, ={}) link_to_remote_dialog(url, html_content, , .merge(:tag_name => 'button')) end |
- (Object) buttonset(name, values, selected = nil, html_options = {})
Print a button set. Each button will be a radio button, and the group will then be passed into jQuery's buttonset() method.
301 302 303 304 305 306 307 308 309 |
# File 'lib/jqr-helpers/helpers.rb', line 301 def (name, values, selected=nil, ={}) [:class] ||= '' [:class] << ' ujs-button-set' content = values.inject('') do |sum, (value, label)| sum += (name, value, selected == value) + label_tag("#{name}_#{value}", label) end content_tag(:div, raw(content), ) end |
- (Object) confirm_button(html_content, url, message, html_options = {})
Create a button that prompts a jQuery confirm dialog, which is nicer-looking than the default window.confirm() which is used by Rails. Done using button_to, so note that a form element will be added.
114 115 116 117 118 119 |
# File 'lib/jqr-helpers/helpers.rb', line 114 def (html_content, url, , ={}) html_content, url, .merge( :data-message' => simple_format(), # turn text into HTML :data-ujs-confirm' => true ) end |
- (String) date_picker_tag(name, value = Date.today, options = {}, html_options = {})
Create a date picker field. The attributes given are passed to text_field_tag. There is a special option :format - this expects a Ruby style date format. It will format both the initial display of the date and the jQuery date format to be the same.
284 285 286 287 288 289 290 291 292 |
# File 'lib/jqr-helpers/helpers.rb', line 284 def date_picker_tag(name, value=Date.today, ={}, ={}) format = .delete(:format) || '%Y-%m-%d' value = value.strftime(format) [:dateFormat] = _map_date(format) [:data-date-options'] = .to_json [:class] ||= '' [:class] << ' ujs-date-picker' text_field_tag(name, value, ) end |
- (String) form_for_ajax(record, options = {}, &block)
Identical to form_tag_ajax except that this passes the given model into form_for instead of form_tag.
224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/jqr-helpers/helpers.rb', line 224 def form_for_ajax(record, ={}, &block) [:remote] = true # note that we only override if nil - not false [:close_dialog] = true if [:close_dialog].nil? [:use_dialog_opener] = true if [:use_dialog_opener].nil? [:html] ||= {} [:html].merge!(()) form_for record, , &block end |
- (String) form_tag_ajax(url, options = {}, &block)
Create a form tag that submits to an Ajax request. Basically a wrapper for form_tag with :remote => true.
208 209 210 211 212 213 214 215 216 217 |
# File 'lib/jqr-helpers/helpers.rb', line 208 def form_tag_ajax(url, ={}, &block) [:remote] = true # note that we only override if nil - not false [:close_dialog] = true if [:close_dialog].nil? [:use_dialog_opener] = true if [:use_dialog_opener].nil? .merge!(()) form_tag url, , &block end |
- (String) link_to_ajax(body, url, options = {}, &block)
Create a link that fires off a jQuery Ajax request. This is basically a wrapper around link_to :remote => true. If a block is given, url and options will be shifted left by 1 position and the block contents will be used for the body.
169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/jqr-helpers/helpers.rb', line 169 def link_to_ajax(body, url, ={}, &block) if block_given? = url url = body body = capture(&block) end [:remote] = true .merge!(()) link_to body, url, end |
- (String) link_to_dialog(dialog_id, html_content = '', dialog_options = {}, html_options = {}, &block)
Add a link to create a jQuery dialog. If a block is given, dialog_options and html_options are shifted left by 1 and the block is used as the html_content.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/jqr-helpers/helpers.rb', line 57 def link_to_dialog(dialog_id, html_content='', ={}, ={}, &block) if block_given? = = html_content.presence || {} html_content = capture(&block) end [:class] ||= '' [:class] << ' ujs-dialog' [:data-dialog-id'] = dialog_id [:data-close-x'] = [:close_x] tag_name = .delete(:tag_name) || :a [:href] = '#' if tag_name == :a [:dialogClass] ||= '' if [:title] == false # not nil or blank [:dialogClass] << ' ujs-dialog-modal no-title' else [:title] ||= 'Dialog' [:dialogClass] << ' ujs-dialog-modal' end [:modal] = true [:width] ||= 'auto' if .delete(:default_buttons) [:buttons] = { :OK => 'submit', :Cancel => 'close' } end [:data-dialog-options'] = .to_json content_tag tag_name, html_content, end |
- (String) link_to_remote_dialog(url, html_content, dialog_options = {}, html_options = {}, &block)
Same as link_to_dialog, but loads content from a remote URL instead of using content already on the page. If a block is given, dialog_options and html_options are shifted left by 1 and the block is used as the html_content.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/jqr-helpers/helpers.rb', line 132 def link_to_remote_dialog(url, html_content, ={}, ={}, &block) if block_given? = = html_content html_content = capture(&block) end [:data-throbber'] = .delete(:throbber) || 'large' [:data-dialog-url'] = url link_to_dialog(Helpers._random_string, html_content, , ) end |
- (Object) tab_container(options = {}, html_options = {}, &block)
Print a tab container. This expects a block, which will be passed a PanelRenderer object. Panels can be local (with content) or remote (with a URL).
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/jqr-helpers/helpers.rb', line 249 def tab_container(={}, ={}, &block) renderer = PanelRenderer.new capture(renderer, &block) [:class] ||= '' [:class] << ' ujs-tab-container' [:data-tab-options'] = .to_json content_tag(:div, ) do s = content_tag :ul do s2 = '' renderer.panels.each do |panel| s2 << content_tag(:li) do link_to panel[:title], panel[:url] end end raw s2 end s3 = renderer.panels.inject('') do |sum, panel| if panel[:options][:id] sum = sum + content_tag(:div, panel[:content], panel[:options]) end sum end s + raw(s3) end end |
- (String) will_paginate_ajax(collection, to_update, options = {})
Create a will_paginate pagination interface which runs via Ajax.
383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
# File 'lib/jqr-helpers/helpers.rb', line 383 def will_paginate_ajax(collection, to_update, ={}) if defined?(AjaxLinkRenderer) [:data-type'] = 'html' [:data-result-method'] = 'update' [:data-selector'] = to_update [:data-remote'] = true [:data-scroll-to'] = true [:data-throbber'] = [:throbber] || 'large' [:renderer] = AjaxLinkRenderer will_paginate(collection, ) else raise 'will_paginate not installed!' end end |