Class: Wallaby::FormBuilder

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

Overview

Custom form builder to add more helper functions

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (protected)

Delegate missing method to ‘@template`



46
47
48
49
50
51
52
53
# File 'lib/forms/wallaby/form_builder.rb', line 46

def method_missing(method, *args, &block)
  return super unless @template.respond_to? method

  # Delegate the method so that we don't come in here the next time
  # when same method is called
  self.class.delegate method, to: :@template
  @template.try method, *args, &block
end

Instance Method Details

#error_class(field_name) ⇒ String

Return error class if there is error

Parameters:

  • field_name (String/Symbol)

Returns:

  • (String)


9
10
11
# File 'lib/forms/wallaby/form_builder.rb', line 9

def error_class(field_name)
  'has-error' if object.errors[field_name].present?
end

#error_messages(field_name) ⇒ String

Build up the HTML for displaying error messages

Parameters:

  • field_name (String/Symbol)

Returns:

  • (String)

    HTML



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/forms/wallaby/form_builder.rb', line 16

def error_messages(field_name)
  errors = Array object.errors[field_name]
  return if errors.blank?

   :ul, class: 'errors' do
    errors.each do |message|
      concat  :li, (
        :small, raw(message)
      )
    end
  end
end

#label(method, text = nil, options = {}, &block) ⇒ Object

Extend label to accept proc type ‘text` argument

See Also:

  • ActionView::Helpers::FormBuilder#label


31
32
33
34
# File 'lib/forms/wallaby/form_builder.rb', line 31

def label(method, text = nil, options = {}, &block)
  text = instance_exec(&text) if text.is_a?(Proc)
  super
end

#select(method, choices = nil, options = {}, html_options = {}, &block) ⇒ Object

Extend select to accept proc type ‘choices` argument

See Also:

  • ActionView::Helpers::FormBuilder#select


38
39
40
41
# File 'lib/forms/wallaby/form_builder.rb', line 38

def select(method, choices = nil, options = {}, html_options = {}, &block)
  choices = instance_exec(&choices) if choices.is_a?(Proc)
  super
end