Class: Sequel::Plugins::Forme::SequelInput
- Inherits:
-
Object
- Object
- Sequel::Plugins::Forme::SequelInput
- 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
-
#field ⇒ Object
readonly
The field/column name related to the receiver.
-
#form ⇒ Object
readonly
The form related to the receiver.
-
#obj ⇒ Object
readonly
The
Sequel::Model
object related to the receiver. -
#opts ⇒ Object
readonly
The options hash related to the receiver.
Instance Method Summary collapse
-
#initialize(obj, form, field, opts) ⇒ SequelInput
constructor
Set the
obj
,form
,field
, andopts
attributes. -
#input ⇒ Object
Determine which type of input to used based on the
field
.
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.
134 135 136 |
# File 'lib/sequel/plugins/forme.rb', line 134 def initialize(obj, form, field, opts) @obj, @form, @field, @opts = obj, form, field, opts end |
Instance Attribute Details
#field ⇒ Object (readonly)
The field/column name related to the receiver. The type of input created usually depends upon this field.
128 129 130 |
# File 'lib/sequel/plugins/forme.rb', line 128 def field @field end |
#form ⇒ Object (readonly)
The form related to the receiver.
124 125 126 |
# File 'lib/sequel/plugins/forme.rb', line 124 def form @form end |
#obj ⇒ Object (readonly)
The Sequel::Model
object related to the receiver.
121 122 123 |
# File 'lib/sequel/plugins/forme.rb', line 121 def obj @obj end |
#opts ⇒ Object (readonly)
The options hash related to the receiver.
131 132 133 |
# File 'lib/sequel/plugins/forme.rb', line 131 def opts @opts end |
Instance Method Details
#input ⇒ Object
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
.
145 146 147 148 149 150 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 |
# File 'lib/sequel/plugins/forme.rb', line 145 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 |