Class: CustomFields::Field

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps
Defined in:
lib/custom_fields/field.rb

Constant Summary collapse

AVAILABLE_TYPES =
%w(default string text email date date_time boolean file select float integer
money tags color relationship_default belongs_to has_many many_to_many password json)

Instance Method Summary collapse

Instance Method Details

#as_json(options = {}) ⇒ Object



75
76
77
78
79
80
# File 'lib/custom_fields/field.rb', line 75

def as_json(options = {})
  method_name     = :"#{self.type}_as_json"
  custom_as_json  = self.send(method_name) rescue {}

  super(options).merge(custom_as_json)
end

#collect_diff(memo) ⇒ Hash

Builds the mongodb updates based on the new state of the field. Call a different method if the field has a different behaviour.

Parameters:

  • memo (Hash)

    Store the updates

Returns:

  • (Hash)

    The memo object upgraded



47
48
49
50
51
52
53
54
55
# File 'lib/custom_fields/field.rb', line 47

def collect_diff(memo)
  method_name = :"collect_#{self.type}_diff"

  if self.respond_to?(method_name)
    self.send(method_name, memo)
  else
    collect_default_diff(memo)
  end
end

#labelObject

validations ##



17
# File 'lib/custom_fields/field.rb', line 17

field :label

#to_recipeHash

Returns the information (name, type, required, …etc) needed to build the custom class. That will be stored into the target instance.

Returns:

  • (Hash)

    The hash



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/custom_fields/field.rb', line 63

def to_recipe
  method_name       = :"#{self.type}_to_recipe"
  custom_to_recipe  = self.send(method_name) rescue {}

  { 'name'      => self.name,
    'type'      => self.type,
    'required'  => self.required?,
    'unique'    => self.unique?,
    'localized' => self.localized?,
    'default'   => self.default }.merge(custom_to_recipe)
end

#typeObject

types ##



12
13
14
# File 'lib/custom_fields/field.rb', line 12

AVAILABLE_TYPES.each do |type|
  include "CustomFields::Types::#{type.camelize}::Field".constantize
end