Sha256: 4487a66fdeca25174c485882276691fd80e0906ff30e7ec7d72e596cf9b81a42

Contents?: true

Size: 1.21 KB

Versions: 6

Compression:

Stored size: 1.21 KB

Contents

module PowerShop
  class Product < ActiveRecord::Base
    self.abstract_class = true
    self.table_name = 'products'

    belongs_to :category, class_name: '::Category'

    # == Calbacks for calculate active products in category
    after_create :increment_category_count, if: ->(product) { product.active? }
    after_destroy :decrement_category_count, if: ->(product) { product.active? }
    after_update :touch_category_count

    has_many :images, :class_name => '::ShopImage', :as => :subject,
      :dependent => :destroy

    accepts_nested_attributes_for :images, :allow_destroy => true
    validates_presence_of :name, :price, :category_id

    # Public: get only active products
    #
    # Returns ActiveRecord::Relation
    def self.active
      where(active: true)
    end

    # Public: get first image from images
    #
    # Returns ShopImage
    def main_image
      images.first
    end

    def increment_category_count
      ::Category.increment_counter :products_count, category_id
    end

    def decrement_category_count
      ::Category.decrement_counter :products_count, category_id
    end

    def touch_category_count
      active? ? increment_category_count : decrement_category_count
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
power_shop-0.2.4 app/models/power_shop/product.rb
power_shop-0.2.3 app/models/power_shop/product.rb
power_shop-0.2.2 app/models/power_shop/product.rb
power_shop-0.2.1 app/models/power_shop/product.rb
power_shop-0.2.0 app/models/power_shop/product.rb
power_shop-0.1.1 app/models/power_shop/product.rb