diff --git a/.rspec b/.rspec index 4e1e0d2f..7115a783 100644 --- a/.rspec +++ b/.rspec @@ -1 +1,2 @@ ---color +--color --format Fuubar +-r turnip/rspec diff --git a/Gemfile b/Gemfile index 21edcfad..04b07c32 100644 --- a/Gemfile +++ b/Gemfile @@ -57,7 +57,6 @@ gem 'mini_magick' group :development do #gem 'pry-remote' gem 'rspec-rails' - gem 'guard-rspec' gem 'quiet_assets' gem 'letter_opener' gem 'thin' @@ -68,13 +67,12 @@ end group :test do #gem 'steak' gem 'rspec-rails' - gem 'cucumber-rails' - gem 'poltergeist' - gem 'selenium-webdriver' gem 'database_cleaner' + gem 'capybara-webkit' + gem 'turnip' + gem 'launchy' + gem 'fuubar' gem 'rb-fsevent', :require => false #if RUBY_PLATFORM =~ /darwin/i - gem 'ruby_gntp' - gem 'guard-rspec' gem 'factory_girl_rails' gem 'pry-rails' end diff --git a/Gemfile.lock b/Gemfile.lock index d932e9ce..fed6a9c3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -103,12 +103,12 @@ GEM rack (>= 1.0.0) rack-test (>= 0.5.4) xpath (~> 2.0) - childprocess (0.3.9) - ffi (~> 1.0, >= 1.0.11) + capybara-webkit (1.0.0) + capybara (~> 2.0, >= 2.0.2) + json chunky_png (1.2.8) climate_control (0.0.3) activesupport (>= 3.0) - cliver (0.2.1) cocaine (0.5.1) climate_control (>= 0.0.3, < 1.0) coderay (1.0.9) @@ -130,17 +130,6 @@ GEM mime-types (~> 1.15) multi_json (~> 1.0) rest-client (~> 1.6.1) - cucumber (1.3.8) - builder (>= 2.1.2) - diff-lcs (>= 1.1.3) - gherkin (~> 2.12.1) - multi_json (>= 1.7.5, < 2.0) - multi_test (>= 0.0.2) - cucumber-rails (1.4.0) - capybara (>= 1.1.2) - cucumber (>= 1.2.0) - nokogiri (>= 1.5.0) - rails (>= 3.0.0) daemons (1.1.9) database_cleaner (1.1.1) devise (3.1.0) @@ -194,20 +183,13 @@ GEM faye-websocket (0.7.0) eventmachine (>= 0.12.0) websocket-driver (>= 0.3.0) - ffi (1.9.0) - formatador (0.2.4) fssm (0.2.10) + fuubar (1.1.1) + rspec (~> 2.0) + rspec-instafail (~> 0.2.0) + ruby-progressbar (~> 1.0) gherkin (2.12.1) multi_json (~> 1.3) - guard (1.8.2) - formatador (>= 0.2.4) - listen (>= 1.0.0) - lumberjack (>= 1.0.2) - pry (>= 0.9.10) - thor (>= 0.14.6) - guard-rspec (3.0.3) - guard (>= 1.8) - rspec (~> 2.13) haml (4.0.3) tilt haml-rails (0.4) @@ -236,11 +218,6 @@ GEM addressable (~> 2.3) letter_opener (1.1.2) launchy (~> 2.2) - listen (1.3.1) - rb-fsevent (>= 0.9.3) - rb-inotify (>= 0.9) - rb-kqueue (>= 0.2) - lumberjack (1.0.4) mail (2.5.4) mime-types (~> 1.16) treetop (~> 1.4.8) @@ -250,7 +227,6 @@ GEM subexec (~> 0.2.1) mini_portile (0.5.1) multi_json (1.8.0) - multi_test (0.0.2) nokogiri (1.6.0) mini_portile (~> 0.5.0) paperclip (3.5.1) @@ -258,11 +234,6 @@ GEM activesupport (>= 3.0.0) cocaine (~> 0.5.0) mime-types - poltergeist (1.4.1) - capybara (~> 2.1.0) - cliver (~> 0.2.1) - multi_json (~> 1.0) - websocket-driver (>= 0.2.0) polyglot (0.3.3) pry (0.9.12.2) coderay (~> 1.0.5) @@ -298,10 +269,6 @@ GEM thor (>= 0.14.6, < 2.0) rake (10.1.0) rb-fsevent (0.9.3) - rb-inotify (0.9.2) - ffi (>= 0.5.0) - rb-kqueue (0.2.0) - ffi (>= 0.5.0) rdoc (3.12.2) json (~> 1.4) rest-client (1.6.7) @@ -314,6 +281,7 @@ GEM rspec-core (2.14.5) rspec-expectations (2.14.2) diff-lcs (>= 1.1.3, < 2.0) + rspec-instafail (0.2.4) rspec-mocks (2.14.3) rspec-rails (2.14.0) actionpack (>= 3.0) @@ -322,18 +290,12 @@ GEM rspec-core (~> 2.14.0) rspec-expectations (~> 2.14.0) rspec-mocks (~> 2.14.0) - ruby_gntp (0.3.4) - rubyzip (0.9.9) + ruby-progressbar (1.1.1) sass (3.2.10) sass-rails (3.2.6) railties (~> 3.2.0) sass (>= 3.1.10) tilt (~> 1.3) - selenium-webdriver (2.35.1) - childprocess (>= 0.2.5) - multi_json (~> 1.0) - rubyzip (< 1.0.0) - websocket (~> 1.0.4) simple_form (2.1.0) actionpack (~> 3.0) activemodel (~> 3.0) @@ -366,13 +328,15 @@ GEM treetop (1.4.15) polyglot polyglot (>= 0.3.1) + turnip (1.1.0) + gherkin (>= 2.5) + rspec (~> 2.0) tzinfo (0.3.37) uglifier (2.2.1) execjs (>= 0.3.0) multi_json (~> 1.0, >= 1.0.2) warden (1.2.3) rack (>= 1.0) - websocket (1.0.7) websocket-driver (0.3.0) xpath (2.0.0) nokogiri (~> 1.3) @@ -385,11 +349,11 @@ DEPENDENCIES active_decorator bootstrap-sass bourbon + capybara-webkit cmtool! coffee-rails (~> 3.2.1) compass-rails couch_potato! - cucumber-rails database_cleaner devise (= 3.1.0) devise_simply_stored! @@ -398,15 +362,15 @@ DEPENDENCIES emblem-rails factory_girl_rails faye - guard-rspec + fuubar jquery-rails jquery-ui-rails js-routes kaminari + launchy letter_opener mini_magick orm_adapter! - poltergeist pry-rails quiet_assets rack-cors @@ -414,11 +378,10 @@ DEPENDENCIES rb-fsevent rqrcode rspec-rails - ruby_gntp sass-rails (~> 3.2.3) - selenium-webdriver simple_form simply_stored! slim-rails thin + turnip uglifier (>= 1.0.3) diff --git a/app/assets/javascripts/supplier/app/controllers/index_controller.js.coffee b/app/assets/javascripts/supplier/app/controllers/index_controller.js.coffee index c6ae6ded..af559ef3 100644 --- a/app/assets/javascripts/supplier/app/controllers/index_controller.js.coffee +++ b/app/assets/javascripts/supplier/app/controllers/index_controller.js.coffee @@ -17,6 +17,7 @@ Qsupplier.App.IndexController = Ember.ObjectController.extend list = Qsupplier.App.List.find(id) list.set('needs_help', false) $.post '/supplier/mark_list_as_helped', {list_id: id} + closeList: (id)-> list = Qsupplier.App.List.find(id) list.set('state', 'closed') diff --git a/app/assets/javascripts/supplier/app/models/list.js.coffee b/app/assets/javascripts/supplier/app/models/list.js.coffee index 6a59c0d3..ee02b33c 100644 --- a/app/assets/javascripts/supplier/app/models/list.js.coffee +++ b/app/assets/javascripts/supplier/app/models/list.js.coffee @@ -11,3 +11,8 @@ Qsupplier.App.List = DS.Model.extend orders: DS.hasMany('Qsupplier.App.Order') section: DS.belongsTo('Qsupplier.App.Section') section_id: attr('string') + close: -> + @get('orders').forEach (order)-> + order.close() + @set('table', null) + @list.set('state', 'closed') diff --git a/app/assets/javascripts/supplier/app/models/order.js.coffee b/app/assets/javascripts/supplier/app/models/order.js.coffee index 4918c392..b8f84f30 100644 --- a/app/assets/javascripts/supplier/app/models/order.js.coffee +++ b/app/assets/javascripts/supplier/app/models/order.js.coffee @@ -2,7 +2,7 @@ attr = DS.attr Qsupplier.App.Order = DS.Model.extend state: attr('string') list: DS.belongsTo('Qsupplier.App.List') - total_amount: attr('number') + price: attr('number') product_orders: attr('object') section: DS.belongsTo('Qsupplier.App.Section') section_id: attr('string') @@ -13,5 +13,9 @@ Qsupplier.App.Order = DS.Model.extend needs_supplier_attention: (-> @get('state') == 'placed' || @get('state') == 'active').property('state') display: (-> + return '' unless @get('product_orders') @get('product_orders').map((po)-> "#{po.product_name} (#{po.quantity})").join(',') ).property('product_orders') + + close: -> + @set('state', 'closed') diff --git a/app/assets/javascripts/supplier/app/modifications/model_extensions.js.coffee b/app/assets/javascripts/supplier/app/modifications/model_extensions.js.coffee index d4b39678..c9f02432 100644 --- a/app/assets/javascripts/supplier/app/modifications/model_extensions.js.coffee +++ b/app/assets/javascripts/supplier/app/modifications/model_extensions.js.coffee @@ -3,9 +3,25 @@ DS.Model.reopen updated_at: DS.attr('string') DS.Model.reopenClass findCached: (id)-> + return null unless id @all().findProperty('id', id) - updateOrCreate: (attributes)-> + updateOrAdd: (attributes)-> if cached = @findCached(attributes.id) cached.setProperties attributes else - @createRecord(attributes) + @find(attributes.id) + pushByAttriburtes: (attributes)-> + #store = @all().get('store') + + #Ugly hack at the cost of an extra request since I do not yet know the proper + #code for adding a record by its attributes and having its associations set + #@find(attributes.id) + + Ember.get(@, 'relationships').forEach (model, relation)-> + relation = relation[0] + if relation.kind == 'belongsTo' and attributes["#{relation.name}_id"] + attributes[relation.name] = model.find(attributes["#{relation.name}_id"]) + delete attributes["#{relation.name}_id"] + + #store.push @, attributes + @createRecord attributes diff --git a/app/assets/javascripts/supplier/app/router.js.coffee b/app/assets/javascripts/supplier/app/router.js.coffee index 7263149a..5e22a576 100644 --- a/app/assets/javascripts/supplier/app/router.js.coffee +++ b/app/assets/javascripts/supplier/app/router.js.coffee @@ -1,4 +1,5 @@ # For more information see: http://emberjs.com/guides/routing/ +# and for queryParams: https://github.com/alexspeller/website/blob/a96d9afe4506454b155cc64299e86e558ce3c9f1/source/guides/routing/query-params.md Qsupplier.App.Router.reopen location: 'history' rootURL: '/supplier' @@ -6,4 +7,4 @@ Qsupplier.App.Router.reopen Qsupplier.App.Router.map -> @resource 'sections', -> @resource 'section', path: ':section_id' - + @resource 'lists', queryParams: ['state'] diff --git a/app/assets/javascripts/supplier/app/routes/index_route.js.coffee b/app/assets/javascripts/supplier/app/routes/index_route.js.coffee index 2c9edc42..85f76f6d 100644 --- a/app/assets/javascripts/supplier/app/routes/index_route.js.coffee +++ b/app/assets/javascripts/supplier/app/routes/index_route.js.coffee @@ -1,11 +1,20 @@ Qsupplier.App.IndexRoute = Ember.Route.extend - model: -> + model: (params, queryParams)-> + # Preload only active lists and orders + @store.find 'list', state: 'active' + @store.find 'order', state: 'active' Ember.Object.create - lists: Qsupplier.App.List.find({state: 'active'}) - orders: Qsupplier.App.Order.find() + # Find with condition does not work since the resulting array promise is not updated for newly created records + #lists: Qsupplier.App.List.find({state: 'active'}) + #orders: Qsupplier.App.Order.find({state: 'active'}) + #lists: @store.filter 'list', (l)-> l.get('state') == 'active' # DOES NOT WORK!!!! (yet) + # use filter to create a scope on all the records + lists: Qsupplier.App.List.filter -> true + orders: Qsupplier.App.Order.filter -> true setupController: (controller, model)-> controller.set('model', model) $('#section_selector').on 'change', (-> controller.set('sectionId', $(this).val())) + #controller.set 'lists', @store.all('list') #controller.set 'lists', Qsupplier.App.List.all() #.filterProperty('state', 'active') #controller.set 'orders', Qsupplier.App.Order.all() #controller.set 'lists', model.get('lists') diff --git a/app/assets/javascripts/supplier/app/store.js.coffee b/app/assets/javascripts/supplier/app/store.js.coffee index a295d7bc..7731d046 100644 --- a/app/assets/javascripts/supplier/app/store.js.coffee +++ b/app/assets/javascripts/supplier/app/store.js.coffee @@ -1,7 +1,7 @@ # http://emberjs.com/guides/models/defining-a-store/ Qsupplier.App.Store = DS.Store.extend - revision: 11 + revision: 13 adapter: DS.RESTAdapter.create namespace: 'supplier' diff --git a/app/assets/javascripts/supplier/app/templates/active_list.emblem b/app/assets/javascripts/supplier/app/templates/active_list.emblem index bc6c09b3..af9184e3 100644 --- a/app/assets/javascripts/supplier/app/templates/active_list.emblem +++ b/app/assets/javascripts/supplier/app/templates/active_list.emblem @@ -4,11 +4,11 @@ td.list-status if view.content.needs_payment span.list-needs-payment-indicator € td.numeric.table_number {{view.content.table_number}} -td.section_title {{view.content.section_title}} -td.currency.total_list_amount {{currency total_amount}} +td.section_title {{view.content.section.title}} +td.currency.total_list_amount {{currency view.content.price}} td.actions if view.content.needs_help - button.btn.btn-info{ action markListAsHelped view.content.id} {{t 'list.is_helped_button'}} - button.btn.btn-warning{ action closeList view.content.id} {{t 'list.close_list' }} + button.btn.btn-info.mark_list_as_helped{ action markListAsHelped view.content.id} {{t 'list.is_helped_button'}} + button.btn.btn-warning.close_list{ action closeList view.content.id} {{t 'list.close_list' }} a.btn href="/supplier/lists/{{unbound view.content.id}}" span.icon-list diff --git a/app/assets/javascripts/supplier/app/templates/active_order.emblem b/app/assets/javascripts/supplier/app/templates/active_order.emblem index 2f8afabf..015638ac 100644 --- a/app/assets/javascripts/supplier/app/templates/active_order.emblem +++ b/app/assets/javascripts/supplier/app/templates/active_order.emblem @@ -1,7 +1,7 @@ td {{view.content.display}} -td.numeric.table_number {{view.content.table_number}} -td.section_title {{view.content.section_title}} -td.currency {{currency view.content.total_amount}} +td.numeric.table_number {{view.content.list.table.number}} +td.section_title {{view.content.section.title}} +td.currency {{currency view.content.price }} td.actions if view.content.placed button.btn.btn-success{ action markOrderActive view.content.id} {{t 'order.being_processed'}} diff --git a/app/assets/javascripts/supplier/app/templates/section.emblem b/app/assets/javascripts/supplier/app/templates/section.emblem index b35e01dd..b4968039 100644 --- a/app/assets/javascripts/supplier/app/templates/section.emblem +++ b/app/assets/javascripts/supplier/app/templates/section.emblem @@ -17,7 +17,7 @@ view Ember.TextField valueBinding="width" classNames="dimension" span x view Ember.TextField valueBinding="height" classNames="dimension" - a.btn.btn-mini{ action finishEditable} + a.btn.btn-mini{ action finishEditable } span.icon-ok else a.btn.btn-mini{ action makeEditable } diff --git a/app/assets/javascripts/supplier/app/views/active_list_view.js.coffee b/app/assets/javascripts/supplier/app/views/active_list_view.js.coffee index 6e5e900e..38c69d69 100644 --- a/app/assets/javascripts/supplier/app/views/active_list_view.js.coffee +++ b/app/assets/javascripts/supplier/app/views/active_list_view.js.coffee @@ -1,3 +1,5 @@ Qsupplier.App.ActiveListView = Ember.View.extend tagName: 'tr' templateName: 'active_list' + classNameBindings: ['classIdentifier'] + classIdentifier: (-> "list-row-#{@get('content.id')}").property() diff --git a/app/assets/javascripts/supplier/app/views/active_order_view.js.coffee b/app/assets/javascripts/supplier/app/views/active_order_view.js.coffee index ad25dc30..aed7accd 100644 --- a/app/assets/javascripts/supplier/app/views/active_order_view.js.coffee +++ b/app/assets/javascripts/supplier/app/views/active_order_view.js.coffee @@ -1,4 +1,5 @@ Qsupplier.App.ActiveOrderView = Ember.View.extend tagName: 'tr' templateName: 'active_order' - classNameBindings: ['content.active:active', 'content.delivered:delivered'] + classNameBindings: ['content.active:active', 'content.delivered:delivered', 'classIdentifier'] + classIdentifier: (-> "order-row-#{@get('content.id')}").property() diff --git a/app/assets/javascripts/supplier/qsupplier.js.coffee b/app/assets/javascripts/supplier/qsupplier.js.coffee index c3f154ba..304bfa37 100644 --- a/app/assets/javascripts/supplier/qsupplier.js.coffee +++ b/app/assets/javascripts/supplier/qsupplier.js.coffee @@ -4,7 +4,7 @@ root.Qsupplier= faye = new Faye.Client(event_host) faye.subscribe "/supplier/"+supplier_id, (e)=> if(e.event == 'new_order') - Qsupplier.App.Order.create(e.data.order) + Qsupplier.App.Order.pushByAttriburtes(e.data.order) if Qsupplier.App # old stuff body = $('#active-orders-table tbody') order = new Order(e.data.order) @@ -12,14 +12,14 @@ root.Qsupplier= body.append @mustache('#active-order-template', order) $('.section-table-list-'+order.list_id()).addClass('active_order') else if(e.event == 'list_needs_help') - if list = Qsupplier.App.List.findCached(e.data.id) + if Qsupplier.App and list = Qsupplier.App.List.findCached(e.data.id) list.set('needs_help', true) # old stuff $('#list-needs-help-indicator-'+e.data.id).removeClass('hide') $('#list-is-helped-button-'+e.data.id).removeClass('hide') $('.section-table-list-'+e.data.id).addClass('needs_help') else if(e.event == 'list_needs_payment') - if list = Qsupplier.App.List.findCached(e.data.id) + if Qsupplier.App and list = Qsupplier.App.List.findCached(e.data.id) list.set('needs_payment', true) # old stuff $('#list-needs-payment-indicator-'+e.data.id).removeClass('hide') @@ -28,10 +28,10 @@ root.Qsupplier= if list = Qsupplier.App.List.findCached(e.data.id) list.set('needs_payment', false) else if e.event == 'list_update' - Qsupplier.App.List.updateOrCreate(e.data.list) + Qsupplier.App.List.updateOrAdd(e.data.list) if Qsupplier.App # old stuff list = new List(e.data.list) - row = $('#list-row-'+list.id()) + row = $('#active-lists-table .list-row-'+list.id()) content = @mustache('#active-list-template', list) if row.length then row.replaceWith(content) else $('#active-lists-table tbody').append(content) table = $('#section-table-'+list.table_id()) @@ -42,15 +42,14 @@ root.Qsupplier= table.addClass('needs_payment') if list.needs_payment() table.addClass('active_order') if list.has_active_orders() else if e.event == 'list_closed' - if list = Qsupplier.App.List.findCached(e.data.id) - list.set('table', null) - list.set('state', 'closed') - $('#list-row-'+e.data.id).remove() + if Qsupplier.App and list = Qsupplier.App.List.findCached(e.data.id) + list.close() + $('.list-row-'+e.data.id).remove() $('.of-list-'+e.data.id).remove() table_list_class = 'section-table-list-'+e.data.id $('.'+table_list_class).removeClass('occupied needs_help needs_payment active_order').removeClass(table_list_class) else if e.event == 'list_helped' - if list = Qsupplier.App.List.findCached(e.data.id) + if Qsupplier.App and list = Qsupplier.App.List.findCached(e.data.id) list.set('needs_help', false) list_id = e.data.id $('#list-needs-help-indicator-'+list_id).addClass('hide') @@ -58,13 +57,13 @@ root.Qsupplier= $('.section-table-list-'+list_id).removeClass('needs_help') else if e.event == 'order_being_processed' $('#order-in-process-button-'+e.data.id).hide() - $('#order-row-'+e.data.id).removeClass('placed').addClass('active') + $('.order-row-'+e.data.id).removeClass('placed').addClass('active') else if e.event == 'order_being_delivered' - $('#order-row-'+e.data.id).remove() + $('.order-row-'+e.data.id).remove() $('.section-table-list-'+e.data.list_id).removeClass('active_order') else if e.event == 'list_changed_table' list = new List(e.data.list) - list_row = $('#list-row-'+list.id()) + 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-'+list.id()) diff --git a/app/assets/javascripts/user/quser.js.coffee b/app/assets/javascripts/user/quser.js.coffee index 6d16ce09..736f1470 100644 --- a/app/assets/javascripts/user/quser.js.coffee +++ b/app/assets/javascripts/user/quser.js.coffee @@ -23,9 +23,9 @@ class Quser window.active_list.needs_help = false @list_needs_help_default_action() else if(e.event == 'order_being_processed') - $('#order-row-'+e.data.id).addClass('active') + $('.order-row-'+e.data.id).addClass('active') else if(e.event == 'order_being_delivered') - $('#order-row-'+e.data.id).addClass('delivered') + $('.order-row-'+e.data.id).addClass('delivered') else if(e.event == 'list_changed_table') list = new List(e.data.list) $('.table-number').text(list.table_number()) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0355ac8f..4d7a5fc3 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -24,7 +24,7 @@ private end def set_locale - I18n.locale = (params[:locale].presence || :nl).to_sym + I18n.locale = (params[:locale].presence || Rails.configuration.i18n.default_locale).to_sym end def layout_by_resource diff --git a/app/controllers/supplier_controller.rb b/app/controllers/supplier_controller.rb index b3e0269a..f5400d8a 100644 --- a/app/controllers/supplier_controller.rb +++ b/app/controllers/supplier_controller.rb @@ -43,8 +43,8 @@ class SupplierController < ApplicationController ho = {products: []} order_total = 0.0 for product_order in order.product_orders - order_total += (product_order.amount * product_order.price).round(2) - ho[:products] << {name: product_order.product.name, id: product_order.product_id, number: product_order.amount, price: product_order.price} + order_total += (product_order.quantity * product_order.price).round(2) + ho[:products] << {name: product_order.product.name, id: product_order.product_id, number: product_order.quantity, price: product_order.price} end ho[:total_amount] = order_total.round(2) ho[:state] = order.state diff --git a/app/controllers/suppliers/lists_controller.rb b/app/controllers/suppliers/lists_controller.rb index 86eeb131..ece21593 100644 --- a/app/controllers/suppliers/lists_controller.rb +++ b/app/controllers/suppliers/lists_controller.rb @@ -33,7 +33,7 @@ module Suppliers respond_to do |format| format.html {} format.json do - render json: {list: @list.with_orders_as_json} + render json: @list end end end diff --git a/app/controllers/suppliers/orders_controller.rb b/app/controllers/suppliers/orders_controller.rb index 18c86b11..5066246e 100644 --- a/app/controllers/suppliers/orders_controller.rb +++ b/app/controllers/suppliers/orders_controller.rb @@ -6,12 +6,19 @@ module Suppliers if params[:state] == 'active' @orders = Order.active_for_supplier(current_supplier.id) else - @orders = Order.for_supplier(current_supplier, page: params[:page], per_page: params['per_page'] || 10) + @orders = Order.for_supplier(current_supplier, page: params[:page], per_page: params['per_page'] || 25) end respond_to do |format| format.html # index.html.erb - format.json { render json: {orders: @orders.map(&:with_products_as_json)} } + format.json { render json: @orders } + end + end + + def show + @order = current_supplier.find_order(params[:id]) + respond_to do |format| + format.json { render json: @order } end end end diff --git a/app/models/list.rb b/app/models/list.rb index 2f6bd201..24675db5 100644 --- a/app/models/list.rb +++ b/app/models/list.rb @@ -243,12 +243,13 @@ class List end end + # Store the final list price in a property def set_price list_total = 0.0 for order in orders order_total = 0.0 for product_order in order.product_orders - order_total += (product_order.amount * product_order.price).round(2) + order_total += (product_order.quantity * product_order.price).round(2) end list_total += order_total.round(2) end @@ -269,23 +270,23 @@ class List order = Order.create list: self, supplier: supplier, user: user, section_id: section_id return unless order.id loaded_products = self.class.database.load_document products.keys - products.each do |product_id, number| - number = number.to_i + products.each do |product_id, quantity| + quantity = quantity.to_i product = loaded_products.find{|p| p.id == product_id} # to get the price - ProductOrder.create order: order, product_id: product_id, amount: number, price: product.price if number > 0 + ProductOrder.create order: order, product_id: product_id, quantity: quantity, price: product.price if quantity > 0 end set_price save for user_id in user_ids broadcast_user user_id, 'new_order', order: order.with_products_as_json, total_amount: price end - broadcast_supplier supplier.id, 'list_update', list: with_info_as_json + broadcast_supplier supplier.id, 'list_update', active_model_serializer.new(self).as_json broadcast_supplier supplier.id, 'new_order', order: order.with_products_as_json order end def as_json(*args) - super.merge(table_number: table_number, has_active_orders: has_active_orders? ) + super.merge(id: id, table_number: table_number, has_active_orders: has_active_orders? ) end def with_orders_as_json @@ -301,8 +302,8 @@ class List ho[:state] = order.state order_total = 0.0 for product_order in order.product_orders - order_total += (product_order.amount * product_order.price).round(2) - ho[:products] << {name: product_order.product_name, id: product_order.product_id, number: product_order.amount, price: product_order.price} + order_total += (product_order.quantity * product_order.price).round(2) + ho[:products] << {name: product_order.product_name, id: product_order.product_id, number: product_order.quantity, price: product_order.price} end ho[:total_amount] = order_total.round(2) ho[:state] = order.state @@ -343,7 +344,7 @@ class List def with_info_as_json return @with_info_as_json if @with_info_as_json.present? hl = as_json - hl[:total_amount] = orders.inject(0.0){|sum, o| sum + o.product_orders.inject(0.0){|s, po| s + (po.amount * po.price).round(2)}}.round(2) + hl[:total_amount] = orders.inject(0.0){|sum, o| sum + o.product_orders.inject(0.0){|s, po| s + (po.quantity * po.price).round(2)}}.round(2) hl[:section_title] = table.section.try(:title) @with_info_as_json = hl end diff --git a/app/models/order.rb b/app/models/order.rb index a5a10d9d..ccf54fd2 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -1,5 +1,6 @@ class Order include SimplyStored::Couch + include ActiveModel::SerializerSupport property :state, default: 'placed' # placed, active, delivered, cancelled, closed @@ -115,9 +116,9 @@ class Order ho[:product_orders] = [] order_total = 0.0 for product_order in product_orders - order_total += (product_order.amount * product_order.price).round(2) - ho[:products] << {name: product_order.product.name, id: product_order.product_id, number: product_order.amount, price: product_order.price} - ho[:product_orders] << {product_name: product_order.product.name, id: product_order.id, quantity: product_order.amount, price: product_order.price} + order_total += (product_order.quantity * product_order.price).round(2) + ho[:products] << {name: product_order.product.name, id: product_order.product_id, number: product_order.quantity, price: product_order.price} + ho[:product_orders] << {product_name: product_order.product.name, id: product_order.id, quantity: product_order.quantity, price: product_order.price} end ho[:total_amount] = order_total.round(2) @with_products_as_json = ho diff --git a/app/models/product_order.rb b/app/models/product_order.rb index ad4ac49f..aa2ac828 100644 --- a/app/models/product_order.rb +++ b/app/models/product_order.rb @@ -1,7 +1,7 @@ class ProductOrder include SimplyStored::Couch - property :amount, type: Fixnum + property :quantity, type: Fixnum property :price, type: Float belongs_to :product diff --git a/app/models/supplier.rb b/app/models/supplier.rb index 642ee210..a069e679 100644 --- a/app/models/supplier.rb +++ b/app/models/supplier.rb @@ -11,7 +11,7 @@ class Supplier #LOCATION property :lat, type: Float, default: 52.08062426379751 property :lng, type: Float, default: 4.312562942504883 - + #WIFI property :offer_wifi property :wifi_ssid @@ -112,6 +112,12 @@ class Supplier self.devise_mailer.confirmation_instructions(self).deliver end + def find_order(id) + order = Order.find(id) + raise SimplyStored::RecordNotFound unless order.supplier_id == self.id + order + end + def send_creation_notifications SupplierMailer.creation(self).deliver end diff --git a/app/models/user.rb b/app/models/user.rb index 393c83e4..5e7b37dc 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -7,6 +7,8 @@ class User devise :database_authenticatable, :recoverable, :rememberable, :trackable, :token_authenticatable # , :registerable + property :authentication_token + has_and_belongs_to_many :lists, storing_keys: false has_many :orders diff --git a/app/serializers/order_serializer.rb b/app/serializers/order_serializer.rb index 866aadd9..6846d9ac 100644 --- a/app/serializers/order_serializer.rb +++ b/app/serializers/order_serializer.rb @@ -1,4 +1,14 @@ class OrderSerializer < Qwaiter::Serializer embed :ids - attributes :state, :list_id, :section_id + attributes :state, :list_id, :section_id, :product_orders, :price + + def product_orders + @product_orders ||= object.product_orders.include_relation(:product) .map do |product_order| + {product_name: product_order.product.name, id: product_order.id, quantity: product_order.quantity, price: product_order.price} + end + end + + def price + product_orders.inject(0){|sum, po| sum + po[:quantity] * po[:price]}.round(2) + end end diff --git a/app/templates/supplier/_active_list.mustache b/app/templates/supplier/_active_list.mustache index d29c5560..974d3570 100644 --- a/app/templates/supplier/_active_list.mustache +++ b/app/templates/supplier/_active_list.mustache @@ -1,4 +1,4 @@ -