58 lines
2.0 KiB
Ruby
58 lines
2.0 KiB
Ruby
class ProductCategory
|
|
include SimplyStored::Couch
|
|
|
|
property :name
|
|
property :position, type: Fixnum, default: 0
|
|
property :week_days, type: Array, default: Array.new(7, 1)
|
|
|
|
belongs_to :supplier
|
|
has_and_belongs_to_many :products, storing_keys: true
|
|
|
|
attr_protected :supplier_id
|
|
|
|
validates :name, presence: true
|
|
validates :position, numericality: true
|
|
validates :supplier_id, presence: true
|
|
view :by_supplier_id_and_id, key: [:supplier_id, :_id]
|
|
|
|
#alias orignal_week_days= week_days=
|
|
def week_days=(ary)
|
|
#write_attribute(:week_days, ary.map(&:to_i))
|
|
typecasted_value = ary.map(&:to_i)
|
|
#return typecasted_value if @week_days == typecasted_value
|
|
week_days_will_change! unless @skip_dirty_tracking || typecasted_value == week_days
|
|
@week_days = typecasted_value
|
|
end
|
|
|
|
def self.for_user(user, options = {})
|
|
table = options[:table]
|
|
raise "ProductCategory.for_user requires a table" unless table.present?
|
|
list = options[:list] || table.active_list
|
|
|
|
# Get supplier objects
|
|
products = table.supplier.products
|
|
product_categories = table.supplier.product_categories || []
|
|
|
|
# sort categories
|
|
product_categories.sort_by!{|pc| (pc.position || 90000).to_i }
|
|
|
|
# Append other category if not defined by supplier
|
|
other = product_categories.find(&:other?) || (product_categories << self.other).last # Container for non categorized products
|
|
|
|
# Initialize base return object
|
|
h = {table_number: table.number, table_occupied: table.occupied?, supplier_name: table.supplier.name, my_list: list.try(:user_ids).to_a.include?(user.id)}
|
|
|
|
(products - product_categories.map(&:products).flatten).each{ |p| other.add_product(p) }
|
|
|
|
h[:categories] = product_categories.map{|pc| {name: pc.name, products: pc.products.to_a.map{|p| p.as_json}}}.select{|pc| pc && pc[:products].present?}
|
|
h
|
|
end
|
|
|
|
def other?
|
|
%w[other overig].include?(name.to_s.downcase)
|
|
end
|
|
def self.other(options = {})
|
|
new(options.reverse_merge(name: I18n.t('user.product_category.other_name')))
|
|
end
|
|
end
|