Class: Padrino::Helpers::FormBuilder::PadrinoFieldsBuilder
- Inherits:
-
AbstractFormBuilder
- Object
- AbstractFormBuilder
- Padrino::Helpers::FormBuilder::PadrinoFieldsBuilder
- Defined in:
- lib/padrino-fields/form_builder.rb
Overview
:nodoc:
Constant Summary collapse
- @@settings =
PadrinoFields::Settings
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#object_name ⇒ Object
readonly
Returns the value of attribute object_name.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Instance Method Summary collapse
- #collect_inputs_as(attribute, type, options = {}) ⇒ Object
- #collection_input(attribute, type, item, options = {}) ⇒ Object
- #css_class(attribute, type, disabled = false) ⇒ Object
- #default_input(attribute, type, options = {}) ⇒ Object
- #default_radios(attribute, type, options) ⇒ Object
- #domize(text) ⇒ Object
- #hint(text) ⇒ Object
- #html_options(attribute, type, options) ⇒ Object
- #input(attribute, options = {}) ⇒ Object
- #klazz ⇒ Object
- #labelize(options) ⇒ Object
- #required?(attribute) ⇒ Boolean
- #setup_label(attribute, type, options = {}) ⇒ Object
- #singular_input_for(attribute, type, options = {}) ⇒ Object
Methods included from PadrinoFields::Settings
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
16 17 18 |
# File 'lib/padrino-fields/form_builder.rb', line 16 def object @object end |
#object_name ⇒ Object (readonly)
Returns the value of attribute object_name.
16 17 18 |
# File 'lib/padrino-fields/form_builder.rb', line 16 def object_name @object_name end |
#template ⇒ Object (readonly)
Returns the value of attribute template.
16 17 18 |
# File 'lib/padrino-fields/form_builder.rb', line 16 def template @template end |
Instance Method Details
#collect_inputs_as(attribute, type, options = {}) ⇒ Object
67 68 69 70 71 |
# File 'lib/padrino-fields/form_builder.rb', line 67 def collect_inputs_as(attribute,type,={}) [:options].map do |item| collection_input(attribute,type,item,.keep_if {|key, value| key != :options}) end.join("") end |
#collection_input(attribute, type, item, options = {}) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/padrino-fields/form_builder.rb', line 106 def collection_input(attribute,type,item, ={}) unchecked_value = .delete(:uncheck_value) || '0' .reverse_merge!(:id => field_id(attribute), :value => '1') .reverse_merge!(:checked => true) if values_matches_field?(attribute, [:value]) klass = css_class(attribute,type,[:disabled]) name = type == :checks ? field_name(attribute) + '[]' : field_name(attribute) if item.is_a?(Array) text, value = item[0], item[1] else text = item; value = item; end id = field_id(attribute) + "_" + domize(text) opts = (attribute,type,.merge(:id => id, :class => klass, :value => value)) if type == :checks html = @template.hidden_field_tag([:name] || name, :value => unchecked_value, :id => nil) input_item = @template.check_box_tag(name, opts) else html = "" input_item = @template.(name, opts) end html << "<label for='#{id}' class='#{klass}'>#{input_item}#{text}</label>" end |
#css_class(attribute, type, disabled = false) ⇒ Object
143 144 145 146 147 148 |
# File 'lib/padrino-fields/form_builder.rb', line 143 def css_class(attribute,type,disabled=false) klass = type.to_s klass << ' required' if required?(attribute) klass << ' required' if disabled klass end |
#default_input(attribute, type, options = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/padrino-fields/form_builder.rb', line 36 def default_input(attribute,type,={}) = .keep_if {|key, value| key != :as} if [:options] || [:grouped_options] if type==:radios || type == :checks collect_inputs_as(attribute,type,) else select(attribute,) end else singular_input_for(attribute,type,) end end |
#default_radios(attribute, type, options) ⇒ Object
170 171 172 173 174 |
# File 'lib/padrino-fields/form_builder.rb', line 170 def default_radios(attribute,type,) [['yes',1],['no',0]].map do |item| collection_input(attribute,:radios,item,.keep_if {|key, value| key != :options}) end.join("") end |
#domize(text) ⇒ Object
129 130 131 |
# File 'lib/padrino-fields/form_builder.rb', line 129 def domize(text) text.downcase.gsub(' ','_').gsub(/[^[:alnum:]]/, '') end |
#hint(text) ⇒ Object
166 167 168 |
# File 'lib/padrino-fields/form_builder.rb', line 166 def hint(text) @template.content_tag(:span, :class=>'hint') { text } end |
#html_options(attribute, type, options) ⇒ Object
150 151 152 153 154 155 156 157 158 159 |
# File 'lib/padrino-fields/form_builder.rb', line 150 def (attribute,type,) .keep_if {|key, value| key != :as} html_class = .merge(:class => css_class(attribute,type,[:disabled])) input_html = [:input_html] if input_html html_class.merge(input_html) {|key, first, second| first + " " + second }.delete(:input_html) else html_class end end |
#input(attribute, options = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/padrino-fields/form_builder.rb', line 18 def input(attribute, ={}) .reverse_merge!(:caption => .delete(:caption)) if [:caption] type = [:as] || klazz.form_column_type_for(attribute) field_html = "" if type == :boolean || [:as] == :boolean field_html << default_input(attribute,type,) field_html << setup_label(attribute,type,labelize()) else field_html << setup_label(attribute,type,labelize()) field_html << default_input(attribute,type, ) end field_html << hint([:hint]) if [:hint] field_html << @template.(@object,attribute,{}) if @object.errors.any? @template.content_tag(@@settings.container, :class => css_class(attribute,type,[:disabled])) do field_html end end |
#klazz ⇒ Object
176 177 178 |
# File 'lib/padrino-fields/form_builder.rb', line 176 def klazz @object.class end |
#labelize(options) ⇒ Object
161 162 163 164 |
# File 'lib/padrino-fields/form_builder.rb', line 161 def labelize() opts = {}.merge() opts.keep_if {|key, value| [:class,:id,:caption].include?(key)} end |
#required?(attribute) ⇒ Boolean
101 102 103 104 |
# File 'lib/padrino-fields/form_builder.rb', line 101 def required?(attribute) k = klazz.is_a?(String) ? klazz.constantize : klazz k.form_attribute_is_required?(attribute) end |
#setup_label(attribute, type, options = {}) ⇒ Object
133 134 135 136 137 138 139 140 141 |
# File 'lib/padrino-fields/form_builder.rb', line 133 def setup_label(attribute, type, ={}) marker = @@settings.label_required_marker text = "" text << marker if required?(attribute) && @@settings.label_required_marker_position == :prepend text << field_human_name(attribute) text << marker if required?(attribute) && @@settings.label_required_marker_position == :append .reverse_merge!(:caption => text, :class => css_class(attribute,type,[:disabled])) @template.label_tag(field_id(attribute), ) end |
#singular_input_for(attribute, type, options = {}) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/padrino-fields/form_builder.rb', line 73 def singular_input_for(attribute,type,={}) .keep_if {|key, value| key != :for} opts = (attribute,type,) case type when :string case attribute when /email/ ; email_field(attribute, opts); when /password/ ; password_field(attribute, opts); when /tel/,/phone/ ; tel_field(attribute, opts); when /url/,/website/ ; url_field(attribute, opts); when /search/ ; search_field(attribute, opts); else ; text_field(attribute, opts); end when :text text_area(attribute, opts) when :boolean check_box(attribute, opts) when :date date_field(attribute, opts) when :file file_field(attribute, opts) when :number number_field(attribute, opts) when :radios default_radios(attribute,type,) end end |