Class: Motr::Forms::Base

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/motr/forms/base.rb

Overview

Base class for creating form builders

Direct Known Subclasses

Builder

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Object) current_field_type

Tracks the field currently being "processed"



16
17
18
# File 'lib/motr/forms/base.rb', line 16

def current_field_type
  @current_field_type
end

- (Object) field_order

Tracks the order in which fields are used in the form, this allows the easy rebuilding of the submitted form data when submitted since normally the hash isn't ordered.



14
15
16
# File 'lib/motr/forms/base.rb', line 14

def field_order
  @field_order
end

- (Object) template

Access the template object



11
12
13
# File 'lib/motr/forms/base.rb', line 11

def template
  @template
end

Instance Method Details

- (Boolean) attribute_required?(attribute) (protected)

Checks to see if a particular attribute is required, if so, a "required" attribute is added to the field.

Parameters:

  • attribute (Symbol)

    The attribute to check against validators

Returns:

  • (Boolean)


40
41
42
# File 'lib/motr/forms/base.rb', line 40

def attribute_required?(attribute)
  validates_presence?(attribute) || validates_inclusion?(attribute)
end

- (Object) content_tag(tag, content, options = {}, escape = true, &block) (protected)

Convenience method to use the content_tag method from our template



46
47
48
# File 'lib/motr/forms/base.rb', line 46

def (tag, content, options = {}, escape = true, &block) #:nodoc:
  @template.(tag, content, options, escape, &block)
end

- (Boolean) errors_on_attribute?(method) (protected)

Checks to see if there are errors for the particular method or attribute

Parameters:

  • method (Symbol)

    The method/attribute to check

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/motr/forms/base.rb', line 57

def errors_on_attribute?(method)
  return false if @object.nil?
  !(@object.errors.empty? || !@object.errors[method.to_sym].present? || [@object.errors[method.to_sym]].flatten.empty?)
end

- (Object) fields_for(record_or_name_or_array, *args, &block)

Overrides fields_for to make sure the form builder is set properly



21
22
23
24
25
26
27
28
# File 'lib/motr/forms/base.rb', line 21

def fields_for(record_or_name_or_array, *args, &block) #:nodoc:
  opts = args.extract_options!
  opts[:builder] ||= Motr::Forms.default_builder
  args.push(opts)
  Motr::Forms.with_custom_error_proc do
    super(record_or_name_or_array, *args, &block)
  end
end

- (Boolean) options_require_validation?(options) (protected)

Checks a passed validator to see if it is required 'borrowed' from Formtastic by Justin French (see github.com/justinfrench/formtastic)

Parameters:

  • options (Hash)

    Validator options

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/motr/forms/base.rb', line 69

def options_require_validation?(options)
  
  allow_blank = options[:allow_blank]
  return !allow_blank unless allow_blank.nil?
  if_condition = !options[:if].nil?
  condition = if_condition ? options[:if] : options[:unless]

  condition = if condition.respond_to?(:call)
    condition.call(@object)
  elsif condition.is_a?(::Symbol) && @object.respond_to?(condition)
    @object.send(condition)
  else
    condition
  end
  if_condition ? !!condition : !condition
end

- (String) render_field_as_custom(helper_method, method, *args) (protected)

Wrapper method used by all form fields to customize the output

Parameters:

  • helper_method (Symbol)

    Original Rails helper method name

  • method (Symbol)

    Object method / symbol used for the element

  • args (Array)

    Array of original arguments passed to the helper

Returns:

  • (String)

    Rendered HTML tag for the element



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/motr/forms/base.rb', line 107

def render_field_as_custom(helper_method, method, *args)

  @current_field_type = helper_method
  
  options = args.extract_options!
  (@field_order ||= []) << method
  
  # Add an error class to the field if it has errors
  #
  if errors_on_attribute?(method)
    klasses = (options.delete(:class) || "").split(" ")
    klasses << Motr::Forms.error_class
    options[:class] = klasses.join(" ")
  end
  
  # Add a required attribute to the field if it is required
  # Skip if false was passed as the required option
  # 
  options[:required] = "required" if attribute_required?(method) && options.delete(:required).to_s !=  'false'
  options['data-validates-uniqueness'] = "true" if validates_uniqueness?(method)
  
  result   = send(:_super_#{helper_method}", method, *(args << options))
  messages = @object.nil? ? [] : @object.errors[method]
  render_field_with_errors(method, result, messages)
  
end

- (Object) render_field_with_errors(method, html_tag, messages) (protected)

Renders the passed html_tag with the custom error_template

Parameters:

  • method (Symbol)

    The method/attribute to check for errors

  • html_tag (Object, String)

    Instance of an input tag or a string

  • messages (Array)

    An array of all error messages to be added to the template

See Also:

  • Motr::Forms#error_template


143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/motr/forms/base.rb', line 143

def render_field_with_errors(method, html_tag, messages)
  
  return html_tag unless errors_on_attribute?(method)
  
  error_class         = Motr::Forms.error_class
  message_error_class = Motr::Forms.message_error_class
  render_binding      = binding
  renderer            = ERB.new(Motr::Forms.error_template)        
  
  renderer.result(render_binding).to_s.html_safe
  
end

- (Boolean) required_by_option?(options) (protected)

Checks an options hash to determine if a required method/attribute was overridden manually

Parameters:

  • options (Hash)

    The options hash to check

Returns:

  • (Boolean)


92
93
94
95
# File 'lib/motr/forms/base.rb', line 92

def required_by_option?(options)
  req = (options.is_a?(Hash) ? options.stringify_keys[:required] : options)
  !(req.to_s === 'false' || req.nil?)
end

- (Boolean) validates_inclusion?(attribute) (protected)

Checks for inclusion validation on a particular attribute

Parameters:

  • attribute (Symbol)

    The attribute to check

Returns:

  • (Boolean)


203
204
205
# File 'lib/motr/forms/base.rb', line 203

def validates_inclusion?(attribute)
  validator_of_type_exists?(validators_for(attribute), :inclusion)
end

- (Boolean) validates_presence?(attribute) (protected)

Checks for a presence validation on a particular attribute

Parameters:

  • attribute (Symbol)

    The attribute to check

Returns:

  • (Boolean)


185
186
187
# File 'lib/motr/forms/base.rb', line 185

def validates_presence?(attribute)
  validator_of_type_exists?(validators_for(attribute), :presence)
end

- (Boolean) validates_uniqueness?(attribute) (protected)

Checks for a uniqueness validation on a particular attribute

Parameters:

  • attribute (Symbol)

    The attribute to check

Returns:

  • (Boolean)


194
195
196
# File 'lib/motr/forms/base.rb', line 194

def validates_uniqueness?(attribute)
  validator_of_type_exists?(validators_for(attribute), :uniqueness, false)
end

- (Object) validators_for(attribute) (protected)

Compiles an array of all validators for a particular attribute

Parameters:

  • attribtue (Symbol)

    The attribute to check



161
162
163
164
165
166
167
168
169
# File 'lib/motr/forms/base.rb', line 161

def validators_for(attribute)
  
  return [] if @object.nil?
  return [] unless @object.class.respond_to?(:validators_on)
  
  attribute  = attribute.to_s.sub(/_id$/, '').to_sym
  @object.class.validators_on(attribute).uniq
  
end

- (Boolean) validators_for?(attribute) (protected)

Convenience method to see if a particular attribute has validators

Parameters:

  • attribute (Symbol)

    The attribute to check

Returns:

  • (Boolean)


176
177
178
# File 'lib/motr/forms/base.rb', line 176

def validators_for?(attribute)
  !validators_for(attribute).empty?
end