Class: Basepack::Forms::Fields::Base

Inherits:
Object
  • Object
show all
Includes:
Delegation, Renderable
Defined in:
lib/basepack/forms/fields/base.rb

Constant Summary collapse

ASSOC_TYPES =
{
  :belongs_to_association              => true,
  :has_and_belongs_to_many_association => true,
  :has_many_association                => true,
  :has_one_association                 => true,
  :polymorphic_association             => true,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, form, delegate_or_attributes = nil) ⇒ Base

Returns a new instance of Base.



31
32
33
34
35
# File 'lib/basepack/forms/fields/base.rb', line 31

def initialize(name, form, delegate_or_attributes = nil)
  @name = name
  @form = form
  update_attributes(delegate_or_attributes)
end

Instance Attribute Details

#delegateObject (readonly)

Returns the value of attribute delegate.



29
30
31
# File 'lib/basepack/forms/fields/base.rb', line 29

def delegate
  @delegate
end

#formObject (readonly)

Returns the value of attribute form.



28
29
30
# File 'lib/basepack/forms/fields/base.rb', line 28

def form
  @form
end

#nameObject (readonly)

Returns the value of attribute name.



28
29
30
# File 'lib/basepack/forms/fields/base.rb', line 28

def name
  @name
end

Instance Method Details

#association?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/basepack/forms/fields/base.rb', line 79

def association?
  !!ASSOC_TYPES[type]
end

#bulk_editable?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/basepack/forms/fields/base.rb', line 152

def bulk_editable?
  true
end

#configure_nested_form(&block) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/basepack/forms/fields/base.rb', line 119

def configure_nested_form(&block)
  if block
    @configure_nested_form = block
  else
    @configure_nested_form
  end
end

#copy(attributes = nil) ⇒ Object



37
38
39
40
41
# File 'lib/basepack/forms/fields/base.rb', line 37

def copy(attributes = nil)
  field = self.class.new(name, form, self)
  field.update_attributes(attributes) if attributes.is_a? Hash
  field
end

#enum_optionsObject



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/basepack/forms/fields/base.rb', line 99

def enum_options
  if type == :enum # TODO - into own class
    if form.resource_class.respond_to? :enumerized_attributes
      # enumerize
      values = form.resource_class.enumerized_attributes[method_name].try(:values)
      values.map {|val| [val.value, val.text]}
    else
      enum.map {|a| a.reverse }
    end
  end
end

#inverse_of_nested_in?Boolean

Returns:

  • (Boolean)


139
140
141
142
# File 'lib/basepack/forms/fields/base.rb', line 139

def inverse_of_nested_in?
  (nested_in = form.nested_in) and nested_in.name == inverse_of and
    nested_in.abstract_model.model == associated_model_config.abstract_model.model
end

#nested_labelObject



144
145
146
# File 'lib/basepack/forms/fields/base.rb', line 144

def nested_label
  form.nested_in ? "#{form.nested_in.nested_label}: #{label}" : label
end

#nformObject



131
132
133
134
135
136
137
# File 'lib/basepack/forms/fields/base.rb', line 131

def nform
  if association? and !polymorphic?
    @nform ||= form.new_form(associated_model_config.abstract_model.model, nested_in: self)
  else
    nil
  end
end

#parse_input(params) ⇒ Object



127
128
129
# File 'lib/basepack/forms/fields/base.rb', line 127

def parse_input(params)
  @delegate.parse_input(params) if @delegate
end

#unique?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/basepack/forms/fields/base.rb', line 148

def unique?
  abstract_model.model.validators_on(self.name).map(&:class).include?(ActiveRecord::Validations::UniquenessValidator)
end

#update_attributes(delegate_or_attributes) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/basepack/forms/fields/base.rb', line 83

def update_attributes(delegate_or_attributes)
  if delegate_or_attributes.is_a? Hash
    delegate_or_attributes.each do |a, v|
      send("#{a}=", v)
    end
  else
    #raise ArgumentError, "Invalid delegate #{delegate_or_attributes}" unless
    #  delegate_or_attributes.respond_to? :type
    @delegate = delegate_or_attributes
  end
end

#valueObject



115
116
117
# File 'lib/basepack/forms/fields/base.rb', line 115

def value
  form.resource.send(name)
end

#viewObject



95
96
97
# File 'lib/basepack/forms/fields/base.rb', line 95

def view
  form.view
end