Sha256: 6313b86deb515b81b9675adfa9c0bc052820d99d7d1b4966becd22bc74ee9fd3

Contents?: true

Size: 1.51 KB

Versions: 3

Compression:

Stored size: 1.51 KB

Contents

class TbCommerce::ProductSku < ActiveRecord::Base
  self.table_name = 'tb_commerce_product_skus'

  scope :ordered, ->{ order('sku asc') }
  scope :search, ->(sku){ where('sku LIKE ?', "%#{sku}%") }

  validates :product, :presence => true
  validates :sku, :presence => true, :uniqueness => true
  validates :add_price, :stock, :numericality => {:greater_than_or_equal_to => 0}

  belongs_to :product, :inverse_of => :product_skus, :foreign_key => :tb_commerce_product_id
  has_many :product_sku_options, :dependent => :destroy, :inverse_of => :product_sku, :foreign_key => :tb_commerce_product_sku_id
  has_many :options, :through => :product_sku_options
  has_many :image_links, -> { order(:sort) }, :as => :imageable
  has_many :images, :through => :image_links

  def description
    return product.title
  end

  def price
    return product.price + add_price
  end

  # Return SKU records that support the provided list of option IDs
  #
  # - option_ids_list: Array of integer values corresponding to TbCommcerce::Option
  # 
  def self.with_option_ids(option_ids_list)
    return includes(:options).to_a.select do |sku|
      sku.has_options?(option_ids_list)
    end
  end

  # Return true if the current record supports the supplied list of option IDs
  #
  # - option_ids_list: Array of integer values corresponding to TbCommcerce::Option
  # 
  def has_options?(option_ids_list)
    option_ids_list.each do |option_id|
      if !self.option_ids.include?(option_id)
        return false
      end
    end
    return true
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tb_commerce-0.0.4 app/models/tb_commerce/product_sku.rb
tb_commerce-0.0.3 app/models/tb_commerce/product_sku.rb
tb_commerce-0.0.2 app/models/tb_commerce/product_sku.rb