class CustomFields::Field

Constants

AVAILABLE_TYPES

Public Instance Methods

as_json(options = {}) click to toggle source
Calls superclass method
# 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) click to toggle source

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

@param [ Hash ] memo Store the updates

@return [ Hash ] The memo object upgraded

# 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
to_recipe() click to toggle source

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

@return [ Hash ] The hash

# 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

Protected Instance Methods

set_name() click to toggle source
# File lib/custom_fields/field.rb, line 100
def set_name
  return if label.blank? && name.blank?

  return unless name.blank?

  self.name = ActiveSupport::Inflector.parameterize(label, separator: '_').gsub('-', '_').downcase
end
siblings() click to toggle source
# File lib/custom_fields/field.rb, line 108
def siblings
  _parent.send(association_name)
end
uniqueness_of_label_and_name() click to toggle source
# File lib/custom_fields/field.rb, line 92
def uniqueness_of_label_and_name
  errors.add(:label, :taken) if siblings.any? { |f| f.label == label && f._id != _id }

  return unless siblings.any? { |f| f.name == name && f._id != _id }

  errors.add(:name, :taken)
end