Class: Formulate::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- Formulate::FormBuilder
- Defined in:
- lib/formulate/form_builder.rb
Instance Method Summary collapse
- #area(method, options = {}, &block) ⇒ Object
- #checkbox(method, options = {}, &block) ⇒ Object
- #country(method, options = {}, &block) ⇒ Object
- #date(method, options = {}, &block) ⇒ Object
- #date_time(method, options = {}, &block) ⇒ Object
- #email(method, options = {}, &block) ⇒ Object
- #errors ⇒ Object
- #expiration(method, options = {}, &block) ⇒ Object
- #fieldset(options = {}, &block) ⇒ Object
- #file(method, options = {}, &block) ⇒ Object
- #input(method, options = {}, &block) ⇒ Object
- #instructions(text_or_nil_with_block = nil, &block) ⇒ Object
- #number(method, options = {}, &block) ⇒ Object
- #password(method, options = {}, &block) ⇒ Object
- #radio(method, options = {}, &block) ⇒ Object
- #section(options = {}, &block) ⇒ Object
- #select(*args, &block) ⇒ Object
- #state(method, options = {}, &block) ⇒ Object
- #submit(value, options = {}, &block) ⇒ Object
- #text(method, options = {}, &block) ⇒ Object
- #time(method, options = {}, &block) ⇒ Object
- #time_zone(method, options, &block) ⇒ Object
Instance Method Details
#area(method, options = {}, &block) ⇒ Object
132 133 134 |
# File 'lib/formulate/form_builder.rb', line 132 def area(method, ={}, &block) input(method, .merge(type: :text_area), &block) end |
#checkbox(method, options = {}, &block) ⇒ Object
136 137 138 |
# File 'lib/formulate/form_builder.rb', line 136 def checkbox(method, ={}, &block) input(method, .merge(type: :check_box), &block) end |
#country(method, options = {}, &block) ⇒ Object
174 175 176 |
# File 'lib/formulate/form_builder.rb', line 174 def country(method, ={}, &block) input(method, .merge(type: :country_select), &block) end |
#date(method, options = {}, &block) ⇒ Object
158 159 160 |
# File 'lib/formulate/form_builder.rb', line 158 def date(method, ={}, &block) input(method, .merge(type: :date_select), &block) end |
#date_time(method, options = {}, &block) ⇒ Object
166 167 168 |
# File 'lib/formulate/form_builder.rb', line 166 def date_time(method, ={}, &block) input(method, .merge(type: :datetime_select), &block) end |
#email(method, options = {}, &block) ⇒ Object
120 121 122 |
# File 'lib/formulate/form_builder.rb', line 120 def email(method, ={}, &block) input(method, .merge(type: :email_field), &block) end |
#errors ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/formulate/form_builder.rb', line 5 def errors if (errors = errors_on(:base)).any? header = I18n.t('errors.template.header', count: errors.length) @template.capture_haml do @template.haml_tag(:fieldset, class: 'errors') do @template.haml_tag(:legend, header) @template.haml_tag(:ul, class: 'errors') do errors.each { || @template.haml_tag(:li, ) } end end end end end |
#expiration(method, options = {}, &block) ⇒ Object
178 179 180 181 |
# File 'lib/formulate/form_builder.rb', line 178 def expiration(method, ={}, &block) .reverse_merge!(add_month_numbers: true, discard_day: true, order: [:month, :year], start_year: Date.today.year, prompt: '') input(method, .merge(type: :date_select), &block) end |
#fieldset(options = {}, &block) ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/formulate/form_builder.rb', line 21 def fieldset(={}, &block) legend = .delete(:legend) @template.capture_haml do @template.haml_tag(:fieldset, ) do @template.haml_tag(:legend, legend) if legend yield(self) end end end |
#file(method, options = {}, &block) ⇒ Object
144 145 146 |
# File 'lib/formulate/form_builder.rb', line 144 def file(method, ={}, &block) input(method, .merge(type: :file_field), &block) end |
#input(method, options = {}, &block) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/formulate/form_builder.rb', line 40 def input(method, ={}, &block) type = .delete(:type) label = .delete(:label) prefix = .delete(:prefix) suffix = .delete(:suffix) instructions = .delete(:instructions) errors = errors_on(method) classes = ['field'] classes << ([:required] ? 'required' : 'optional') classes << 'checkbox' if type == :check_box classes << 'radio' if type == :radio_button classes << 'error' if errors.any? classes << .delete(:class) if [:class] classes.uniq! if [:placeholder] == true [:placeholder] = object.class.human_attribute_name(method) end input = case type when :check_box checked_value = .delete(:checked_value) unchecked_value = .delete(:unchecked_value) checked_value = '1' if unchecked_value && checked_value.blank? arguments = [, checked_value, unchecked_value].compact send(type, method, *arguments) when :radio_button value = .delete(:value) object_method = method method = "#{method.to_s.gsub(/\?$/, '')}_#{value.gsub(/\s/, '_')}".downcase send(type, object_method, value, ) when :date_select @template.capture_haml do @template.haml_tag(:div, input, class: 'group') end when :collection_select collection = .delete(:collection) value_method = .delete(:value_method) text_method = .delete(:text_method) = .delete(:html) || {} send(type, method, collection, value_method, text_method, , ) when :time_zone_select priority_zones = .delete(:priority_zones) send(type, method, priority_zones, ) when :state_select country = .delete(:country) || Carmen.default_country = .delete(:html) || {} send(type, method, country, , ) when :country_select priority_countries = .delete(:priority_countries) = .delete(:html) || {} send(type, method, priority_countries, , ) else send(type, method, ) end unless block_given? label = label != false ? label(method, label) : nil markup = [label, prefix, input, suffix].compact markup.reverse! if type.in?([:check_box, :radio_button]) markup << @template.capture_haml do yield(object.send(method)) if block_given? errors_list(errors) instructions(instructions) end @template.capture_haml do @template.haml_tag(:div, class: classes.join(' ')) do @template.haml_concat(markup.join) end end end |
#instructions(text_or_nil_with_block = nil, &block) ⇒ Object
187 188 189 190 191 192 |
# File 'lib/formulate/form_builder.rb', line 187 def instructions(text_or_nil_with_block=nil, &block) if text_or_nil_with_block content = text_or_nil_with_block || @template.capture_haml(&block) @template.haml_tag(:p, content, class: 'instructions') end end |
#number(method, options = {}, &block) ⇒ Object
124 125 126 |
# File 'lib/formulate/form_builder.rb', line 124 def number(method, ={}, &block) input(method, .merge(type: :number_field), &block) end |
#password(method, options = {}, &block) ⇒ Object
128 129 130 |
# File 'lib/formulate/form_builder.rb', line 128 def password(method, ={}, &block) input(method, .merge(type: :password_field), &block) end |
#radio(method, options = {}, &block) ⇒ Object
140 141 142 |
# File 'lib/formulate/form_builder.rb', line 140 def radio(method, ={}, &block) input(method, .merge(type: :radio_button), &block) end |
#section(options = {}, &block) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/formulate/form_builder.rb', line 32 def section(={}, &block) [:class] = "#{[:class]} section".strip @template.capture_haml do @template.haml_tag(:div, ) { yield(self) } end end |
#select(*args, &block) ⇒ Object
148 149 150 151 152 153 154 155 156 |
# File 'lib/formulate/form_builder.rb', line 148 def select(*args, &block) method = args.shift = args. [:collection] = args[0] [:value_method] = args[1] [:text_method] = args[2] input(method, .merge(type: :collection_select), &block) end |
#state(method, options = {}, &block) ⇒ Object
170 171 172 |
# File 'lib/formulate/form_builder.rb', line 170 def state(method, ={}, &block) input(method, .merge(type: :state_select), &block) end |
#submit(value, options = {}, &block) ⇒ Object
194 195 196 197 198 199 200 201 |
# File 'lib/formulate/form_builder.rb', line 194 def submit(value, ={}, &block) @template.capture_haml do @template.haml_tag(:div, class: 'submit') do @template.haml_concat(super(value, )) yield(self) if block_given? end end end |
#text(method, options = {}, &block) ⇒ Object
116 117 118 |
# File 'lib/formulate/form_builder.rb', line 116 def text(method, ={}, &block) input(method, .merge(type: :text_field), &block) end |
#time(method, options = {}, &block) ⇒ Object
162 163 164 |
# File 'lib/formulate/form_builder.rb', line 162 def time(method, ={}, &block) input(method, .merge(type: :time_select), &block) end |
#time_zone(method, options, &block) ⇒ Object
183 184 185 |
# File 'lib/formulate/form_builder.rb', line 183 def time_zone(method, , &block) input(method, .merge(type: :time_zone_select), &block) end |