class Formulate::FormBuilder

Public Instance Methods

area(method, options={}, &block) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/formulate/form_builder.rb, line 120
def email(method, options={}, &block)
  input(method, options.merge(type: :email_field), &block)
end
errors() click to toggle source
# 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) click to toggle source
# 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={}) { |self| ... } click to toggle source
# 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) click to toggle source
# 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={}) { |send| ... } click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/formulate/form_builder.rb, line 140
def radio(method, options={}, &block)
  input(method, options.merge(type: :radio_button), &block)
end
section(options={}) { |self| ... } click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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={}) { |self| ... } click to toggle source
Calls superclass method
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/formulate/form_builder.rb, line 183
def time_zone(method, options, &block)
  input(method, options.merge(type: :time_zone_select), &block)
end

Private Instance Methods

errors_list(errors) click to toggle source
# File lib/formulate/form_builder.rb, line 208
def errors_list(errors)
  if errors.any?
    error_messages = "#{errors.to_sentence.capitalize}."
    @template.haml_tag(:p, error_messages, class: 'errors')
  end
end
errors_on(method) click to toggle source
# File lib/formulate/form_builder.rb, line 203
def errors_on(method)
  object.respond_to?(:errors) ? object.errors[method] : []
end