Class: Skyline::FormBuilder

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

Overview

Provides error wrapping and some extra helper methods to working with labels and translations easier.

Apart from adding new helper methods, this class overwrites the standard *_field and *_select helpers. If one of the fields has an error, the validation errors for that method are added just after the field. See #wrap_with_error for more information on what get’s added.

Instance Method Summary collapse

Instance Method Details

#dom_id(attribute, options = {}) ⇒ String

The ID that a field for a certain field would get.



162
163
164
# File 'lib/skyline/form_builder.rb', line 162

def dom_id(attribute,options={})
  CustomInstanceTag.new(@object_name, attribute, @template, objectify_options(options)).to_id(@options)
end

#fieldset_errors(attribute) ⇒ String?

Generates a list of errors. Each error is wrapped in a

<div class="error">...</div>


146
147
148
149
150
151
152
153
154
155
156
# File 'lib/skyline/form_builder.rb', line 146

def fieldset_errors(attribute)
  return unless @object.errors[attribute]
  out = []
  if errs = @object.errors[attribute]
    errs = [errs] if self.single_error_message?(errs)
    out = errs.map do |err|        
      @template.("div",err,:class => "error")
    end
  end
  out.join("\n")
end

#has_error?(attribute) ⇒ Boolean

Does the object have any errors set on an attribute.



90
91
92
# File 'lib/skyline/form_builder.rb', line 90

def has_error?(attribute)
  @object.errors[attribute].present?
end

#label(method, text = nil, options = {}) ⇒ String

An improved version of the standard label helper it will append the class “invalid”

See Also:

  • ActionView::Helpers::FormBuilder#label


43
44
45
46
47
48
49
# File 'lib/skyline/form_builder.rb', line 43

def label(method, text = nil, options = {})
  if @object.errors[method]
    super(method,text,options.merge(:class => "invalid #{options[:class]}".strip))
  else
    super
  end    
end

#label_with_text(method, options = {}) ⇒ String

Same as label but automatically translates the field name

See Also:



56
57
58
# File 'lib/skyline/form_builder.rb', line 56

def label_with_text(method, options = {})
  self.label(method, self.t(method), options)
end

#object_name_with_indexString

Add the index of the current scope to the object_name if an index is used.



97
98
99
100
101
102
103
# File 'lib/skyline/form_builder.rb', line 97

def object_name_with_index
  if options[:index].present?
    self.object_name + "[#{options[:index]}]"
  else
    self.object_name
  end
end

#positioning_fieldObject

Special field to handle automated reordering of elements ( with help of NestedAttributesPositioning module)

Examples:

Usage:

<% article_form.fields_for "sections_attributes", section, :index => 5 do |s| %>
  <%= s.hidden_field :id unless s.object.new_record? %>
  <%= s.hidden_field :_destroy, :class => "delete" %>
  <%= s.positioning_field %>
  ...
<% end %>

Results in the following fields:

<input type="hidden" name="article[sections_attributes][5][id]" id="article_sections_attributes_5_id"      value="2211"/>
<input type="hidden" name="article[sections_attributes][5][_destroy]" id="article_sections_attributes_5__destroy" class="delete"/>
<input type="hidden" name="article[sections_position][]"  id="article_sections_position_5" value="5"/>


76
77
78
79
80
81
82
83
84
# File 'lib/skyline/form_builder.rb', line 76

def positioning_field
  name = self.object_name.sub(/\[([^\]]*)_attributes\]$/, "[\\1_position]") + "[]"
  value = options[:index]    
  raise "No options[:index] defined, need one to use for ordering correctly." unless value
  
  tag_id = name.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "") + value.to_s

  @template.hidden_field_tag name, value, :id => tag_id
end

#t(attribute, options = {}) ⇒ String

Translation for an attribute of that’s used in this formbuilder. Uses ActiveRecord::Base#human_attribute_name



172
173
174
# File 'lib/skyline/form_builder.rb', line 172

def t(attribute, options = {})
  self.object.class.human_attribute_name(attribute.to_s, options)
end

#wrap_with_error(method, *parms) {|parameters| ... } ⇒ String

Wrap an input element with errors, also appends “invalid” to the helpers class. See #fieldset_errors on how the errors are added.

Yields:

  • (parameters)

    The return value of the block will be wrapped with the error

Yield Parameters:

  • parameters (Hash)

    The parameters to pass to the block (is the same as the parms of the method)



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/skyline/form_builder.rb', line 120

def wrap_with_error(method,*parms,&block)
  options = parms.extract_options!
  method_options = options.except(:text_suffix)
  
  if @object.errors[method]
    method_options[:class] ||= ""
    method_options[:class] << " invalid"
  end
  
  parms << method_options if method_options.any?    
  html = yield(parms)
  html << options[:text_suffix] if options[:text_suffix]

  if @object.errors[method]
    html + fieldset_errors(method)
  else
    html
  end
  
end