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 134 def initialize(obj, form, field, opts) 135 @obj, @form, @field, @opts = obj, form, field, opts 136 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 145 def input 146 opts[:attr] = opts[:attr] ? opts[:attr].dup : {} 147 opts[:wrapper_attr] = opts[:wrapper_attr] ? opts[:wrapper_attr].dup : {} 148 handle_errors(field) 149 handle_validations(field) 150 151 type = opts[:type] 152 if !type && (sch = obj.db_schema[field]) 153 meth = :"input_#{sch[:type]}" 154 opts[:key] = field unless opts.has_key?(:key) 155 opts[:required] = true if !opts.has_key?(:required) && sch[:allow_null] == false && sch[:type] != :boolean 156 handle_label(field) 157 158 ::Forme.attr_classes(opts[:wrapper_attr], sch[:type]) 159 ::Forme.attr_classes(opts[:wrapper_attr], "required") if opts[:required] 160 161 if respond_to?(meth, true) 162 send(meth, sch) 163 else 164 input_other(sch) 165 end 166 elsif !type && (ref = obj.model.association_reflection(field)) 167 ::Forme.attr_classes(opts[:wrapper_attr], ref[:type]) 168 meth = :"association_#{ref[:type]}" 169 if respond_to?(meth, true) 170 send(meth, ref) 171 else 172 raise Error, "Association type #{ref[:type]} not currently handled for association #{ref[:name]}" 173 end 174 else 175 rt = obj.respond_to?(field) 176 raise(Error, "Unrecognized field used: #{field}") unless rt || type 177 meth = :"input_#{type}" 178 opts[:value] = nil unless rt || opts.has_key?(:value) 179 opts[:key] = field unless opts.has_key?(:key) 180 handle_label(field) 181 if respond_to?(meth, true) 182 opts.delete(:type) 183 send(meth, opts) 184 else 185 input_other(opts) 186 end 187 end 188 end