class Para::AttributeField::Base

Attributes

field_method[R]
field_type[R]
model[R]
name[R]
options[R]
type[R]

Public Class Methods

field_option(key, method_name, options = {}) click to toggle source
# File lib/para/attribute_field/base.rb, line 9
def self.field_option(key, method_name, options = {})
  self._field_options ||= []

  self._field_options += [{
    key: key,
    method_name: method_name,
    options: options
  }]
end
field_types() click to toggle source
# File lib/para/attribute_field/base.rb, line 38
def self.field_types
  @_field_types ||= {}
end
new(model, options = {}) click to toggle source
# File lib/para/attribute_field/base.rb, line 44
def initialize(model, options = {})
  @model = model
  @name = options.delete(:name)
  @type = options.delete(:type)
  @field_type = options.delete(:field_type)
  @options = options

  determine_name_and_field_method!
end
register(*args) click to toggle source

Registers the class as the responder for a given field type

Example :

# This will allow looking :my_field or :myfield up and instantiate
# self as the field
class MyField < Para::AttributeField::Base
  register :my_field, :myfield, self
end
# File lib/para/attribute_field/base.rb, line 30
def self.register(*args)
  attribute_class = args.pop

  args.each do |arg|
    Base.field_types[arg] = attribute_class
  end
end

Public Instance Methods

attribute_column_path() click to toggle source
# File lib/para/attribute_field/base.rb, line 108
def attribute_column_path
  [name]
end
determine_name_and_field_method!() click to toggle source
# File lib/para/attribute_field/base.rb, line 54
def determine_name_and_field_method!
  name = @name

  reference = model.reflect_on_all_associations.find do |association|
    association.foreign_key == name
  rescue ArgumentError
    # This can happen when the association is polymorphic and the foreign key can't
    # be determined, in this case, we just ignore the association.
    false
  end

  if reference
    @name = reference.name
    @field_method = :association
  else
    @name = name
    @field_method = :input
  end
end
excerptable_value?() click to toggle source
# File lib/para/attribute_field/base.rb, line 78
def excerptable_value?
  true
end
field_name() click to toggle source
# File lib/para/attribute_field/base.rb, line 104
def field_name
  name
end
field_options() click to toggle source
# File lib/para/attribute_field/base.rb, line 97
def field_options
  self.class._field_options.each_with_object({}) do |params, hash|
    value = send(params[:method_name])
    hash[params[:key]] = value if !value.nil? || params[:options][:allow_nil]
  end
end
parse_input(params, resource) click to toggle source

Allows parsing input params before they’re passed to the model, so it can be easy to edit them according to some field type specific behavior

# File lib/para/attribute_field/base.rb, line 95
def parse_input(params, resource); end
searchable?() click to toggle source
# File lib/para/attribute_field/base.rb, line 82
def searchable?
  options[:searchable] != false && (
    %i[string text].include?(type.to_sym) && !name.match(/password/)
  ) && (
    !model.respond_to?(:ransackable_attributes) ||
    model.ransackable_attributes.include?(name.to_s)
  )
end
type?(type) click to toggle source
# File lib/para/attribute_field/base.rb, line 112
def type?(type)
  self.type.to_s == type.to_s
end
value_for(instance) click to toggle source
# File lib/para/attribute_field/base.rb, line 74
def value_for(instance)
  instance.send(name)
end

Private Instance Methods

field_type_name() click to toggle source
# File lib/para/attribute_field/base.rb, line 118
def field_type_name
  field_type.presence && field_type.to_sym
end