Class: Formize::Definition::Field
- Inherits:
-
FormElement
- Object
- FormElement
- Formize::Definition::Field
- Defined in:
- lib/formize/definition/field.rb
Overview
Represents the field element
Constant Summary collapse
- TYPES =
[:check_box, :choice, :date, :datetime, :label, :numeric, :password, :mono_choice, :string, :text_area].freeze
Instance Attribute Summary collapse
-
#choices ⇒ Object
readonly
Returns the value of attribute choices.
-
#column ⇒ Object
readonly
Returns the value of attribute column.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#field_id ⇒ Object
readonly
Returns the value of attribute field_id.
-
#html_options ⇒ Object
readonly
Returns the value of attribute html_options.
-
#input_id ⇒ Object
readonly
Returns the value of attribute input_id.
-
#item_label ⇒ Object
readonly
Returns the value of attribute item_label.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#record_name ⇒ Object
readonly
Returns the value of attribute record_name.
-
#reflection ⇒ Object
readonly
Returns the value of attribute reflection.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#search_attributes ⇒ Object
readonly
Returns the value of attribute search_attributes.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Attributes inherited from FormElement
#children, #depend_on, #form, #html_id, #id, #options, #parent, #unique_name
Instance Method Summary collapse
-
#initialize(form, parent, name, options = {}) ⇒ Field
constructor
A new instance of Field.
Methods inherited from FormElement
#all_elements, #all_fields, #arguments, #dependeds, #dependents, #dependents_on, #hidden_if, #mono_choices, #prototype, #shown_if
Constructor Details
#initialize(form, parent, name, options = {}) ⇒ Field
Returns a new instance of Field.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/formize/definition/field.rb', line 11 def initialize(form, parent, name, ={}) super(form, parent, ) @name = name.to_s @column = form.model.columns_hash[@name] @record_name = form.record_name @method = @name unless @options[:default].nil? @default = @options.delete(:default) # (@options[:default].is_a?(String) ? Formize::Generator::Code.new(@options[:default]) : @options[:default]) end @html_options = @options.delete(:html_options)||{} @depend_on = @options.delete(:depend_on) raise ArgumentError.new("A depended element must defined before its dependencies (#{@depended.inspect})") if !@depend_on.blank? and form.all_fields[@depend_on].nil? if type = @options.delete(:as) raise ArgumentError.new("Unknown field type (got #{@options[:as].inspect}, expects #{TYPES.join(', ')})") unless TYPES.include? type @type = type else @type = :password if @name.to_s.match /password/ if @choices = @options.delete(:choices) if @choices.is_a? Array @type = :choice elsif [Symbol, Hash].include? @choices.class @type = :mono_choice @reflection = form.model.reflections[@method.to_sym] @source = @options.delete(:source) # || @reflection.class_name @is_method = true if @options[:new] @method_name = self.form.unique_name + "_inf_" + @name @method = @reflection.send(Formize.foreign_key) unless @item_label = @options.delete(:item_label) model = @reflection.class_name.constantize available_methods = (model.columns_hash.keys+model.instance_methods).collect{|x| x.to_s} @item_label = [:label, :name, :title, :code, :number, :inspect].detect{|x| available_methods.include?(x.to_s)} end @search_attributes = @options[:search] || @reflection.class_name.constantize.content_columns.select{|c| c.type != :boolean and ![:created_at, :updated_at, :lock_version].include?(c.name.to_sym)}.collect{|c| c.name.to_sym} else raise ArgumentError.new("Option :choices must be Array, Symbol or Hash (got #{@choices.class.name})") end end if column @type = :check_box if column.type == :boolean @type = :date if column.type == :date @type = :datetime if column.type==:datetime or column.type==:timestamp @type = :numeric if [:integer, :float, :decimal].include? column.type @type = :text_area if column.type == :text end @type = :label if @form.model.readonly_attributes.include? @record_name @type ||= :string end @required = false @required = !@column.null if @column @required = true if @options.delete(:required).is_a?(TrueClass) @input_id = form.model.name.underscore << '_' << method.to_s @field_id = "ff" << Time.now.to_i.to_s(36) << rand.to_s[2..-1].to_i.to_s(36) end |
Instance Attribute Details
#choices ⇒ Object (readonly)
Returns the value of attribute choices.
7 8 9 |
# File 'lib/formize/definition/field.rb', line 7 def choices @choices end |
#column ⇒ Object (readonly)
Returns the value of attribute column.
7 8 9 |
# File 'lib/formize/definition/field.rb', line 7 def column @column end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
7 8 9 |
# File 'lib/formize/definition/field.rb', line 7 def default @default end |
#field_id ⇒ Object (readonly)
Returns the value of attribute field_id.
7 8 9 |
# File 'lib/formize/definition/field.rb', line 7 def field_id @field_id end |
#html_options ⇒ Object (readonly)
Returns the value of attribute html_options.
7 8 9 |
# File 'lib/formize/definition/field.rb', line 7 def @html_options end |
#input_id ⇒ Object (readonly)
Returns the value of attribute input_id.
7 8 9 |
# File 'lib/formize/definition/field.rb', line 7 def input_id @input_id end |
#item_label ⇒ Object (readonly)
Returns the value of attribute item_label.
7 8 9 |
# File 'lib/formize/definition/field.rb', line 7 def item_label @item_label end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
7 8 9 |
# File 'lib/formize/definition/field.rb', line 7 def method @method end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/formize/definition/field.rb', line 7 def name @name end |
#record_name ⇒ Object (readonly)
Returns the value of attribute record_name.
7 8 9 |
# File 'lib/formize/definition/field.rb', line 7 def record_name @record_name end |
#reflection ⇒ Object (readonly)
Returns the value of attribute reflection.
7 8 9 |
# File 'lib/formize/definition/field.rb', line 7 def reflection @reflection end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
7 8 9 |
# File 'lib/formize/definition/field.rb', line 7 def required @required end |
#search_attributes ⇒ Object (readonly)
Returns the value of attribute search_attributes.
7 8 9 |
# File 'lib/formize/definition/field.rb', line 7 def search_attributes @search_attributes end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
7 8 9 |
# File 'lib/formize/definition/field.rb', line 7 def source @source end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
7 8 9 |
# File 'lib/formize/definition/field.rb', line 7 def type @type end |