Sha256: c77615cf6835ba83c715e9c9b6d9edd1153e452011a01339f0e0d3f5b9376ef2

Contents?: true

Size: 1.72 KB

Versions: 2

Compression:

Stored size: 1.72 KB

Contents

module ErpProducts
  module Extensions
    module ActiveRecord
      module ActsAsProductInstance
        def self.included(base)
          base.extend(ClassMethods)
        end

        module ClassMethods
          def acts_as_product_instance
            extend ActsAsProductInstance::SingletonMethods
            include ActsAsProductInstance::InstanceMethods

            after_initialize :initialize_product_instance
            after_create :save_product_instance
            after_update :save_product_instance
            after_destroy :destroy_product_instance

            has_one :product_instance, :as => :product_instance_record

            [
                :product_type,
                :product_type=,
                :description,
                :description=,
                :external_identifier,
                :external_identifier=,
                :external_id_source,
                :external_id_source=,
                :product_feature_instances
            ].each do |m|
              delegate m, :to => :product_instance
            end

          end
        end

        module SingletonMethods
        end

        module InstanceMethods
          def save_product_instance
            self.product_instance.save
          end

          def initialize_product_instance
            if self.new_record? && self.product_instance.nil?
              product_instance = ProductInstance.new
              self.product_instance = product_instance
              product_instance.product_instance_record = self
            end
          end

          def destroy_product_instance
            self.product_instance.destroy if (self.product_instance && !self.product_instance.frozen?)
          end

        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
erp_products-4.2.0 lib/erp_products/extensions/active_record/acts_as_product_instance.rb
erp_products-4.0.0 lib/erp_products/extensions/active_record/acts_as_product_instance.rb