From c38686c820958eecce83979b10289fc9539e0598 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 11 Dec 2014 09:39:19 +1100 Subject: [PATCH] Create new variant overrides --- .../admin/services/status_message.js.coffee | 4 ++-- .../admin/variant_overrides_controller.rb | 23 +++++++++++++++++++ app/models/spree/ability_decorator.rb | 6 ++++- app/models/variant_override_set.rb | 5 ++++ config/routes.rb | 4 +++- spec/features/admin/variant_overrides_spec.rb | 17 ++++++++++++++ spec/models/spree/ability_spec.rb | 21 +++++++++++++---- 7 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 app/models/variant_override_set.rb diff --git a/app/assets/javascripts/admin/services/status_message.js.coffee b/app/assets/javascripts/admin/services/status_message.js.coffee index 914dabb390..aaa55cf339 100644 --- a/app/assets/javascripts/admin/services/status_message.js.coffee +++ b/app/assets/javascripts/admin/services/status_message.js.coffee @@ -2,9 +2,9 @@ angular.module("ofn.admin").factory "StatusMessage", ($timeout) -> new class StatusMessage types: progress: {timeout: false, style: {color: '#ff9906'}} - alert: {timeout: 3000, style: {color: 'grey'}} + alert: {timeout: 5000, style: {color: 'grey'}} notice: {timeout: false, style: {color: 'grey'}} - success: {timeout: 3000, style: {color: '#9fc820'}} + success: {timeout: 5000, style: {color: '#9fc820'}} failure: {timeout: false, style: {color: '#da5354'}} statusMessage: diff --git a/app/controllers/admin/variant_overrides_controller.rb b/app/controllers/admin/variant_overrides_controller.rb index 8d32e62b9d..5f2a2e205d 100644 --- a/app/controllers/admin/variant_overrides_controller.rb +++ b/app/controllers/admin/variant_overrides_controller.rb @@ -13,5 +13,28 @@ module Admin order_cycle_enterprises_per_hub @variant_overrides = VariantOverride.for_hubs(@hubs) end + + + def bulk_update + collection_hash = Hash[params[:variant_overrides].each_with_index.map { |vo, i| [i, vo] }] + vo_set = VariantOverrideSet.new collection_attributes: collection_hash + + # Ensure we're authorised to update all variant overrides + vo_set.collection.each { |vo| authorize! :update, vo } + + if vo_set.save + render json: {} + else + if vo_set.errors.present? + render json: { errors: vo_set.errors }, status: 400 + else + render nothing: true, status: 500 + end + end + end + + + def collection + end end end diff --git a/app/models/spree/ability_decorator.rb b/app/models/spree/ability_decorator.rb index aaabc53be8..2c10e99b04 100644 --- a/app/models/spree/ability_decorator.rb +++ b/app/models/spree/ability_decorator.rb @@ -74,7 +74,11 @@ class AbilityDecorator OpenFoodNetwork::Permissions.new(user).managed_product_enterprises.include? variant.product.supplier end - can [:admin, :index], VariantOverride + can [:admin, :index, :read, :update, :bulk_update], VariantOverride do |vo| + OpenFoodNetwork::Permissions.new(user). + order_cycle_enterprises.is_distributor. + include? vo.hub + end can [:admin, :index, :read, :create, :edit, :update_positions, :destroy], Spree::ProductProperty can [:admin, :index, :read, :create, :edit, :update, :destroy], Spree::Image diff --git a/app/models/variant_override_set.rb b/app/models/variant_override_set.rb new file mode 100644 index 0000000000..cf36ee547b --- /dev/null +++ b/app/models/variant_override_set.rb @@ -0,0 +1,5 @@ +class VariantOverrideSet < ModelSet + def initialize(attributes={}) + super(VariantOverride, VariantOverride.all, nil, attributes) + end +end diff --git a/config/routes.rb b/config/routes.rb index daccbbbe64..23df433a0a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -69,7 +69,9 @@ Openfoodnetwork::Application.routes.draw do get :move_down end - resources :variant_overrides + resources :variant_overrides do + post :bulk_update, on: :collection + end end namespace :api do diff --git a/spec/features/admin/variant_overrides_spec.rb b/spec/features/admin/variant_overrides_spec.rb index c5ea4d6d0b..2775caf76c 100644 --- a/spec/features/admin/variant_overrides_spec.rb +++ b/spec/features/admin/variant_overrides_spec.rb @@ -67,6 +67,23 @@ feature %q{ page.should_not have_content producer2.name page.should_not have_content product2.name end + + it "creates new overrides" do + fill_in "variant-overrides-#{variant.id}-price", with: '777.77' + fill_in "variant-overrides-#{variant.id}-count-on-hand", with: '123' + page.should have_content "Changes to one override remain unsaved." + + expect do + click_button 'Save Changes' + page.should have_content "Changes saved." + end.to change(VariantOverride, :count).by(1) + + vo = VariantOverride.last + vo.variant_id.should == variant.id + vo.hub_id.should == hub.id + vo.price.should == 777.77 + vo.count_on_hand.should == 123 + end end context "with overrides" do diff --git a/spec/models/spree/ability_spec.rb b/spec/models/spree/ability_spec.rb index 2962877b47..dee6f007de 100644 --- a/spec/models/spree/ability_spec.rb +++ b/spec/models/spree/ability_spec.rb @@ -155,10 +155,6 @@ module Spree should_not have_ability([:admin, :index, :read, :edit, :update, :search, :destroy], for: p2.master) end - it "should be able to read/write variant overrides" do - should have_ability([:admin, :index], for: VariantOverride) - end - it "should not be able to access admin actions on orders" do should_not have_ability([:admin], for: Spree::Order) end @@ -243,6 +239,23 @@ module Spree o end + let(:vo1) { create(:variant_override, hub: d1, variant: p1.master) } + let(:vo2) { create(:variant_override, hub: d2, variant: p2.master) } + + describe "variant overrides" do + it "should be able to access variant overrides page" do + should have_ability([:admin, :index, :bulk_update], for: VariantOverride) + end + + it "should be able to read/write their own variant overrides" do + should have_ability([:admin, :index, :read, :update], for: vo1) + end + + it "should not be able to read/write other enterprises' variant overrides" do + should_not have_ability([:admin, :index, :read, :update], for: vo2) + end + end + it "should be able to read/write their enterprises' orders" do should have_ability([:admin, :index, :read, :edit], for: o1) end