Class: Formulate::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/formulate/form_builder.rb

Instance Method Summary collapse

Instance Method Details

#area(method, options = {}, &block) ⇒ Object



132
133
134
# File 'lib/formulate/form_builder.rb', line 132

def area(method, options={}, &block)
  input(method, options.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, options={}, &block)
  input(method, options.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, options={}, &block)
  input(method, options.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, options={}, &block)
  input(method, options.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, options={}, &block)
  input(method, options.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, options={}, &block)
  input(method, options.merge(type: :email_field), &block)
end

#errorsObject



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 { |message| @template.haml_tag(:li, message) }
        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, options={}, &block)
  options.reverse_merge!(add_month_numbers: true, discard_day: true, order: [:month, :year], start_year: Date.today.year, prompt: '')
  input(method, options.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(options={}, &block)
  legend = options.delete(:legend)

  @template.capture_haml do
    @template.haml_tag(:fieldset, options) 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, options={}, &block)
  input(method, options.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, options={}, &block)
  type = options.delete(:type)
  label = options.delete(:label)
  prefix = options.delete(:prefix)
  suffix = options.delete(:suffix)
  instructions = options.delete(:instructions)
  errors = errors_on(method)

  classes = ['field']
  classes << (options[:required] ? 'required' : 'optional')
  classes << 'checkbox' if type == :check_box
  classes << 'radio' if type == :radio_button
  classes << 'error' if errors.any?
  classes << options.delete(:class) if options[:class]
  classes.uniq!

  if options[:placeholder] == true
    options[:placeholder] = object.class.human_attribute_name(method)
  end

  input = case type
    when :check_box
      checked_value = options.delete(:checked_value)
      unchecked_value = options.delete(:unchecked_value)
      checked_value = '1' if unchecked_value && checked_value.blank?
      arguments = [options, checked_value, unchecked_value].compact
      send(type, method, *arguments)
    when :radio_button
      value = options.delete(:value)
      object_method = method
      method = "#{method.to_s.gsub(/\?$/, '')}_#{value.gsub(/\s/, '_')}".downcase
      send(type, object_method, value, options)
   when :date_select
      @template.capture_haml do
        @template.haml_tag(:div, input, class: 'group')
      end
    when :collection_select
      collection = options.delete(:collection)
      value_method = options.delete(:value_method)
      text_method = options.delete(:text_method)
      html_options = options.delete(:html) || {}
      send(type, method, collection, value_method, text_method, options, html_options)
    when :time_zone_select
      priority_zones = options.delete(:priority_zones)
      send(type, method, priority_zones, options)
    when :state_select
      country = options.delete(:country) || Carmen.default_country
      html_options = options.delete(:html) || {}
      send(type, method, country, options, html_options)
    when :country_select
      priority_countries = options.delete(:priority_countries)
      html_options = options.delete(:html) || {}
      send(type, method, priority_countries, options, html_options)
    else
      send(type, method, options)
  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, options={}, &block)
  input(method, options.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, options={}, &block)
  input(method, options.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, options={}, &block)
  input(method, options.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(options={}, &block)
  options[:class] = "#{options[:class]} section".strip

  @template.capture_haml do
    @template.haml_tag(:div, options) { 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
  options = args.extract_options!

  options[:collection] = args[0]
  options[:value_method] = args[1]
  options[:text_method] = args[2]
  input(method, options.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, options={}, &block)
  input(method, options.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, options={}, &block)
  @template.capture_haml do
    @template.haml_tag(:div, class: 'submit') do
      @template.haml_concat(super(value, options))
      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, options={}, &block)
  input(method, options.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, options={}, &block)
  input(method, options.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, options, &block)
  input(method, options.merge(type: :time_zone_select), &block)
end