many changes

This commit is contained in:
2012-12-03 18:39:36 +01:00
parent e3dc6a7c68
commit 7d64ab2022
37 changed files with 540 additions and 101 deletions
+7 -1
View File
@@ -8,6 +8,8 @@ class List
property :closed_at, type: Time
property :join_requests, type: Array, default: []
property :price, type: Float
property :is_payed, type: :boolean, default: false
property :payed_at, type: Time
has_many :orders, dependent: :destroy
belongs_to :table
@@ -90,6 +92,11 @@ class List
database.view(for_supplier_view({startkey: [supplier.id, range.last], endkey: [supplier.id, range.first], include_docs: true, reduce: false, descending: true}.merge(options)))
end
def mark_as_payed
self.is_payed = true
self.payed_at = Time.now
end
def close!
orders.include_relation(:product_orders)
set_price
@@ -155,7 +162,6 @@ class List
add_user(user)
user.save
self.is_dirty
binding.pry
if save
broadcast_user user.id, 'join_request_approved'
end
+17 -1
View File
@@ -5,10 +5,26 @@ class Product
property :code
property :price, type: Float
belongs_to :product_category
#belongs_to :product_category
has_and_belongs_to_many :product_categories, storing_keys: false
belongs_to :supplier # direct! category is an aid
has_many :product_orders
validates :supplier_id, presence: true
after_save :persist_product_category_ids
def product_category_ids=(ids)
@product_category_ids = ids.select(&:present?)
end
private
def persist_product_category_ids
return unless @product_category_ids.present?
database.load(@product_category_ids).each do |product_category|
product_category.product_ids ||= []
product_category.product_ids |= [id]
product_category.save
end
end
end
+32 -1
View File
@@ -5,10 +5,41 @@ class ProductCategory
property :position, type: Fixnum, default: 0
belongs_to :supplier
has_many :products
has_and_belongs_to_many :products, storing_keys: true
attr_protected :supplier_id
validates :position, numericality: true
validates :supplier_id, presence: true
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, 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