Sha256: fc3cd4f0daf51dd9ccc9413fe6bd89474b0801ca14dc55142b515b47eea2896c

Contents?: true

Size: 1.37 KB

Versions: 16

Compression:

Stored size: 1.37 KB

Contents

module CustomAttributes
  module ActsAsCustomValue
    extend ActiveSupport::Concern

    included do
    end

    module ClassMethods
      def acts_as_custom_value(_options = {})
        include CustomAttributes::ActsAsCustomValue::InstanceMethods

        belongs_to :custom_field
        belongs_to :customizable, polymorphic: true

        after_save :custom_field_after_save_custom_value
      end
    end

    module InstanceMethods
      def initialize(attributes = nil, *args)
        super
        if new_record? && custom_field && !attributes.key?(:value)
          self.value ||= custom_field.default
        end
      end

      def value
        field_value = read_attribute(:value) || nil

        field_value = nil if field_value.nil? || ( !field_value.is_a?(Numeric) && field_value.empty? )
        return field_value if new_record?

        field_value || custom_field.try(:default)
      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

      private

      # Calls CustomAttributes::FieldType.after_save_custom_value
      # Thus extendable by FieldType
      # Default is: do nothing
      def custom_field_after_save_custom_value
        custom_field.after_save_custom_value(self)
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

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