Files
old-ribica/front-api/models/item.rb

30 lines
1.3 KiB
Ruby
Raw Normal View History

2015-01-22 06:38:48 +01:00
class Item < ActiveRecord::Base
belongs_to :unit
2015-03-28 14:38:15 +01:00
belongs_to :brand
2015-01-22 06:38:48 +01:00
has_many :multi_media_descriptions
belongs_to :sub_category
has_many :item_in_groups
has_many :item_groups, through: :item_in_groups
attr_accessor :count # used for cart
def attributes
super.merge('count' => self.count)
end
2015-01-22 06:38:48 +01:00
# TODO: change "best selling" algorithm when get some data - currently it's only sorted by earnings
scope :best_selling, -> (offset, limit) { where(:on_display => true).order("list_price ASC").limit(limit).offset(offset) }
2015-01-22 06:38:48 +01:00
scope :best_selling_in_sub_category, -> (sub_category_id, offset, limit) { best_selling(offset, limit).where(sub_category_id: sub_category_id) }
2015-01-22 06:38:48 +01:00
scope :best_selling_in_category, -> (category_id, offset, limit) { best_selling(offset, limit).joins( sub_category: [:category] ).where(["category_id = ?", category_id]) }
2015-01-22 06:38:48 +01:00
scope :best_selling_in_section, -> (section_id, offset, limit) { best_selling(offset, limit).joins( sub_category: [category: [ :section ]] ).where( ["section_id = ?", section_id] ) }
scope :all_in_category, -> (category_id) { where(on_display: true).joins( sub_category: [:category]).where(["category_id = ?", category_id]) }
2015-03-29 23:23:21 +02:00
scope :all_in_sub_category, -> (sub_category_id) { where(on_display: true).where(["sub_category_id = ?", sub_category_id]) }
2015-01-22 06:38:48 +01:00
end