Sha256: a8600ba49d63dd0160d7eb73418f1c17058aa1c1086afd29a7626eedc4528e31

Contents?: true

Size: 1.08 KB

Versions: 18

Compression:

Stored size: 1.08 KB

Contents

module CustomAttributes
  # Decorator for CustomValues
  class CustomFieldValue
    attr_accessor :custom_field, :customizable, :value, :value_was

    def initialize(attributes = {})
      attributes.each do |name, v|
        send "#{name}=", v
      end
    end

    def custom_field_id
      custom_field.id
    end

    def custom_field_slug
      custom_field.slug
    end

    def true?
      value == '1'
    end

    def visible?
      custom_field.visible?
    end

    def required?
      custom_field.is_required?
    end

    def to_s
      value.to_s
    end

    def serializable_hash(options = nil)
      { custom_field_id: self.custom_field_id, value: self.value }.as_json
    end

    def value=(v)
      @value = custom_field.set_custom_field_value(self, v)
    end

    def value_present?
      if value.is_a?(Array)
        value.any?(&:present?)
      else
        value.present?
      end
    end

    def validate_value
      custom_field.validate_custom_value(self).each do |message|
        customizable.errors.add(:base, custom_field.name + ' ' + message)
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
create_custom_attributes-0.6.3 lib/custom_attributes/custom_field_value.rb
create_custom_attributes-0.6.2 lib/custom_attributes/custom_field_value.rb
create_custom_attributes-0.6.1 lib/custom_attributes/custom_field_value.rb
create_custom_attributes-0.5.25 lib/custom_attributes/custom_field_value.rb
create_custom_attributes-0.5.24 lib/custom_attributes/custom_field_value.rb
create_custom_attributes-0.5.23 lib/custom_attributes/custom_field_value.rb
create_custom_attributes-0.5.22 lib/custom_attributes/custom_field_value.rb
create_custom_attributes-0.5.21 lib/custom_attributes/custom_field_value.rb
create_custom_attributes-0.5.20 lib/custom_attributes/custom_field_value.rb
create_custom_attributes-0.5.19 lib/custom_attributes/custom_field_value.rb
create_custom_attributes-0.5.18 lib/custom_attributes/custom_field_value.rb
create_custom_attributes-0.5.17 lib/custom_attributes/custom_field_value.rb
create_custom_attributes-0.5.16 lib/custom_attributes/custom_field_value.rb
create_custom_attributes-0.5.15 lib/custom_attributes/custom_field_value.rb
create_custom_attributes-0.5.14 lib/custom_attributes/custom_field_value.rb
create_custom_attributes-0.5.13 lib/custom_attributes/custom_field_value.rb
create_custom_attributes-0.5.12 lib/custom_attributes/custom_field_value.rb
create_custom_attributes-0.5.11 lib/custom_attributes/custom_field_value.rb