class Sequel::Plugins::Forme::SequelInput

  1. lib/sequel/plugins/forme.rb
Superclass: Object

Helper class for dealing with Forme/Sequel integration. One instance is created for each call to Forme::Form#input for forms associated with Sequel::Model objects.

Methods

Public Class

  1. new

Public Instance

  1. field
  2. form
  3. input
  4. obj
  5. opts

Included modules

  1. ::Forme

Constants

FORME_NAME_METHODS = [:forme_name, :name, :title, :number]  

The name methods that will be tried, in order, to get the text to use for the options in the select input created for associations.

Attributes

field [R]

The field/column name related to the receiver. The type of input created usually depends upon this field.

form [R]

The form related to the receiver.

obj [R]

The Sequel::Model object related to the receiver.

opts [R]

The options hash related to the receiver.

Public Class methods

new (obj, form, field, opts)

Set the obj, form, field, and opts attributes.

[show source]
    # File lib/sequel/plugins/forme.rb
129 def initialize(obj, form, field, opts)
130   @obj, @form, @field, @opts = obj, form, field, opts
131 end

Public Instance methods

input ()

Determine which type of input to used based on the field. If the field is a column, use the column's type to determine an appropriate field type. If the field is an association, use either a regular or multiple select input (or multiple radios or checkboxes if the related :as option is used). If it's not a column or association, but the object responds to field, create a text input. Otherwise, raise an Error.

[show source]
    # File lib/sequel/plugins/forme.rb
140 def input
141   opts[:attr] = opts[:attr] ? opts[:attr].dup : {}
142   opts[:wrapper_attr] = opts[:wrapper_attr] ? opts[:wrapper_attr].dup : {}
143   handle_errors(field)
144   handle_validations(field)
145 
146   type = opts[:type]
147   if !type && (sch = obj.db_schema[field])
148     meth = :"input_#{sch[:type]}"
149     opts[:key] = field unless opts.has_key?(:key)
150     opts[:required] = true if !opts.has_key?(:required) && sch[:allow_null] == false && sch[:type] != :boolean
151     handle_label(field)
152 
153     ::Forme.attr_classes(opts[:wrapper_attr], sch[:type])
154     ::Forme.attr_classes(opts[:wrapper_attr], "required") if opts[:required]
155 
156     if respond_to?(meth, true)
157       send(meth, sch)
158     else
159       input_other(sch)
160     end
161   elsif !type && (ref = obj.model.association_reflection(field))
162     ::Forme.attr_classes(opts[:wrapper_attr], ref[:type])
163     meth = :"association_#{ref[:type]}"
164     if respond_to?(meth, true)
165       send(meth, ref)
166     else
167       raise Error, "Association type #{ref[:type]} not currently handled for association #{ref[:name]}"
168     end
169   else
170     rt = obj.respond_to?(field)
171     raise(Error, "Unrecognized field used: #{field}") unless rt || type
172     meth = :"input_#{type}"
173     opts[:value] = nil unless rt || opts.has_key?(:value)
174     opts[:key] = field unless opts.has_key?(:key)
175     handle_label(field)
176     if respond_to?(meth, true)
177       opts.delete(:type)
178       send(meth, opts)
179     else
180       input_other(opts)
181     end
182   end
183 end