Sha256: d0d5a22f86c4a000d7ec6c0e2d5841addfbc43447a6a79a659e4ad1d40076f74

Contents?: true

Size: 1.1 KB

Versions: 4

Compression:

Stored size: 1.1 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 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

4 entries across 4 versions & 1 rubygems

Version Path
create_custom_attributes-0.5.4 lib/custom_attributes/acts_as/acts_as_custom_value.rb
create_custom_attributes-0.5.3 lib/custom_attributes/acts_as/acts_as_custom_value.rb
create_custom_attributes-0.5.2 lib/custom_attributes/acts_as/acts_as_custom_value.rb
create_custom_attributes-0.5.0 lib/custom_attributes/acts_as/acts_as_custom_value.rb