lib/jqr-helpers/helpers.rb in jqr-helpers-1.0.4 vs lib/jqr-helpers/helpers.rb in jqr-helpers-1.0.5
- old
+ new
@@ -1,5 +1,7 @@
+require 'securerandom'
+
module JqrHelpers
module Helpers
# A renderer used for tabs, accordions, etc.
class PanelRenderer
@@ -25,11 +27,11 @@
content = ''
id = nil
else
options = url_or_options
content = yield
- id = (0...8).map { (65 + rand(26)).chr }.join # random string
+ id = _random_string
url = '#' + id
end
options.merge!(:id => id)
panels << {
:title => title,
@@ -117,46 +119,40 @@
# 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.
- # @param id [String] A unique ID to use to reference the dialog options.
- # This is ultimately created as an element with that ID in the DOM,
- # but the element does not have to exist already, unlike link_to_dialog.
# @param url [String] The URL to load the content from.
# @param html_content [String] Text or HTML tags to use as the link body.
# @param dialog_options [Hash] See above.
# @param html_options [Hash] 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.
# @return [String]
- def link_to_remote_dialog(id, url, html_content, dialog_options={},
+ 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(id, html_content, dialog_options, html_options)
+ link_to_dialog(_random_string, html_content, dialog_options, html_options)
end
# Same as button_to_dialog, but loads content from a remote URL instead of
# using content already on the page.
- # @param id [String] A unique ID to use to reference the dialog options.
- # This is ultimately created as an element with that ID in the DOM,
- # but the element does not have to exist already, unlike button_to_dialog.
# @param url [String] The URL to load the content from.
# @param html_content [String] Text or HTML tags to use as the button body.
# @param dialog_options [Hash] See above.
# @param html_options [Hash] Attributes to put on the button tag.
# @return [String]
- def button_to_remote_dialog(id, url, html_content, dialog_options={},
+ def button_to_remote_dialog(url, html_content, dialog_options={},
html_options={})
- link_to_remote_dialog(id, url, html_content, dialog_options,
+ link_to_remote_dialog(url, html_content, dialog_options,
html_options.merge(:tag_name => 'button'))
end
# Create a link that fires off a jQuery Ajax request. This is basically
# a wrapper around link_to :remote => true.
@@ -281,9 +277,15 @@
html_options[:class] << ' ujs-date-picker'
text_field_tag(name, value, html_options)
end
private
+
+ # Generate a random string for IDs.
+ # @return [String]
+ def _random_string
+ SecureRandom.hex(16)
+ end
# Process options related to Ajax requests (e.g. button_to_ajax).
# @param options [Hash]
# @return [Hash] HTML options to inject.
def _process_ajax_options(options)