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, line 128
def initialize(obj, form, field, opts)
  @obj, @form, @field, @opts = obj, form, field, opts
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, line 139
def input
  opts[:attr] = opts[:attr] ? opts[:attr].dup : {}
  opts[:wrapper_attr] = opts[:wrapper_attr] ? opts[:wrapper_attr].dup : {}
  handle_errors(field)
  handle_validations(field)

  type = opts[:type]
  if !type && (sch = obj.db_schema[field])
    meth = :"input_#{sch[:type]}"
    opts[:key] = field unless opts.has_key?(:key)
    opts[:required] = true if !opts.has_key?(:required) && sch[:allow_null] == false && sch[:type] != :boolean
    handle_label(field)

    ::Forme.attr_classes(opts[:wrapper_attr], sch[:type])
    ::Forme.attr_classes(opts[:wrapper_attr], "required") if opts[:required]

    if respond_to?(meth, true)
      send(meth, sch)
    else
      input_other(sch)
    end
  elsif !type && (ref = obj.model.association_reflection(field))
    ::Forme.attr_classes(opts[:wrapper_attr], ref[:type])
    meth = :"association_#{ref[:type]}"
    if respond_to?(meth, true)
      send(meth, ref)
    else
      raise Error, "Association type #{ref[:type]} not currently handled for association #{ref[:name]}"
    end
  else
    rt = obj.respond_to?(field)
    raise(Error, "Unrecognized field used: #{field}") unless rt || type
    meth = :"input_#{type}"
    opts[:value] = nil unless rt || opts.has_key?(:value)
    opts[:key] = field unless opts.has_key?(:key)
    handle_label(field)
    if respond_to?(meth, true)
      opts.delete(:type)
      send(meth, opts)
    else
      input_other(opts)
    end
  end
end