From 86e518890f6e3cb9ed8f5e9a5d1e141b3f3d939d Mon Sep 17 00:00:00 2001 From: Benjamin ter Kuile Date: Mon, 12 Oct 2015 15:25:58 +0200 Subject: [PATCH] remove denormalization of lists and orders towards the section --- .../supplier/app/controllers/index.js.coffee | 2 +- app/models/list.rb | 25 +++++++++++-------- app/models/supplier.rb | 2 ++ spec/models/list_spec.rb | 8 ------ spec/requests/users/lists_spec.rb | 12 ++++----- 5 files changed, 22 insertions(+), 27 deletions(-) diff --git a/app/assets/javascripts/supplier/app/controllers/index.js.coffee b/app/assets/javascripts/supplier/app/controllers/index.js.coffee index e501a8ef..c34cc9c2 100644 --- a/app/assets/javascripts/supplier/app/controllers/index.js.coffee +++ b/app/assets/javascripts/supplier/app/controllers/index.js.coffee @@ -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' ) diff --git a/app/models/list.rb b/app/models/list.rb index 89df415b..3c7d1da0 100644 --- a/app/models/list.rb +++ b/app/models/list.rb @@ -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) diff --git a/app/models/supplier.rb b/app/models/supplier.rb index a5cf668b..1d467647 100644 --- a/app/models/supplier.rb +++ b/app/models/supplier.rb @@ -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) diff --git a/spec/models/list_spec.rb b/spec/models/list_spec.rb index abdeaf31..e6b79e97 100644 --- a/spec/models/list_spec.rb +++ b/spec/models/list_spec.rb @@ -56,14 +56,6 @@ describe List do it "changes the table" do expect{ list.move_to_table! new_table }.to change{ list.table_id }.to( new_table.id ) 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 describe '#has_active_orders?' do diff --git a/spec/requests/users/lists_spec.rb b/spec/requests/users/lists_spec.rb index a061221c..7fcdf152 100644 --- a/spec/requests/users/lists_spec.rb +++ b/spec/requests/users/lists_spec.rb @@ -4,15 +4,13 @@ describe "User lists", type: :request do let(:supplier){ create :supplier } let(:user){ create :user} before do - supplier.add_employee(employee) - login_as employee, scope: :employee + #supplier.add_employee(employee) + #login_as employee, scope: :employee end it "works" do supplier = create :supplier - get "/supplier/suppliers/#{supplier.id}.json" - api_response.data.relationships.product_categorie - included_section = api_response.included.find{|ih| ih['type'] == 'sections'} - included_section['relationships']['tables']['data'].should eq [] - included_section['relationships']['section-areas']['data'].should eq [] + get current_users_lists_path, auth_token: user.authentication_token + expect( api_response ).to eq Hash.new + response.status.should eq 404 end end