Module: Mack::View::FormBuilder

Included in:
DefaultFormBuilder
Defined in:
lib/mack/view/form_builder.rb

Overview

FormBuilders are a great way to encapsulate reusable form formatting. To help keep a consistent look and feel across the forms on your site, simply wrap them with a custom FormBuilder.

Example:

# app/form_builders/my_site_form_builder.rb:
class MySiteFormBuilder
  include Mack::View::FormBuilder

  partial :password_field, 'form_partials/password_field'

  def text_field(*args)
    "<h1>#{element(:text_field, *args)}</h1>"
  end

  def all(sym, *args)
    "<p>#{element(sym, *args)}</p>"
  end
end

# form_partials/_password_field.html.erb:
<h2><%= form_element %></h2>

# some_view.html.erb:
<% my_site_form('/foo', :method => :post) do |f| %>
  <%= f.text_field :username %> # => '<h1><input id="username" name="username" type="text" /></h1>'
  <%= f.password_field :password %> # => '<h2><input id="password" name="password" type="password" /></h2>'
  <%= f.hidden_field :token, :value => '123' %> # => '<p><input id="token" name="token" type="hidden" value="123" /></p>'
<% end %>

# some_view.html.erb (alternative):
<% form('/foo', :method => :post, :builder => MySiteFormBuilder.new(self)) do |f| %>
  <%= f.text_field :username %> # => '<h1><input id="username" name="username" type="text" /></h1>'
  <%= f.password_field :password %> # => '<h2><input id="password" name="password" type="password" /></h2>'
  <%= f.hidden_field :token, :value => '123' %> # => '<p><input id="token" name="token" type="hidden" value="123" /></p>'
<% end %>

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object

:nodoc:



57
58
59
# File 'lib/mack/view/form_builder.rb', line 57

def method_missing(sym, *args) # :nodoc:
  self.all(sym, *args)
end

Class Method Details

.included(base) ⇒ Object

Creates a method named after the class that’s including Mack::View::FormBuilder

Example:

class SweetFormBuilder
  include Mack::View::FormBuilder
end

This creates the following method available in views:

sweet_form('/someurl', :method => :post) do |f|
  # ...
end

This could also have been written like this:

form('/someurl', :method => :post, :builder => SweetFormBuilder.new(self)) do |f|
  # ...
end

As you can see the helper method is a lot nicer to use. :)



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/mack/view/form_builder.rb', line 98

def self.included(base)
  klass = base.to_s
  klass = klass.gsub('Builder', '').gsub('Form', '')
  mod = Module.new do
    eval %{
      def #{klass.methodize}_form(action, options = {}, &block)
        builder = #{base}.new(self)
        concat("\#{builder.form_start}\n", block.binding)
        self.form(action, options.merge(:builder => builder), &block)
        concat("\n\#{builder.form_end}\n", block.binding)
      end
    }
  end # Module.new

  Mack::Rendering::ViewTemplate.class_eval do
    include mod
  end
  
  base.extend Mack::View::FormBuilder::ClassMethods
end

Instance Method Details

#all(sym, *args) ⇒ Object

This is the catch all method for form elements that aren’t overridden. Override and make your own.



53
54
55
# File 'lib/mack/view/form_builder.rb', line 53

def all(sym, *args)
  self.view.send(sym, *args)
end

#element(name, *args) ⇒ Object

Returns the actual form element from the Mack::ViewHelpers::FormHelper module. This should be used in any method you override in a FormBuilder.

Example:

element(:text_field, :username, :value => 'fubar') # => <input id="username" name="username" type="text" value="fubar" />


76
77
78
# File 'lib/mack/view/form_builder.rb', line 76

def element(name, *args)
  self.view.send(name, *args)
end

#form_endObject

Override form_start and form_ends to bookmark your forms.



67
68
69
# File 'lib/mack/view/form_builder.rb', line 67

def form_end
  ''
end

#form_startObject

Override form_start and form_ends to bookmark your forms.



62
63
64
# File 'lib/mack/view/form_builder.rb', line 62

def form_start
  ''
end

#initialize(view) ⇒ Object

Requires a Mack::Rendering::ViewTemplate instance.



42
43
44
# File 'lib/mack/view/form_builder.rb', line 42

def initialize(view)
  @_view = view
end

#viewObject

Returns the Mack::Rendering::ViewTemplate instance.



47
48
49
# File 'lib/mack/view/form_builder.rb', line 47

def view
  @_view
end