class Basepack::Forms::Fields::Base

Constants

ASSOC_TYPES

Attributes

delegate[R]
form[R]
name[R]

Public Class Methods

new(name, form, delegate_or_attributes = nil) click to toggle source
# 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

Public Instance Methods

association?() click to toggle source
# File lib/basepack/forms/fields/base.rb, line 79
def association?
  !!ASSOC_TYPES[type]
end
bulk_editable?() click to toggle source
# File lib/basepack/forms/fields/base.rb, line 152
def bulk_editable?
  true
end
configure_nested_form(&block) click to toggle source
# 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) click to toggle source
# 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_options() click to toggle source
# 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?() click to toggle source
# 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_label() click to toggle source
# File lib/basepack/forms/fields/base.rb, line 144
def nested_label
  form.nested_in ? "#{form.nested_in.nested_label}: #{label}" : label
end
nform() click to toggle source
# 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) click to toggle source
# File lib/basepack/forms/fields/base.rb, line 127
def parse_input(params)
  @delegate.parse_input(params) if @delegate
end
unique?() click to toggle source
# 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) click to toggle source
# 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
value() click to toggle source
# File lib/basepack/forms/fields/base.rb, line 115
def value
  form.resource.send(name)
end
view() click to toggle source
# File lib/basepack/forms/fields/base.rb, line 95
def view
  form.view
end