Class: HandyForm::Builder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/handy_form/builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.model_nameObject

Allows this all handy_form_for objects to behave like the normal form builder in partials.

e.g., this will use the “_form.html.erb” partial in the current view directory.

<%= handy_form_for(@user) do |f| %>
  <%= render :partial => f %>
<% end %>


55
56
57
# File 'lib/handy_form/builder.rb', line 55

def self.model_name
  @model_name ||= Struct.new(:partial_path).new("form")
end

Instance Method Details

#build_label(method, options) ⇒ Object Also known as: label_for

Build’s a label for the given ActiveModel method

When options = false, then no label is built When options is a string, then the value for the label is the string When options is a hash, hash will become the value for the label and all other options are passed into Rails’ label builder. Otherwise, the label is built off of the method.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/handy_form/builder.rb', line 70

def build_label(method, options)
  return "" if options == false

  text = nil
  label_options = {}

  text = method.to_s.humanize
  case options
  when String
    text = options
  when Hash
    if !options[:text].blank?
      text = options.delete(:text)
    end
    label_options = options
  end

  label(method, text, label_options)
end

#errors_for(method, options = {}) ⇒ Object



91
92
93
94
95
# File 'lib/handy_form/builder.rb', line 91

def errors_for(method, options = {})
  return "" unless object.errors[method.to_s].any?
  add_errors!(method, options)
  @template.error_tag(options[:error])
end

#extract_handy_options!(options) ⇒ Object



59
60
61
# File 'lib/handy_form/builder.rb', line 59

def extract_handy_options!(options)
  options.extract!(:error, :hint, :label, :wrapper, :input_first)
end

#hint(method, options = {}) ⇒ Object



97
98
99
# File 'lib/handy_form/builder.rb', line 97

def hint(method, options = {})
  @template.hint_tag(options)
end