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.
Included modules
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
Public Class methods
Set the obj
, form
, field
, and opts
attributes.
# File lib/sequel/plugins/forme.rb 150 def initialize(obj, form, field, opts) 151 @obj, @form, @field, @opts = obj, form, field, opts 152 end
Public Instance methods
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
.
# File lib/sequel/plugins/forme.rb 161 def input 162 opts[:attr] = opts[:attr] ? opts[:attr].dup : {} 163 opts[:wrapper_attr] = opts[:wrapper_attr] ? opts[:wrapper_attr].dup : {} 164 handle_errors(field) 165 handle_validations(field) 166 167 type = opts[:type] 168 if !type && (sch = obj.db_schema[field]) 169 meth = :"input_#{sch[:type]}" 170 opts[:key] = field unless opts.has_key?(:key) 171 opts[:required] = true if !opts.has_key?(:required) && sch[:allow_null] == false && sch[:type] != :boolean 172 handle_label(field) 173 174 ::Forme.attr_classes(opts[:wrapper_attr], sch[:type]) 175 ::Forme.attr_classes(opts[:wrapper_attr], "required") if opts[:required] 176 177 if respond_to?(meth, true) 178 send(meth, sch) 179 else 180 input_other(sch) 181 end 182 elsif !type && (ref = obj.model.association_reflection(field)) 183 ::Forme.attr_classes(opts[:wrapper_attr], ref[:type]) 184 meth = :"association_#{ref[:type]}" 185 if respond_to?(meth, true) 186 send(meth, ref) 187 else 188 raise Error, "Association type #{ref[:type]} not currently handled for association #{ref[:name]}" 189 end 190 else 191 rt = obj.respond_to?(field) 192 raise(Error, "Unrecognized field used: #{field}") unless rt || type 193 meth = :"input_#{type}" 194 opts[:value] = nil unless rt || opts.has_key?(:value) 195 opts[:key] = field unless opts.has_key?(:key) 196 handle_label(field) 197 if respond_to?(meth, true) 198 opts.delete(:type) 199 send(meth, opts) 200 else 201 input_other(opts) 202 end 203 end 204 end