Module: JqrHelpers::Helpers

Defined in:
lib/jqr-helpers/helpers.rb

Defined Under Namespace

Classes: PanelRenderer

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._random_stringString

Generate a random string for IDs.

Returns:

  • (String)


310
311
312
# File 'lib/jqr-helpers/helpers.rb', line 310

def self._random_string
  SecureRandom.hex(16)
end

Instance Method Details

#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.

Parameters:

  • body (String)

    the text/content that goes inside the tag.

  • url (String)

    the URL to connect to.

  • options (Hash) (defaults to: {})

    Ajax options - see above.

Returns:

  • (String)


185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/jqr-helpers/helpers.rb', line 185

def button_to_ajax(body, url, options={})

  # Specifically do not add data-remote
  options[:'data-method'] = options.delete(:method)
  options[:'class'] ||= ''
  options[:'class'] << ' ujs-ajax-button'
  options[:'data-url'] = url
  if options.key?(:confirm)
    options[:'data-confirm'] = options.delete(:confirm)
  end
  options.merge!(_process_ajax_options(options))

   :button, body, options
end

#button_to_dialog(dialog_id, html_content, dialog_options = {}, html_options = {}) ⇒ String

Add a button to create a jQuery dialog.

Parameters:

  • dialog_id (String)

    The ID of the element to put in the dialog.

  • html_content (String)

    Text or HTML tags to use as the button body.

  • html_options (Hash) (defaults to: {})

    Attributes to put on the button tag.

  • dialog_options (Hash) (defaults to: {})

    See above.

Returns:

  • (String)


100
101
102
103
104
# File 'lib/jqr-helpers/helpers.rb', line 100

def button_to_dialog(dialog_id, html_content, dialog_options={},
  html_options={})
  link_to_dialog(dialog_id, html_content, dialog_options,
                 html_options.merge(:tag_name => 'button'))
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.

Parameters:

  • url (String)

    The URL to load the content from.

  • html_content (String)

    Text or HTML tags to use as the button body.

  • dialog_options (Hash) (defaults to: {})

    See above.

  • html_options (Hash) (defaults to: {})

    Attributes to put on the button tag.

Returns:

  • (String)


152
153
154
155
156
# File 'lib/jqr-helpers/helpers.rb', line 152

def button_to_remote_dialog(url, html_content, dialog_options={},
  html_options={})
  link_to_remote_dialog(url, html_content, dialog_options,
                 html_options.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.

Parameters:

  • name (String)

    the name of the form element.

  • values (Hash<String, String>)

    a hash of value => label.

  • selected (String) (defaults to: nil)

    the selected value, if any.

  • html_options (Hash) (defaults to: {})

    a set of options that will be passed into the parent div tag.



298
299
300
301
302
303
304
305
306
# File 'lib/jqr-helpers/helpers.rb', line 298

def buttonset(name, values, selected=nil, html_options={})
  html_options[:class] ||= ''
  html_options[:class] << ' ujs-button-set'
  content = values.inject('') do |sum, (value, label)|
    sum += radio_button_tag(name, value, selected == value) +
      label_tag("#{name}_#{value}", label)
  end
  (:div, raw(content), html_options)
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.

Parameters:

  • html_content (String)

    the text or content to go inside the button

  • url (String)

    the URL which the button should go to if confirmed

  • message (String)

    the confirm message to prompt

  • html_options (Hash)

    HTML attributes.



113
114
115
116
117
118
# File 'lib/jqr-helpers/helpers.rb', line 113

def confirm_button(html_content, url, message, html_options)
  button_to html_content, url, html_options.merge(
    :'data-message' => simple_format(message), # 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.

Parameters:

  • name (String)

    the name of the form element.

  • value (Date) (defaults to: Date.today)

    the initial value.

  • options (Hash) (defaults to: {})

    options to be passed to datepicker().

  • html_options (Hash) (defaults to: {})

    options to be passed to text_field_tag.

Returns:

  • (String)


281
282
283
284
285
286
287
288
289
# File 'lib/jqr-helpers/helpers.rb', line 281

def date_picker_tag(name, value=Date.today, options={}, html_options={})
  format = options.delete(:format) || '%Y-%m-%d'
  value = value.strftime(format)
  options[:dateFormat] = _map_date(format)
  html_options[:'data-date-options'] = options.to_json
  html_options[:class] ||= ''
  html_options[:class] << ' ujs-date-picker'
  text_field_tag(name, value, html_options)
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.

Parameters:

  • record (ActiveRecord::Base)
  • options (Hash) (defaults to: {})

Returns:

  • (String)


221
222
223
224
225
226
227
228
229
230
231
# File 'lib/jqr-helpers/helpers.rb', line 221

def form_for_ajax(record, options={}, &block)
  options[:remote] = true
  # note that we only override if nil - not false
  options[:close_dialog] = true if options[:close_dialog].nil?
  options[:use_dialog_opener] = true if options[:use_dialog_opener].nil?
  options[:html] ||= {}
  options[:html].merge!(_process_ajax_options(options))

  form_for record, options, &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.

Parameters:

  • url (String)

    the URL to connect to.

  • options (Hash) (defaults to: {})

    Ajax options - see above.

Returns:

  • (String)


205
206
207
208
209
210
211
212
213
214
# File 'lib/jqr-helpers/helpers.rb', line 205

def form_tag_ajax(url, options={}, &block)

  options[:remote] = true
  # note that we only override if nil - not false
  options[:close_dialog] = true if options[:close_dialog].nil?
  options[:use_dialog_opener] = true if options[:use_dialog_opener].nil?
  options.merge!(_process_ajax_options(options))

  form_tag url, options, &block
end

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.

Parameters:

  • body (String)

    the text/content that goes inside the tag.

  • url (String)

    the URL to connect to.

  • options (Hash) (defaults to: {})

    Ajax options - see above.

Returns:

  • (String)


166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/jqr-helpers/helpers.rb', line 166

def link_to_ajax(body, url, options={}, &block)
  if block_given?
    options = url
    url = body
    body = capture(&block)
  end

  options[:remote] = true
  options.merge!(_process_ajax_options(options))

  link_to body, url, options
end

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.

Parameters:

  • dialog_id (String)

    The ID of the element to put in the dialog.

  • html_content (String) (defaults to: '')

    Text or HTML tags to use as the link body.

  • dialog_options (Hash) (defaults to: {})

    See above.

  • html_options (Hash) (defaults to: {})

    Attributes to put on the link tag. There is a special :tag_name option that can be used to change the tag being created. Default is :a, but you can pass :div, :span, etc.

Returns:

  • (String)


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
# File 'lib/jqr-helpers/helpers.rb', line 57

def link_to_dialog(dialog_id, html_content='', dialog_options={},
  html_options={}, &block)

  if block_given?
    html_options = dialog_options
    dialog_options = html_content.presence || {}
    html_content = capture(&block)
  end

  html_options[:class] ||= ''
  html_options[:class] << ' ujs-dialog'
  html_options[:'data-dialog-id'] = dialog_id
  html_options[:'data-close-x'] = dialog_options[:close_x]

  tag_name = html_options.delete(:tag_name) || :a
  html_options[:href] = '#' if tag_name == :a

  if dialog_options[:title] == false # not nil or blank
    dialog_options[:dialogClass] ||= ''
    dialog_options[:dialogClass] << ' ujs-dialog-modal no-title'
  else
    dialog_options[:title] ||= 'Dialog'
  end
  dialog_options[:modal] = true
  dialog_options[:width] ||= 'auto'
  if dialog_options.delete(:default_buttons)
    dialog_options[:buttons] = {
      :OK => 'submit',
      :Cancel => 'close'
    }
  end

  html_options[:'data-dialog-options'] = dialog_options.to_json

   tag_name, html_content, html_options
end

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.

Parameters:

  • url (String)

    The URL to load the content from.

  • html_content (String)

    Text or HTML tags to use as the link body.

  • dialog_options (Hash) (defaults to: {})

    See above.

  • html_options (Hash) (defaults to: {})

    Attributes to put on the link tag. There is a special :tag_name option that can be used to change the tag being created. Default is :a, but you can pass :div, :span, etc.

Returns:

  • (String)


131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/jqr-helpers/helpers.rb', line 131

def link_to_remote_dialog(url, html_content, dialog_options={},
  html_options={}, &block)

  if block_given?
    html_options = dialog_options
    dialog_options = html_content
    html_content = capture(&block)
  end

  html_options[:'data-dialog-url'] = url
  link_to_dialog(Helpers._random_string, html_content,
                 dialog_options, html_options)
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).

Examples:

<%= tab_container {:collapsible => true}, {:class => 'my-tabs}' do |r| %>
  <%= r.panel 'Tab 1',  do %>
    My tab content here
  <% end %>
  <%= r.panel 'Tab 2', 'http://www.foobar.com/' %>
<% end %>

Parameters:

  • options (Hash) (defaults to: {})

    options to pass to the jQuery tabs() method.

  • html_options (Hash) (defaults to: {})

    options to pass to the tab container element itself.



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/jqr-helpers/helpers.rb', line 246

def tab_container(options={}, html_options={}, &block)
  renderer = PanelRenderer.new
  capture(renderer, &block)
  html_options[:class] ||= ''
  html_options[:class] << ' ujs-tab-container'
  html_options[:'data-tab-options'] = options.to_json
  (:div, html_options) do
    s =  :ul do
      s2 = ''
      renderer.panels.each do |panel|
        s2 << (: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 + (:div, panel[:content], panel[:options])
      end
      sum
    end
    s + raw(s3)
  end
end