Module: JqrHelpers::Helpers
- Defined in:
- lib/jqr-helpers/helpers.rb
Defined Under Namespace
Classes: PanelRenderer
Class Method Summary collapse
-
._random_string ⇒ String
Generate a random string for IDs.
Instance Method Summary collapse
-
#ajax_change(url, options = {}, &block) ⇒ String
Observe a field for changes.
-
#button_to_ajax(body, url, options = {}) ⇒ String
Create a button that fires off a jQuery Ajax request.
-
#button_to_dialog(dialog_id, html_content, dialog_options = {}, html_options = {}) ⇒ String
Add a button to create a jQuery dialog.
-
#button_to_external(content, url, options = {}) ⇒ Object
This is identical to the built-in Rails button_to() in every way except that it will work inside an existing form.
-
#button_to_remote_dialog(url, html_content, dialog_options = {}, html_options = {}) ⇒ String
Same as button_to_dialog, but loads content from a remote URL instead of using content already on the page.
-
#button_to_toggle(body, id, options = {}) ⇒ Object
Add a button which toggles the visibility of a separate element.
-
#buttonset(name, values, selected = nil, html_options = {}) ⇒ Object
Print a button set.
-
#confirm_button(html_content, url, message, html_options = {}) ⇒ Object
Create a button that prompts a jQuery confirm dialog, which is nicer-looking than the default window.confirm() which is used by Rails.
-
#date_picker_tag(name, value = Date.today, options = {}, html_options = {}) ⇒ String
Create a date picker field.
-
#dialog_title(content) ⇒ Object
Set the dialog title from
inside
the dialog itself. -
#form_for_ajax(record, options = {}, &block) ⇒ String
Identical to form_tag_ajax except that this passes the given model into form_for instead of form_tag.
-
#form_tag_ajax(url, options = {}, &block) ⇒ String
Create a form tag that submits to an Ajax request.
-
#link_to_ajax(body, url, options = {}, &block) ⇒ String
Create a link that fires off a jQuery Ajax request.
-
#link_to_dialog(dialog_id, html_content = '', dialog_options = {}, html_options = {}, &block) ⇒ String
Add a link to create a jQuery dialog.
-
#link_to_remote_dialog(url, html_content, dialog_options = {}, html_options = {}, &block) ⇒ String
Same as link_to_dialog, but loads content from a remote URL instead of using content already on the page.
-
#link_to_toggle(body, id, options = {}) ⇒ Object
Add a link which toggles the visibility of a separate element.
-
#quick_radio_set(name, values, selected = nil, html_options = {}) ⇒ Object
Prints a button set which pretends to be a jQuery buttonset() and is specifically for radio buttons.
-
#tab_container(options = {}, html_options = {}, &block) ⇒ Object
Print a tab container.
-
#will_paginate_ajax(collection, to_update, options = {}) ⇒ String
Create a will_paginate pagination interface which runs via Ajax.
Class Method Details
._random_string ⇒ String
Generate a random string for IDs.
446 447 448 |
# File 'lib/jqr-helpers/helpers.rb', line 446 def self._random_string SecureRandom.hex(16) end |
Instance Method Details
#ajax_change(url, options = {}, &block) ⇒ String
Observe a field for changes. On change, post to the Ajax URL and calculate callbacks.
436 437 438 439 440 441 |
# File 'lib/jqr-helpers/helpers.rb', line 436 def ajax_change(url, ={}, &block) [:class] = 'ujs-ajax-change' = () ['data-url'] = url content_tag :span, , &block end |
#button_to_ajax(body, url, options = {}) ⇒ String
Create a button that fires off a jQuery Ajax request. This does not use button_to, so it can be used inside forms.
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/jqr-helpers/helpers.rb', line 200 def (body, url, ={}) # Specifically do not add data-remote [:'data-method'] = .delete(:method) [:class] ||= '' [:class] << ' ujs-ajax-button' [:type] = 'button' [:'data-url'] = url if .key?(:confirm) [:'data-confirm'] = .delete(:confirm) end .merge!(()) content_tag :button, body, end |
#button_to_dialog(dialog_id, html_content, dialog_options = {}, html_options = {}) ⇒ String
Add a button to create a jQuery dialog.
102 103 104 105 106 |
# File 'lib/jqr-helpers/helpers.rb', line 102 def (dialog_id, html_content, ={}, ={}) link_to_dialog(dialog_id, html_content, , .merge(:tag_name => 'button', :type => 'button')) end |
#button_to_external(content, url, options = {}) ⇒ Object
This is identical to the built-in Rails button_to() in every way except that it will work inside an existing form. Instead, it appends a form to the body, and uses a click handler to submit it. This does not support the :remote option - instead, use #button_to_ajax. This supports the :target option to pass to the form.
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/jqr-helpers/helpers.rb', line 376 def (content, url, ={}) [:disabled] = 'disabled' if .delete(:disabled) method = (.delete(:method) || 'post').to_s.downcase confirm = .delete(:confirm) disable_with = .delete(:disable_with) token_name = token_value = nil if %w(post put).include?(method) && protect_against_forgery? token_name = request_forgery_protection_token.to_s token_value = form_authenticity_token end url = url_for(url) if url.is_a?(Hash) [:type] = 'button' ['data-confirm'] = confirm if confirm ['data-disable-with'] = disable_with if disable_with ['data-method'] = method if method ['data-url'] = url [:'data-target'] = .delete(:target) [:class] ||= '' [:class] << ' ujs-external-button' if token_name ['data-token-name'] = token_name ['data-token-value'] = token_value end content_tag(:button, content, ) end |
#button_to_remote_dialog(url, html_content, dialog_options = {}, html_options = {}) ⇒ String
Same as button_to_dialog, but loads content from a remote URL instead of using content already on the page.
161 162 163 164 165 |
# File 'lib/jqr-helpers/helpers.rb', line 161 def (url, html_content, ={}, ={}) link_to_remote_dialog(url, html_content, , .merge(:tag_name => 'button', :type => 'button')) end |
#button_to_toggle(body, id, options = {}) ⇒ Object
Add a button which toggles the visibility of a separate element.
427 428 429 |
# File 'lib/jqr-helpers/helpers.rb', line 427 def (body, id, ={}) link_to_toggle(body, id, .merge(:tag_name => 'button')) end |
#buttonset(name, values, selected = nil, html_options = {}) ⇒ Object
Print a button set. Each button will be a radio button, and the group will then be passed into jQuery’s buttonset() method.
322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/jqr-helpers/helpers.rb', line 322 def (name, values, selected=nil, ={}) [:class] ||= '' [:class] << ' ujs-button-set' given_id = .delete(:input_id) content = values.inject('') do |sum, (value, label)| id = "#{sanitize_to_id(name)}_#{sanitize_to_id(value)}" id << "_#{given_id}" if given_id sum += (name, value, selected == value, :id => id) + label_tag(id, label) end content_tag(:div, raw(content), ) end |
#confirm_button(html_content, url, message, html_options = {}) ⇒ Object
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.
115 116 117 118 119 120 |
# File 'lib/jqr-helpers/helpers.rb', line 115 def (html_content, url, , ={}) html_content, url, .merge( :'data-message' => simple_format(), # turn text into HTML :'data-ujs-confirm' => true ) end |
#date_picker_tag(name, value = Date.today, options = {}, html_options = {}) ⇒ String
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.
299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/jqr-helpers/helpers.rb', line 299 def date_picker_tag(name, value=Date.today, ={}, ={}) format = .delete(:format) || '%Y-%m-%d' if value.present? value = Date.parse(value) if value.is_a?(String) value = value.strftime(format) end [:dateFormat] = _map_date(format) [:'data-date-options'] = .to_json [:class] ||= '' [:class] << ' ujs-date-picker' text_field_tag(name, value, ) end |
#dialog_title(content) ⇒ Object
Set the dialog title from inside
the dialog itself. This prints a hidden div which is read by the UJS callback to set the title.
169 170 171 |
# File 'lib/jqr-helpers/helpers.rb', line 169 def dialog_title(content) content_tag :div, content, :class => 'ujs-dialog-title-hidden' end |
#form_for_ajax(record, options = {}, &block) ⇒ String
Identical to form_tag_ajax except that this passes the given model into form_for instead of form_tag.
237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/jqr-helpers/helpers.rb', line 237 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] ||= {} orig_class = [:html][:class] [:html].merge!(()) [:html][:class] << " #{orig_class}" form_for record, , &block end |
#form_tag_ajax(url, options = {}, &block) ⇒ String
Create a form tag that submits to an Ajax request. Basically a wrapper for form_tag with :remote => true.
221 222 223 224 225 226 227 228 229 230 |
# File 'lib/jqr-helpers/helpers.rb', line 221 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 |
#link_to_ajax(body, url, options = {}, &block) ⇒ String
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.
181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/jqr-helpers/helpers.rb', line 181 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 |
#link_to_dialog(dialog_id, html_content = '', dialog_options = {}, html_options = {}, &block) ⇒ String
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.
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 94 |
# File 'lib/jqr-helpers/helpers.rb', line 58 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 |
#link_to_remote_dialog(url, html_content, dialog_options = {}, html_options = {}, &block) ⇒ String
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. Note that this method should only be used if the URL being linked to is valid to display by itself (i.e. the user can copy/paste it or open it in a new tab). If it is only viewable in a partial, you should be using button_to_remote_dialog.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/jqr-helpers/helpers.rb', line 137 def link_to_remote_dialog(url, html_content, ={}, ={}, &block) if block_given? = = html_content html_content = capture(&block) end [:href] = url [:'data-throbber'] = .delete(:throbber) || 'large' [:'data-dialog-url'] = url link_to_dialog(Helpers._random_string, html_content, , ) end |
#link_to_toggle(body, id, options = {}) ⇒ Object
Add a link which toggles the visibility of a separate element. The link element itself will be given the class “ujs-toggle-open” and “ujs-toggle-closed” so you can style it.
413 414 415 416 417 418 419 420 421 |
# File 'lib/jqr-helpers/helpers.rb', line 413 def link_to_toggle(body, id, ={}) tag_name = .delete(:tag_name) || :a [:class] ||= '' [:class] << ' ujs-toggle' [:class] << (.delete(:start_open) ? ' ujs-toggle-open' : ' ujs-toggle-closed') content_tag tag_name, body, .merge(:'data-id' => id) end |
#quick_radio_set(name, values, selected = nil, html_options = {}) ⇒ Object
Prints a button set which pretends to be a jQuery buttonset() and is specifically for radio buttons. The main difference is that this will load much faster in DOM-heavy pages (e.g. in tables where you may have hundreds of buttons) and it does not have most of the frills of jQuery button(), such as allowing disabling.
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/jqr-helpers/helpers.rb', line 345 def quick_radio_set(name, values, selected=nil, ={}) [:class] ||= '' [:class] << ' ujs-quick-buttonset ui-buttonset' [:autocomplete] = 'off' content = '' last_key = values.keys.length - 1 values.each_with_index do |(value, label), i| content << (name, value, selected == value, :class => 'ui-helper-hidden-accessible') label_class = 'ui-button ui-widget ui-state-default ui-button-text-only' label_class << ' ui-state-active' if selected == value label_class << ' ui-corner-left' if i == 0 label_class << ' ui-corner-right' if i == last_key content << label_tag("#{name}_#{value}", :class => label_class, :role => 'button', :'aria-disabled' => 'false') do content_tag :span, label, :class => 'ui-button-text' end end content_tag(:div, raw(content), ) end |
#tab_container(options = {}, html_options = {}, &block) ⇒ Object
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).
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/jqr-helpers/helpers.rb', line 264 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[:options], &panel[:content]) end sum end s + raw(s3) end end |
#will_paginate_ajax(collection, to_update, options = {}) ⇒ String
Create a will_paginate pagination interface which runs via Ajax. If will_paginate is not in the Gemfile or gem environment, this will throw an error.
518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
# File 'lib/jqr-helpers/helpers.rb', line 518 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 |