Class: Sequel::Plugins::Forme::SequelInput

Inherits:
Object
  • Object
show all
Includes:
Forme
Defined in:
lib/sequel/plugins/forme.rb

Overview

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.

Constant Summary collapse

FORME_NAME_METHODS =

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.

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

Constants included from Forme

Forme::CONFIGURATIONS, Forme::MAJOR, Forme::MINOR, Forme::SHARED_WRAPPERS, Forme::TINY, Forme::TRANSFORMERS, Forme::TRANSFORMER_TYPES, Forme::VERSION, Forme::VERSION_NUMBER

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Forme

attr_classes, attr_classes_after, form, merge_classes, raw, register_config, register_transformer, transform, transformer, version

Constructor Details

#initialize(obj, form, field, opts) ⇒ SequelInput

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



150
151
152
# File 'lib/sequel/plugins/forme.rb', line 150

def initialize(obj, form, field, opts)
  @obj, @form, @field, @opts = obj, form, field, opts
end

Instance Attribute Details

#fieldObject (readonly)

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



144
145
146
# File 'lib/sequel/plugins/forme.rb', line 144

def field
  @field
end

#formObject (readonly)

The form related to the receiver.



140
141
142
# File 'lib/sequel/plugins/forme.rb', line 140

def form
  @form
end

#objObject (readonly)

The Sequel::Model object related to the receiver.



137
138
139
# File 'lib/sequel/plugins/forme.rb', line 137

def obj
  @obj
end

#optsObject (readonly)

The options hash related to the receiver.



147
148
149
# File 'lib/sequel/plugins/forme.rb', line 147

def opts
  @opts
end

Instance Method Details

#inputObject

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.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/sequel/plugins/forme.rb', line 161

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