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 %>


53
54
55
# File 'lib/handy_form/builder.rb', line 53

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.



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

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



89
90
91
92
93
# File 'lib/handy_form/builder.rb', line 89

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



57
58
59
# File 'lib/handy_form/builder.rb', line 57

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

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



95
96
97
# File 'lib/handy_form/builder.rb', line 95

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