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].freeze

Instance Method Summary collapse

Instance Method Details

#as_json(options = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/custom_fields/field.rb', line 79

def as_json(options = {})
  method_name     = :"#{type}_as_json"
  custom_as_json  = begin
    send(method_name)
  rescue StandardError
    {}
  end

  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_#{type}_diff"

  if respond_to?(method_name)
    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
74
75
76
77
# File 'lib/custom_fields/field.rb', line 63

def to_recipe
  method_name       = :"#{type}_to_recipe"
  custom_to_recipe  = begin
    send(method_name)
  rescue StandardError
    {}
  end

  { 'name' => name,
    'type' => type,
    'required' => required?,
    'unique' => unique?,
    'localized' => localized?,
    'default' => 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