Class: Lotus::Helpers::FormHelper::Form

Inherits:
Object
  • Object
show all
Defined in:
lib/lotus/helpers/form_helper.rb

Overview

Form object

Since:

  • 0.2.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, url, values = {}, attributes = {}) ⇒ Form

Initialize a form

It accepts a set of values that are used in combination with request params to autofill value attributes for fields.

The keys of this Hash, MUST correspond to the structure of the (nested) fields of the form.

For a given input where the name is `book`, Lotus will look for `:book` key in values.

If the current params have the same key, it will be PREFERRED over the given values.

For instance, if params.get('book.title') equals to "TDD" while values[:book].title returns "No test", the first will win.

Examples:

Pass A Value

# Given the following view

module Web::Views::Deliveries
  class Edit
    include Web::View

    def form
      Form.new(:delivery, routes.delivery_path(id: delivery.id),
      {delivery: delivery, customer: customer},
      {method: :patch})
    end
  end
end

# And the corresponding template:

<%=
  form_for form do
    date_field :delivered_on

    fields_for :customer do
      text_field :name

      fields_for :address do
        # ...
        text_field :city
      end
    end

    submit 'Update'
  end
%>

# It will render:
#
#  <form action="/deliveries/1" method="POST" accept-charset="utf-8">
#    <input type="hidden" name="_method" value="PATCH">
#
#    # Value taken from delivery.delivered_on
#    <input type="date" name="delivery[delivered_on]" id="delivery-delivered-on" value="2015-05-27">
#
#    # Value taken from customer.name
#    <input type="text" name="delivery[customer][name]" id="delivery-customer-name" value="Luca">
#
#    # Value taken from customer.address.city
#    <input type="text" name="delivery[customer][address][city]" id="delivery-customer-address-city" value="Rome">
#
#    <button type="submit">Update</button>
#  </form>

Parameters:

  • name (Symbol)

    the name of the form

  • url (String)

    the action of the form

  • values (Hash, NilClass) (defaults to: {})

    a Hash of values to be used to autofill value attributes for fields

  • attributes (Hash, NilClass) (defaults to: {})

    a Hash of attributes to pass to the form tag

Since:

  • 0.2.0


198
199
200
201
202
203
# File 'lib/lotus/helpers/form_helper.rb', line 198

def initialize(name, url, values = {}, attributes = {})
  @name       = name
  @url        = url
  @values     = values
  @attributes = attributes || {}
end

Instance Attribute Details

#nameSymbol (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the form name.

Returns:

  • (Symbol)

    the form name

Since:

  • 0.2.0


107
108
109
# File 'lib/lotus/helpers/form_helper.rb', line 107

def name
  @name
end

#urlString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the form action.

Returns:

  • (String)

    the form action

Since:

  • 0.2.0


113
114
115
# File 'lib/lotus/helpers/form_helper.rb', line 113

def url
  @url
end

#values::Hash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the form values.

Returns:

  • (::Hash)

    the form values

Since:

  • 0.2.0


119
120
121
# File 'lib/lotus/helpers/form_helper.rb', line 119

def values
  @values
end

Instance Method Details

#verbString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the method specified by the given attributes or fall back to the default value

Returns:

  • (String)

    the method for the action

See Also:

Since:

  • 0.2.0


214
215
216
# File 'lib/lotus/helpers/form_helper.rb', line 214

def verb
  @attributes.fetch(:method, DEFAULT_METHOD)
end