diff --git a/app/assets/javascripts/supplier/list.js.coffee b/app/assets/javascripts/supplier/list.js.coffee index 80c58ce6..6717dd7c 100644 --- a/app/assets/javascripts/supplier/list.js.coffee +++ b/app/assets/javascripts/supplier/list.js.coffee @@ -9,6 +9,7 @@ class List active: -> @attributes.state == 'active' table_id: -> @attributes.table_id products: -> @attributes.products || [] + has_active_orders: -> @attributes.has_active_orders supplier_name: -> @attributes.supplier_name created_at: -> @attributes.created_at display: -> diff --git a/app/assets/javascripts/supplier/qsupplier.js.coffee b/app/assets/javascripts/supplier/qsupplier.js.coffee index 5fb043f9..bfde9708 100644 --- a/app/assets/javascripts/supplier/qsupplier.js.coffee +++ b/app/assets/javascripts/supplier/qsupplier.js.coffee @@ -35,6 +35,7 @@ root.Qsupplier= table.addClass('occupied') table.addClass('needs_help') if list.needs_help() table.addClass('needs_payment') if list.needs_payment() + table.addClass('active_order') if list.has_active_orders() else if e.event == 'list_closed' $('#list-row-'+e.data.id).remove() $('.of-list-'+e.data.id).remove() @@ -52,20 +53,24 @@ root.Qsupplier= $('#order-row-'+e.data.id).remove() $('.section-table-list-'+e.data.list_id).removeClass('active_order') else if e.event == 'list_changed_table' - list_row = $('#list-row-'+e.data.list_id) - list_row.find('.table_number').text(e.data.table.number).addClass('changed') + list = new List(e.data.list) + list_row = $('#list-row-'+list.id()) + list_row.find('.table_number').text(list.table_number()).addClass('changed') list_row.find('.section_title').text(e.data.section_title) - order_rows = $('.of-list-'+e.data.list_id) - order_rows.find('.table_number').text(e.data.table.number).addClass('changed') + order_rows = $('.of-list-'+list.id()) + order_rows.find('.table_number').text(list.table_number()).addClass('changed') order_rows.find('.section_title').text(e.data.section_title) # Move properties from table in section tables view window.last_data = e.data from_table = $('#section-table-'+e.data.from_table_id) - to_table = $('#section-table-'+e.data.table._id) - # Now switch classes - from_class = from_table.attr('class') - from_table.attr('class', to_table.attr('class')) - to_table.attr('class', from_class) + to_table = $('#section-table-'+list.table_id()) + if to_table.length + to_table.addClass('section-table-list-'+list.id()) + to_table.addClass('occupied') + to_table.addClass('needs_help') if list.needs_help() + to_table.addClass('needs_payment') if list.needs_payment() + to_table.addClass('active_order') if list.has_active_orders() + from_table.removeClass('occupied needs_help needs_payment active_order section-table-list-'+list.id()) console.log(e) diff --git a/app/assets/javascripts/user/quser.js.coffee b/app/assets/javascripts/user/quser.js.coffee index 2c5e6179..6d16ce09 100644 --- a/app/assets/javascripts/user/quser.js.coffee +++ b/app/assets/javascripts/user/quser.js.coffee @@ -27,7 +27,8 @@ class Quser else if(e.event == 'order_being_delivered') $('#order-row-'+e.data.id).addClass('delivered') else if(e.event == 'list_changed_table') - $('.table-number').text(e.data.table.number) + list = new List(e.data.list) + $('.table-number').text(list.table_number()) else if(e.event == 'list_needs_help') window.active_list.needs_help = true @list_needs_help_default_action() diff --git a/app/models/list.rb b/app/models/list.rb index 737818b5..686bce99 100644 --- a/app/models/list.rb +++ b/app/models/list.rb @@ -168,10 +168,15 @@ class List self.table = to_table self.section_id = to_table.section_id if save - for user_id in user_ids - broadcast_user user_id, 'list_changed_table', list_id: id, table: to_table, section_title: to_table.section.try(:title), from_table_id: from_table + # Update the section of an order + orders.each do |order| + order.section_id = self.section_id + order.save end - broadcast_supplier supplier_id, 'list_changed_table', list_id: id, table: to_table, section_title: to_table.section.try(:title), from_table_id: from_table + for user_id in user_ids + broadcast_user user_id, 'list_changed_table', list: as_json, section_title: to_table.section.try(:title), from_table_id: from_table + end + broadcast_supplier supplier_id, 'list_changed_table', list: as_json, section_title: to_table.section.try(:title), from_table_id: from_table end end @@ -272,7 +277,7 @@ class List end def as_json(*args) - super.merge(table_number: table_number) + super.merge(table_number: table_number, has_active_orders: has_active_orders? ) end def with_orders_as_json @@ -305,6 +310,10 @@ class List @with_orders_and_join_requests_as_json = with_orders_as_json.merge(join_requests_as_json) end + def has_active_orders? + Order.count_active_for_supplier_and_list(supplier_id, id) > 0 + end + # Return a join requests object in the form of: # {join_request: [{user_id: '1saf3...', user_email: 'info@qwaiter.com'}, [....]]} def join_requests_as_json diff --git a/app/models/order.rb b/app/models/order.rb index a81b13dd..54e88644 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -15,25 +15,40 @@ class Order view :active_for_supplier_view, type: :custom, map_function: %[function(doc){ if(doc.ruby_class == 'Order' && (doc.state == 'placed' || doc.state == 'active')){ - emit(doc.supplier_id, 1); + emit([doc.supplier_id, doc.list_id], 1); } - }], reduce_function: '_sum' + }], reduce_function: '_sum' + view :active_for_supplier_and_section_view, type: :custom, map_function: %[function(doc){ if(doc.ruby_class == 'Order' && (doc.state == 'placed' || doc.state == 'active')){ emit([doc.supplier_id, doc.section_id], 1); } - }], reduce_function: '_sum' - view :by_supplier_id_and_id, key: [:supplier_id, :_id] - view :by_list_id, key: :list_id + }], reduce_function: '_sum' + + #view :by_supplier_id_and_id, key: [:supplier_id, :_id] # Return all currently active orders for a given supplier def self.active_for_supplier(supplier_id) - database.view(active_for_supplier_view(key: supplier_id, reduce: false, include_docs: true)) + database.view(active_for_supplier_view(startkey: [supplier_id], endkey: [supplier_id, {}], reduce: false, include_docs: true)) + end + + # Return all currently active orders for a given supplier and list + def self.active_for_supplier_and_list(supplier_id, list_id) + supplier_id = supplier_id.id if supplier_id.is_a?(SimplyStored::Couch) + list_id = list_id.id if list_id.is_a?(SimplyStored::Couch) + database.view(active_for_supplier_view(key: [supplier_id, list_id], reduce: false, include_docs: true)) + end + + def self.count_active_for_supplier_and_list(supplier_id, list_id) + supplier_id = supplier_id.id if supplier_id.is_a?(SimplyStored::Couch) + list_id = list_id.id if list_id.is_a?(SimplyStored::Couch) + database.view(active_for_supplier_view(key: [supplier_id, list_id], reduce: true, include_docs: false)) end # Return all currently active orders for a given section def self.active_for_supplier_and_section(supplier, section_id) supplier_id = supplier.is_a?(SimplyStored::Couch) ? supplier.id : supplier + section_id = section_id.id if section_id.is_a?(SimplyStored::Couch) database.view(active_for_supplier_and_section_view(key: [supplier_id, section_id], reduce: false, include_docs: true)) end diff --git a/app/views/suppliers/sections/tables_view.html.slim b/app/views/suppliers/sections/tables_view.html.slim index bf7c9aea..7bc89573 100644 --- a/app/views/suppliers/sections/tables_view.html.slim +++ b/app/views/suppliers/sections/tables_view.html.slim @@ -26,6 +26,5 @@ Qsupplier.position_table_in_active_section(active_section_container, $(this), false); }); Qsupplier.update_section_tables_view('#{@section.id}'); - //setInterval("Qsupplier.update_section_tables_view('#{@section.id}')", 7500); Qsupplier.watch_events(); }); diff --git a/spec/factories/order_factory.rb b/spec/factories/order_factory.rb new file mode 100644 index 00000000..e36aaad9 --- /dev/null +++ b/spec/factories/order_factory.rb @@ -0,0 +1,7 @@ +FactoryGirl.define do + factory :order do + association :list + association :user + association :supplier #TODO warning! this may create a different supplier than the one created by the associated table + end +end diff --git a/spec/factories/user_factory.rb b/spec/factories/user_factory.rb index 9cb79a2a..8d14f246 100644 --- a/spec/factories/user_factory.rb +++ b/spec/factories/user_factory.rb @@ -1,6 +1,6 @@ FactoryGirl.define do factory :user do - email "test@example.com" + sequence( :email ){|i| "test#{i}@example.com" } password "secret" end end diff --git a/spec/models/list_spec.rb b/spec/models/list_spec.rb index 884479c4..da401cc3 100644 --- a/spec/models/list_spec.rb +++ b/spec/models/list_spec.rb @@ -2,28 +2,84 @@ require 'spec_helper' describe List do - before :each do - @list = create :list - end + let(:supplier) { create :supplier } + let(:user) { create :user } + let(:section) { create :section, supplier: supplier} + let(:table) { create :table, supplier: supplier} + let(:list){ create :list, supplier: supplier, table: table, user_ids: [user.id] } + subject { list } describe :as_json do it 'should include _id in as_json serialization' do - @list.as_json.keys.map(&:to_sym).should include :_id + list.as_json.keys.map(&:to_sym).should include :_id end it 'should include table_number in as_json serialization' do - @list.as_json.keys.should include :table_number + list.as_json.keys.should include :table_number end end describe :mark_as_payed do it "should set payed_at to a time" do - @list.payed_at.should be_nil - @list.mark_as_payed - @list.payed_at.should be_kind_of Time + list.payed_at.should be_nil + list.mark_as_payed + list.payed_at.should be_kind_of Time end it "should set is_payed to true" do - @list.is_payed.should be_false - @list.mark_as_payed - @list.is_payed.should be_true + list.is_payed.should be_false + list.mark_as_payed + list.is_payed.should be_true + end + end + + describe '#move_to_table!' do + let(:new_section) { create :section, supplier: supplier} + let(:new_table) { create :table, supplier: supplier, section: new_section} + before do + @order1 = create :order, supplier: supplier, list: list, section: section + end + + it 'adds a model to keep track of the movement' do + expect{ list.move_to_table! new_table }.to change{ UserTableMove.count }.by(1) + end + + 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 + its(:has_active_orders?) { should be_false } + + it 'returns true when there are placed orders for the list' do + create :order, supplier: supplier, list: list, section: section, state: 'placed' + list.has_active_orders?.should be_true + end + + it 'returns true when there are active orders for the list' do + create :order, supplier: supplier, list: list, section: section, state: 'active' + list.has_active_orders?.should be_true + end + + it 'returns false when there are only delivered orders' do + create :order, supplier: supplier, list: list, section: section, state: 'delivered' + list.has_active_orders?.should be_false + end + + it 'returns false when there are only closed orders' do + create :order, supplier: supplier, list: list, section: section, state: 'closed' + list.has_active_orders?.should be_false + end + + it 'returns false when there are only cancelled orders' do + create :order, supplier: supplier, list: list, section: section, state: 'cancelled' + list.has_active_orders?.should be_false end end diff --git a/spec/models/order_spec.rb b/spec/models/order_spec.rb new file mode 100644 index 00000000..5a428b19 --- /dev/null +++ b/spec/models/order_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' + + +describe Order do + let(:supplier) { create :supplier } + let(:user) { create :user } + let(:section) { create :section, supplier: supplier} + let(:table) { create :table, supplier: supplier} + let(:list){ create :list, supplier: supplier, table: table, user_ids: [user.id] } + let(:order) {create :order, supplier: supplier, list: list, state: 'placed' } + subject { order } + + its(:placed?) { should be_true } + its(:active?) { should be_false } + + describe 'count_active_for_supplier_and_list' do + before { order } + it 'counts active orders for a list with objects' do + Order.count_active_for_supplier_and_list(supplier, list).should == 1 + end + it 'counts active orders for a list with ids' do + Order.count_active_for_supplier_and_list(supplier.id, list.id).should == 1 + end + it 'does not count inactive orders' do + order.close! + Order.count_active_for_supplier_and_list(supplier, list).should == 0 + end + end + +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 377ca999..ed6392d5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -38,7 +38,7 @@ RSpec.configure do |config| # Use color in STDOUT config.color_enabled = true - config.fail_fast = true + config.fail_fast = false # Use color not only in STDOUT but also in pagers and files config.tty = true