remove denormalization of lists and orders towards the section

This commit is contained in:
2015-10-12 15:25:58 +02:00
parent a6c74f1872
commit 86e518890f
5 changed files with 22 additions and 27 deletions
@@ -6,7 +6,7 @@ App.IndexController = Ember.Controller.extend
orders: Ember.computed -> @store.peekAll('order')
sections: Ember.computed -> @store.peekAll('section')
#active_section: Ember.computed.alias 'globals.active_section'
active_lists: Ember.computed 'lists.@each.state', 'globals.active_section.id', 'lists.@each.table', ->
active_lists: Ember.computed 'lists.@each.state', 'globals.active_section.id', 'lists.@each.table', -> # table needed for when list changes table
@get('orders') # trigger orders, otherwise observers are not initialized/triggered (active_orders)
if section_id = @get('globals.active_section.id')
lists = @get('lists').filter (l)=>( l.get('table.section.id') == section_id && l.get('state') == 'active' )
+14 -11
View File
@@ -16,7 +16,7 @@ class List
has_many :orders, dependent: :destroy
belongs_to :table
belongs_to :supplier
belongs_to :section
belongs_to :section #TODO: deprecate
has_many :list_payments
has_and_belongs_to_many :users, storing_keys: true
has_and_belongs_to_many :employees, storing_keys: true
@@ -46,12 +46,14 @@ class List
#}
#}|, reduce_function: '_sum'
#TODO: deprecate
view :active_by_section_id_view, type: :custom, map_function: %|function(doc){
if(doc.ruby_class == 'List' && doc.state == 'active' && doc.section_id){
emit(doc.section_id, 1);
}
}|, reduce_function: '_sum'
#TODO: deprecate
view :active_for_supplier_and_section_view, type: :custom, map_function: %|function(doc){
if(doc.ruby_class == 'List' && doc.state == 'active'){
emit([doc.supplier_id, doc.section_id], 1);
@@ -81,13 +83,13 @@ class List
# Create, a list given a table and a user
def self.from_table table, user
return if user.has_active_list?
list = new table: table, section_id: table.section_id
list = new table: table #, section_id: table.section_id
list.supplier_id = table.supplier_id
list.add_user user
list.save
user.active_list_id = list.id
user.save
# list_added is depricated, now handled by list_update
# list_added is deprecated, now handled by list_update
#list.broadcast_supplier list.supplier_id, 'list_added', list.with_info_as_json
list
end
@@ -95,7 +97,7 @@ class List
# Create, a list given a table and a employee
def self.from_table_by_employee table, employee
unless list = table.active_list
list = new table: table, section_id: table.section_id
list = new table: table #, section_id: table.section_id
list.supplier_id = table.supplier_id
list.add_employee employee
list.save
@@ -116,11 +118,13 @@ class List
database.view(active_view(startkey: [supplier.id], endkey: ["#{supplier.id}\u9999"], reduce: false, include_docs: true))
end
#TODO: deprecate
def self.active_for_section(section, options = {})
database.view(active_by_section_id_view(key: section.id, reduce: false, include_docs: true))
end
# Return all currently active orders for a given section
#TODO: deprecate
def self.active_for_supplier_and_section(supplier, section_id, options = {})
database.view(active_for_supplier_and_section_view(key: [supplier.id, section_id], reduce: false, include_docs: true))
end
@@ -229,13 +233,13 @@ class List
UserTableMove.create list: self, from_table_id: table_id, to_table: to_table
from_table_id = self.table_id.try(:dup)
self.table = to_table
self.section_id = to_table.section_id
#self.section_id = to_table.section_id
if save
# Update the section of an order
orders.each do |order|
order.section_id = self.section_id
order.save
end
#orders.each do |order|
#order.section_id = self.section_id
#order.save
#end
# user performs a client side refresh
broadcast_users 'list_changed_table', list_id: id, from_table_id: from_table_id, to_table_id: to_table.id
broadcast_supplier supplier_id, 'list_changed_table',
@@ -288,7 +292,7 @@ class List
def place_order(product_orders: [], user: nil, employee: nil, first_order: true)
return false unless product_orders.any?
order = Order.create list: self, supplier: supplier, user: user, employee: employee, section_id: section_id, table_id: table_id
order = Order.create list: self, supplier: supplier, user: user, employee: employee #, section_id: section_id, table_id: table_id
return unless order.id
orders_placed_count = supplier.increment_orders_placed_count!
loaded_products = self.class.database.load_document product_orders.map{|po| po['product_id'] || po['product']}
@@ -321,7 +325,6 @@ class List
orders.product_orders
orders.product_orders.product
users
section
table
])
# broadcast_supplier supplier.id, 'new_order', OrderSerializer.new(order)
+2
View File
@@ -92,6 +92,7 @@ class Supplier
employee_settings.for_employee(employee)
end
#TODO: depricate
def active_orders(options = {})
return @active_orders if @active_orders && @active_orders_options == options
@active_orders_options = options
@@ -101,6 +102,7 @@ class Supplier
end
# Tody, simplify to List.active_for_supplier(self)
#TODO: depricate
def active_lists(options = {})
return @active_lists if @active_lists
@active_lists = options[:section_id].present? ? List.active_for_supplier_and_section(self, options[:section_id]) : List.active_for_supplier(self)