Class: Formize::Definition::Field

Inherits:
FormElement show all
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

Attributes inherited from FormElement

#children, #depend_on, #form, #html_id, #id, #options, #parent, #unique_name

Instance Method Summary collapse

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.

Raises:

  • (ArgumentError)


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, options={})
  super(form, parent, options)
  @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

#choicesObject (readonly)

Returns the value of attribute choices.



7
8
9
# File 'lib/formize/definition/field.rb', line 7

def choices
  @choices
end

#columnObject (readonly)

Returns the value of attribute column.



7
8
9
# File 'lib/formize/definition/field.rb', line 7

def column
  @column
end

#defaultObject (readonly)

Returns the value of attribute default.



7
8
9
# File 'lib/formize/definition/field.rb', line 7

def default
  @default
end

#field_idObject (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_optionsObject (readonly)

Returns the value of attribute html_options.



7
8
9
# File 'lib/formize/definition/field.rb', line 7

def html_options
  @html_options
end

#input_idObject (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_labelObject (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

#methodObject (readonly)

Returns the value of attribute method.



7
8
9
# File 'lib/formize/definition/field.rb', line 7

def method
  @method
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/formize/definition/field.rb', line 7

def name
  @name
end

#record_nameObject (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

#reflectionObject (readonly)

Returns the value of attribute reflection.



7
8
9
# File 'lib/formize/definition/field.rb', line 7

def reflection
  @reflection
end

#requiredObject (readonly)

Returns the value of attribute required.



7
8
9
# File 'lib/formize/definition/field.rb', line 7

def required
  @required
end

#search_attributesObject (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

#sourceObject (readonly)

Returns the value of attribute source.



7
8
9
# File 'lib/formize/definition/field.rb', line 7

def source
  @source
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/formize/definition/field.rb', line 7

def type
  @type
end