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') orders: Ember.computed -> @store.peekAll('order')
sections: Ember.computed -> @store.peekAll('section') sections: Ember.computed -> @store.peekAll('section')
#active_section: Ember.computed.alias 'globals.active_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) @get('orders') # trigger orders, otherwise observers are not initialized/triggered (active_orders)
if section_id = @get('globals.active_section.id') if section_id = @get('globals.active_section.id')
lists = @get('lists').filter (l)=>( l.get('table.section.id') == section_id && l.get('state') == 'active' ) 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 has_many :orders, dependent: :destroy
belongs_to :table belongs_to :table
belongs_to :supplier belongs_to :supplier
belongs_to :section belongs_to :section #TODO: deprecate
has_many :list_payments has_many :list_payments
has_and_belongs_to_many :users, storing_keys: true has_and_belongs_to_many :users, storing_keys: true
has_and_belongs_to_many :employees, storing_keys: true has_and_belongs_to_many :employees, storing_keys: true
@@ -46,12 +46,14 @@ class List
#} #}
#}|, reduce_function: '_sum' #}|, reduce_function: '_sum'
#TODO: deprecate
view :active_by_section_id_view, type: :custom, map_function: %|function(doc){ view :active_by_section_id_view, type: :custom, map_function: %|function(doc){
if(doc.ruby_class == 'List' && doc.state == 'active' && doc.section_id){ if(doc.ruby_class == 'List' && doc.state == 'active' && doc.section_id){
emit(doc.section_id, 1); emit(doc.section_id, 1);
} }
}|, reduce_function: '_sum' }|, reduce_function: '_sum'
#TODO: deprecate
view :active_for_supplier_and_section_view, type: :custom, map_function: %|function(doc){ view :active_for_supplier_and_section_view, type: :custom, map_function: %|function(doc){
if(doc.ruby_class == 'List' && doc.state == 'active'){ if(doc.ruby_class == 'List' && doc.state == 'active'){
emit([doc.supplier_id, doc.section_id], 1); emit([doc.supplier_id, doc.section_id], 1);
@@ -81,13 +83,13 @@ class List
# Create, a list given a table and a user # Create, a list given a table and a user
def self.from_table table, user def self.from_table table, user
return if user.has_active_list? 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.supplier_id = table.supplier_id
list.add_user user list.add_user user
list.save list.save
user.active_list_id = list.id user.active_list_id = list.id
user.save 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.broadcast_supplier list.supplier_id, 'list_added', list.with_info_as_json
list list
end end
@@ -95,7 +97,7 @@ class List
# Create, a list given a table and a employee # Create, a list given a table and a employee
def self.from_table_by_employee table, employee def self.from_table_by_employee table, employee
unless list = table.active_list 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.supplier_id = table.supplier_id
list.add_employee employee list.add_employee employee
list.save list.save
@@ -116,11 +118,13 @@ class List
database.view(active_view(startkey: [supplier.id], endkey: ["#{supplier.id}\u9999"], reduce: false, include_docs: true)) database.view(active_view(startkey: [supplier.id], endkey: ["#{supplier.id}\u9999"], reduce: false, include_docs: true))
end end
#TODO: deprecate
def self.active_for_section(section, options = {}) def self.active_for_section(section, options = {})
database.view(active_by_section_id_view(key: section.id, reduce: false, include_docs: true)) database.view(active_by_section_id_view(key: section.id, reduce: false, include_docs: true))
end end
# Return all currently active orders for a given section # Return all currently active orders for a given section
#TODO: deprecate
def self.active_for_supplier_and_section(supplier, section_id, options = {}) 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)) database.view(active_for_supplier_and_section_view(key: [supplier.id, section_id], reduce: false, include_docs: true))
end end
@@ -229,13 +233,13 @@ class List
UserTableMove.create list: self, from_table_id: table_id, to_table: to_table UserTableMove.create list: self, from_table_id: table_id, to_table: to_table
from_table_id = self.table_id.try(:dup) from_table_id = self.table_id.try(:dup)
self.table = to_table self.table = to_table
self.section_id = to_table.section_id #self.section_id = to_table.section_id
if save if save
# Update the section of an order # Update the section of an order
orders.each do |order| #orders.each do |order|
order.section_id = self.section_id #order.section_id = self.section_id
order.save #order.save
end #end
# user performs a client side refresh # 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_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', 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) def place_order(product_orders: [], user: nil, employee: nil, first_order: true)
return false unless product_orders.any? 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 return unless order.id
orders_placed_count = supplier.increment_orders_placed_count! orders_placed_count = supplier.increment_orders_placed_count!
loaded_products = self.class.database.load_document product_orders.map{|po| po['product_id'] || po['product']} 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
orders.product_orders.product orders.product_orders.product
users users
section
table table
]) ])
# broadcast_supplier supplier.id, 'new_order', OrderSerializer.new(order) # broadcast_supplier supplier.id, 'new_order', OrderSerializer.new(order)
+2
View File
@@ -92,6 +92,7 @@ class Supplier
employee_settings.for_employee(employee) employee_settings.for_employee(employee)
end end
#TODO: depricate
def active_orders(options = {}) def active_orders(options = {})
return @active_orders if @active_orders && @active_orders_options == options return @active_orders if @active_orders && @active_orders_options == options
@active_orders_options = options @active_orders_options = options
@@ -101,6 +102,7 @@ class Supplier
end end
# Tody, simplify to List.active_for_supplier(self) # Tody, simplify to List.active_for_supplier(self)
#TODO: depricate
def active_lists(options = {}) def active_lists(options = {})
return @active_lists if @active_lists 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) @active_lists = options[:section_id].present? ? List.active_for_supplier_and_section(self, options[:section_id]) : List.active_for_supplier(self)
-8
View File
@@ -56,14 +56,6 @@ describe List do
it "changes the table" do it "changes the table" do
expect{ list.move_to_table! new_table }.to change{ list.table_id }.to( new_table.id ) expect{ list.move_to_table! new_table }.to change{ list.table_id }.to( new_table.id )
end end
it "changes the section" do
expect{ list.move_to_table! new_table }.to change{ list.section_id }.to( new_section.id )
end
it "changes the section of the orders" do
expect{ list.move_to_table! new_table }.to change{ @order1.reload; @order1.section_id }.to( new_section.id )
end
end end
describe '#has_active_orders?' do describe '#has_active_orders?' do
+5 -7
View File
@@ -4,15 +4,13 @@ describe "User lists", type: :request do
let(:supplier){ create :supplier } let(:supplier){ create :supplier }
let(:user){ create :user} let(:user){ create :user}
before do before do
supplier.add_employee(employee) #supplier.add_employee(employee)
login_as employee, scope: :employee #login_as employee, scope: :employee
end end
it "works" do it "works" do
supplier = create :supplier supplier = create :supplier
get "/supplier/suppliers/#{supplier.id}.json" get current_users_lists_path, auth_token: user.authentication_token
api_response.data.relationships.product_categorie expect( api_response ).to eq Hash.new
included_section = api_response.included.find{|ih| ih['type'] == 'sections'} response.status.should eq 404
included_section['relationships']['tables']['data'].should eq []
included_section['relationships']['section-areas']['data'].should eq []
end end
end end