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::TRANSFORMERS, Forme::TRANSFORMER_TYPES, Forme::VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Forme

attr_classes, form, merge_classes, register_config, register_transformer, version

Constructor Details

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

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



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

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.



134
135
136
# File 'lib/sequel/plugins/forme.rb', line 134

def field
  @field
end

#formObject (readonly)

The form related to the receiver.



130
131
132
# File 'lib/sequel/plugins/forme.rb', line 130

def form
  @form
end

#objObject (readonly)

The Sequel::Model object related to the receiver.



127
128
129
# File 'lib/sequel/plugins/forme.rb', line 127

def obj
  @obj
end

#optsObject (readonly)

The options hash related to the receiver.



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

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.



151
152
153
154
155
156
157
158
159
160
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
# File 'lib/sequel/plugins/forme.rb', line 151

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[:id] = form.namespaced_id(field) unless opts.has_key?(:id)
    opts[:name] = form.namespaced_name(field) unless opts.has_key?(:name)
    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[:id] = form.namespaced_id(field) unless opts.has_key?(:id)
    opts[:name] = form.namespaced_name(field, opts[:multiple]) unless opts.has_key?(:name)
    handle_label(field)
    if respond_to?(meth, true)
      opts.delete(:type)
      send(meth, opts)
    else
      input_other(opts)
    end
  end
end