module CustomAttributes::ActsAsCustomizable::InstanceMethods

Public Instance Methods

assign_custom_values=(values) click to toggle source

Sets the values of the object's custom fields values is an array like [{'id' => 1, 'value' => 'foo'}, {'id' => 2, 'value' => 'bar'}]

# File lib/custom_attributes/acts_as/acts_as_customizable.rb, line 76
def assign_custom_values=(values)
  values_to_hash = values.each_with_object({}) do |v, hash|
    v = v.stringify_keys
    hash[v['id']] = v['value'] if v['id'] && v.key?('value')
  end

  self.custom_field_values = values_to_hash
end
available_custom_fields() click to toggle source

Helper function to access available custom fields from an instance

# File lib/custom_attributes/acts_as/acts_as_customizable.rb, line 64
def available_custom_fields
  self.class.available_custom_fields
end
custom_field_value(c) click to toggle source

Returns the value for the passed CustomField object or ID

# File lib/custom_attributes/acts_as/acts_as_customizable.rb, line 153
def custom_field_value(c)
  field_id = (c.is_a?(CustomField) ? c.id : c.to_i)
  custom_field_values.detect { |v| v.custom_field_id == field_id }.try(:value)
end
custom_field_values() click to toggle source

Accessor for custom fields, returns array of CustomFieldValues

# File lib/custom_attributes/acts_as/acts_as_customizable.rb, line 105
def custom_field_values
  @_custom_field_values ||= available_custom_fields.collect do |field|
    populate_custom_field_value(field)
  end
end
custom_field_values=(values) click to toggle source

Sets the values of the object's custom fields values is a hash like {'1' => 'foo', 2 => 'bar'}

# File lib/custom_attributes/acts_as/acts_as_customizable.rb, line 87
def custom_field_values=(values)
  values = values.stringify_keys

  custom_field_values.each do |custom_field_value|
    key = custom_field_value.custom_field_id.to_s
    slug = custom_field_value.custom_field_slug

    if values.key?(key)
      custom_field_value.value = values[key]
    elsif values.key?(slug)
      custom_field_value.value = values[slug]
    end
  end

  @custom_field_values_changed = true
end
custom_field_values_changed?() click to toggle source
# File lib/custom_attributes/acts_as/acts_as_customizable.rb, line 142
def custom_field_values_changed?
  @custom_field_values_changed == true
end
custom_value_for(c) click to toggle source

Returns a CustomValue object for the passed CustomField object or ID

# File lib/custom_attributes/acts_as/acts_as_customizable.rb, line 147
def custom_value_for(c)
  field_id = (c.is_a?(CustomField) ? c.id : c.to_i)
  custom_values.detect { |v| v.custom_field_id == field_id }
end
populate_custom_field_value(field) click to toggle source
# File lib/custom_attributes/acts_as/acts_as_customizable.rb, line 119
def populate_custom_field_value(field)
  x = CustomAttributes::CustomFieldValue.new
  x.custom_field = field
  x.customizable = self
  if field.multiple?
    values = custom_values.select { |v| v.custom_field == field }
    if values.empty?
      values << custom_values.build(customizable: self, custom_field: field)
    end
    x.instance_variable_set('@value', values.map(&:value))
  else
    cv = custom_values.detect { |v| v.custom_field == field }
    cv ||= custom_values.build(customizable: self, custom_field: field)
    x.instance_variable_set('@value', cv.value)
  end
  x.value_was = x.value.dup if x.value
  x
end
populated_custom_field_value(c) click to toggle source
# File lib/custom_attributes/acts_as/acts_as_customizable.rb, line 111
def populated_custom_field_value(c)
  field_id = (c.is_a?(CustomField) ? c.id : c.to_i)
  field = available_custom_fields.select { |field| field.id == field_id }.first

  return populate_custom_field_value(field) unless field.nil?
  CustomAttributes::CustomFieldValue.new
end
reassign_custom_field_values() click to toggle source
# File lib/custom_attributes/acts_as/acts_as_customizable.rb, line 196
def reassign_custom_field_values
  if @_custom_field_values
    values                   = @_custom_field_values.each_with_object({}) { |v, h| h[v.custom_field_id] = v.value; }
    @_custom_field_values    = nil
    self.custom_field_values = values
  end
end
reload(*args) click to toggle source
Calls superclass method
# File lib/custom_attributes/acts_as/acts_as_customizable.rb, line 209
def reload(*args)
  @_custom_field_values        = nil
  @custom_field_values_changed = false
  super
end
reset_custom_values!() click to toggle source
# File lib/custom_attributes/acts_as/acts_as_customizable.rb, line 204
def reset_custom_values!
  @_custom_field_values        = nil
  @custom_field_values_changed = true
end
save_custom_field_values() click to toggle source

Called after save of the extended model, so validation should already be over. This method is responsible for persisting the values that have been written to the CustomFieldValues and handle and save CustomValues correctly.

# File lib/custom_attributes/acts_as/acts_as_customizable.rb, line 173
def save_custom_field_values
  target_custom_values = []

  custom_field_values.each do |custom_field_value|
    if custom_field_value.value.is_a?(Array)
      custom_field_value.value.each do |v|
        target = custom_values.detect { |cv| cv.custom_field == custom_field_value.custom_field && cv.value == v }
        target ||= custom_values.build(customizable: self, custom_field: custom_field_value.custom_field, value: v)
        target_custom_values << target
      end
    else
      target = custom_values.detect { |cv| cv.custom_field == custom_field_value.custom_field }
      target ||= custom_values.build(customizable: self, custom_field: custom_field_value.custom_field)
      target.value = custom_field_value.value
      target_custom_values << target
    end
  end
  self.custom_values = target_custom_values
  custom_values.each(&:save)
  @custom_field_values_changed = false
  true
end
use_custom_value_json(hash = {}) click to toggle source

Set JSON representation in elasticsearch. Default is to decorate the models JSON presentation with custom values

# File lib/custom_attributes/acts_as/acts_as_customizable.rb, line 49
def use_custom_value_json(hash = {})
  to_json = {
    methods: :visible_in_search,
    include: {
      custom_field_values: {
        only: %i[custom_field_id value]
      } 
    }
  }.merge(hash)
  as_json(
    to_json
  )
end
validate_custom_field_values() click to toggle source

Extends model validation

  1. Calls .validate_value on each CustomFieldValue

  2. .validate_value calls .validate_custom_value on the CustomField,

  3. which calls the validate_custom_value method on the assigned FieldType.

The FieldType is therefor responsible for CustomValue validation.

# File lib/custom_attributes/acts_as/acts_as_customizable.rb, line 164
def validate_custom_field_values
  if new_record? || custom_field_values_changed?
    custom_field_values.each(&:validate_value)
  end
end
visible_custom_field_values() click to toggle source
# File lib/custom_attributes/acts_as/acts_as_customizable.rb, line 138
def visible_custom_field_values
  custom_field_values.select(&:visible?)
end