Sha256: a86443c8130e52bb24402b888ab1649f86939d7d74ef5b1afcfa1a8615b3c9ce

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

module ErpProducts
  module Extensions
    module ActiveRecord
      module ActsAsProductOffer

        def self.included(base)
          base.extend(ClassMethods)
        end

        module ClassMethods
          def acts_as_product_offer
            extend ActsAsProductOffer::SingletonMethods
            include ActsAsProductOffer::InstanceMethods

            after_initialize :initialize_product_offer
            after_create :save_product_offer
            after_update :save_product_offer
            after_destroy :destroy_product_offer

            has_one :product_offer, :as => :product_offer_record

            [:description, :description=, :valid_from,
             :valid_from=, :valid_to, :valid_to=].each do |m|
              delegate m, :to => :product_offer
            end
          end
        end

        module SingletonMethods
        end

        module InstanceMethods
          def initialize_product_offer
            if self.new_record? && self.product_offer.nil?
              product_offer = ProductOffer.new
              self.product_offer = product_offer
              product_offer.save
              self.save
            end
          end

          def save_product_offer
            self.product_offer.description = self.description
            self.product_offer.save
          end

          def destroy_product_offer
            self.product_offer.destroy if (self.product_offer && !self.product_offer.frozen?)
          end

        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
erp_products-4.2.0 lib/erp_products/extensions/active_record/acts_as_product_offer.rb