module Sequent::Web::Sinatra::TagHelper
Exposes various helper methods for creating form tags
Public Instance Methods
calculate_name(field)
click to toggle source
# File lib/sequent-sinatra/tag_helper.rb, line 151 def calculate_name(field) reverse_names = tree_in_names(field, :postfix) "#{reverse_names.first}#{reverse_names[1..-1].map { |n| n == '[]' ? n : "[#{n}]" }.join}" end
full_path(field)
click to toggle source
# File lib/sequent-sinatra/tag_helper.rb, line 145 def full_path(field) tree_in_names(field, :postfix_for_id).join('_') end
Also aliased as: calculate_id
has_form_error?(name)
click to toggle source
# File lib/sequent-sinatra/tag_helper.rb, line 175 def has_form_error?(name) @errors.try(:has_key?, name.to_sym) end
i18n_name(field)
click to toggle source
# File lib/sequent-sinatra/tag_helper.rb, line 167 def i18n_name(field) if @path "#{@path}.#{field}" else field.to_s end end
merge_and_append_class_attributes(to_append, options = {})
click to toggle source
# File lib/sequent-sinatra/tag_helper.rb, line 161 def merge_and_append_class_attributes(to_append, options = {}) to_append.merge(options) do |key, oldval, newval| key == :class ? "#{oldval} #{newval}" : newval end end
param_or_default(field, default, options = {})
click to toggle source
# File lib/sequent-sinatra/tag_helper.rb, line 156 def param_or_default(field, default, options = {}) return options[:value] if options.has_key?(:value) @values.nil? ? default : @values.has_key?(field.to_s) ? @values[field.to_s] || default : default end
raw_checkbox(field, options={})
click to toggle source
creates a <input type=checkbox>
Parameters
+field+ the name of the attribute within the current object. +options+ Hash with optional attributes. :default - the default checked value if the current object has none :class - the css class
# File lib/sequent-sinatra/tag_helper.rb, line 16 def raw_checkbox(field, options={}) id = options[:id] || calculate_id(field) value = param_or_default(field, options.delete(:default), options) || id values = [value].compact field_value = param_or_default(field, false) checked = options.has_key?(:checked) ? options[:checked] : values.include?(field_value) checked = checked ? "checked" : nil single_tag :input, options.merge( :type => "checkbox", :id => id, :name => calculate_name(field), :value => value, checked: checked ) end
raw_email(field, options={})
click to toggle source
Creates a <input type=email>
Parameters
+field+ the name of the attribute within the current object. +options+ Hash with optional attributes. :default - the default value if the current object has none :class - the css class
# File lib/sequent-sinatra/tag_helper.rb, line 51 def raw_email(field, options={}) raw_field(field, "email", options) end
raw_input(field, options={})
click to toggle source
Creates a <input type=text>
Parameters
+field+ the name of the attribute within the current object. +options+ Hash with optional attributes. :default - the default value if the current object has none :class - the css class
# File lib/sequent-sinatra/tag_helper.rb, line 39 def raw_input(field, options={}) raw_field(field, "text", options) end
raw_password(field, options={})
click to toggle source
Creates a <input type=password>
Parameters
+field+ the name of the attribute within the current object. +options+ Hash with optional attributes. :default - the default value if the current object has none :class - the css class
# File lib/sequent-sinatra/tag_helper.rb, line 63 def raw_password(field, options={}) raw_field(field, "password", options) end
raw_radio(field, options = {})
click to toggle source
creates a <input type=radio>
By default it will check the radio button who's value is present in the backing object
Parameters
+field+ the name of the attribute within the current object. +options+ Hash with optional attributes. :default - the value of the radio option :checked - does this radio need to be checked :class - the css class
# File lib/sequent-sinatra/tag_helper.rb, line 131 def raw_radio(field, options = {}) raise "radio buttons need a value" unless options[:value] id = options[:id] || calculate_id(field) value = options.delete(:value) checked = (value == @values[field.to_s] || options.include?(:checked)) single_tag :input, options.merge( :type => "radio", :id => id, :name => calculate_name(field), :value => value, checked: checked ? "checked" : nil ) end
raw_select(field, values, options={})
click to toggle source
Creates a <select> with <option>
Parameters
+field+ the name of the attribute within the current object. +values+ an array of pairs (arrays) of [value, text_to_display] +options+ Hash with optional attributes. :default - the default value if the current object has none :class - the css class
# File lib/sequent-sinatra/tag_helper.rb, line 106 def raw_select(field, values, options={}) value = param_or_default(field, options.delete(:default), options) content = "" css_id = options[:id] || calculate_id(field) Array(values).each do |val| id, text = id_and_text_from_value(val) option_values = {value: id} option_values.merge!(selected: "selected") if (id == value) option_values.merge!(disabled: "disabled") if options[:disable].try(:include?, id) content << tag(:option, text, option_values) end tag :select, content, options.merge(id: css_id, name: calculate_name(field)) end
raw_textarea(field, options={})
click to toggle source
Creates a <textarea>
Parameters
+field+ the name of the attribute within the current object. +options+ Hash with optional attributes. :default - the default value if the current object has none :class - the css class :rows - the number of rows of the textarea, default 3
# File lib/sequent-sinatra/tag_helper.rb, line 76 def raw_textarea(field, options={}) value = param_or_default(field, options.delete(:default), options) id = options[:id] || calculate_id(field) with_closing_tag :textarea, value, {rows: "3"}.merge(options.merge( :id => id, :name => calculate_name(field) )) end
Private Instance Methods
data_attributes(data, prefix = 'data')
click to toggle source
# File lib/sequent-sinatra/tag_helper.rb, line 222 def data_attributes(data, prefix = 'data') case data when Hash data.reduce({}) { |memo, (key, value)| memo.merge(data_attributes(value, "#{prefix}-#{key}")) } else { prefix.gsub('_', '-').to_sym => data } end end
hash_to_html_attrs(options={})
click to toggle source
# File lib/sequent-sinatra/tag_helper.rb, line 195 def hash_to_html_attrs(options={}) raise %Q{Keys used in options must be a Symbol. Don't use {"class" => "col-md-4"} but use {class: "col-md-4"}} if options.keys.find { |k| not k.kind_of? Symbol } html_attrs = "" options.keys.sort.each do |key| next if options[key].nil? # do not include empty attributes html_attrs << %Q(#{key}="#{h(options[key])}" ) end html_attrs.chop end
id_and_text_from_value(val)
click to toggle source
# File lib/sequent-sinatra/tag_helper.rb, line 181 def id_and_text_from_value(val) if val.is_a? Array [val[0], val[1]] else [val, val] end end
raw_field(field, field_type, options)
click to toggle source
# File lib/sequent-sinatra/tag_helper.rb, line 205 def raw_field(field, field_type, options) value = param_or_default(field, options.delete(:default), options) if options[:formatter] value = self.send(options[:formatter], value) options.delete(:formatter) end #TODO use calculate_id id = options[:id] || calculate_id(field) options.merge!(data_attributes(options.delete(:data))) single_tag :input, options.merge( :type => field_type, :id => id, :name => calculate_name(field), :value => value, ) end
single_tag(name, options={})
click to toggle source
# File lib/sequent-sinatra/tag_helper.rb, line 246 def single_tag(name, options={}) "<#{name.to_s} #{hash_to_html_attrs(options)} />" end
tag(name, content, options={})
click to toggle source
# File lib/sequent-sinatra/tag_helper.rb, line 189 def tag(name, content, options={}) "<#{name.to_s}" + (options.length > 0 ? " #{hash_to_html_attrs(options)}" : '') + (content.nil? ? '>' : ">#{content}</#{name}>") end
tree_in_names(field, postfix_method_name)
click to toggle source
# File lib/sequent-sinatra/tag_helper.rb, line 231 def tree_in_names(field, postfix_method_name) if respond_to? :path names = [field, send(postfix_method_name), path].compact parent = @parent while parent.is_a? Fieldset names << parent.postfix if parent.send(postfix_method_name) names << parent.path parent = parent.parent end names.reverse else [field] end end
with_closing_tag(name, value, options={})
click to toggle source
# File lib/sequent-sinatra/tag_helper.rb, line 250 def with_closing_tag(name, value, options={}) %Q{<#{name.to_s} #{hash_to_html_attrs(options)} >#{h value}</#{name.to_s}>} end