From c695bafe694d8f4df109da288d908fad1da380ff Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Thu, 21 Nov 2013 16:45:59 +1100 Subject: [PATCH] Reworking the way the Reports works so we have more granular control over what shows --- .../admin/reports_controller_decorator.rb | 25 ++++++++++++++----- spec/features/admin/reports_spec.rb | 21 ++++++++++++++++ .../request/authentication_workflow.rb | 5 +++- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb index b924ddae84..73c5752233 100644 --- a/app/controllers/spree/admin/reports_controller_decorator.rb +++ b/app/controllers/spree/admin/reports_controller_decorator.rb @@ -19,12 +19,11 @@ Spree::Admin::ReportsController.class_eval do render_to_string(partial: 'products_and_inventory_description', layout: false, locals: {report_types: REPORT_TYPES[:products_and_inventory]}).html_safe } } } - Spree::Admin::ReportsController::AVAILABLE_REPORTS.merge!({:orders_and_distributors => {:name => "Orders And Distributors", :description => "Orders with distributor details"}}) - Spree::Admin::ReportsController::AVAILABLE_REPORTS.merge!({:bulk_coop => {:name => "Bulk Co-Op", :description => "Reports for Bulk Co-Op orders"}}) - Spree::Admin::ReportsController::AVAILABLE_REPORTS.merge!({:payments => {:name => "Payment Reports", :description => "Reports for Payments"}}) - Spree::Admin::ReportsController::AVAILABLE_REPORTS.merge!({:orders_and_fulfillment => {:name => "Orders & Fulfillment Reports", :description => ''}}) - Spree::Admin::ReportsController::AVAILABLE_REPORTS.merge!({:products_and_inventory => {:name => "Products & Inventory", :description => ''}}) - + # OVERRIDING THIS so we use a method not a constant for available reports + def index + @reports = available_reports + respond_with(@reports) + end REPORT_TYPES = { orders_and_fulfillment: [ @@ -569,4 +568,18 @@ Spree::Admin::ReportsController.class_eval do @suppliers = my_suppliers | suppliers_of_products_I_distribute @order_cycles = OrderCycle.active_or_complete.accessible_by(spree_current_user).order('orders_close_at DESC') end + + def available_reports + reports = { + :orders_and_distributors => {:name => "Orders And Distributors", :description => "Orders with distributor details"}, + :bulk_coop => {:name => "Bulk Co-Op", :description => "Reports for Bulk Co-Op orders"}, + :payments => {:name => "Payment Reports", :description => "Reports for Payments"}, + :orders_and_fulfillment => {:name => "Orders & Fulfillment Reports", :description => ''}, + :products_and_inventory => {:name => "Products & Inventory", :description => ''} + } + if spree_current_user.has_spree_role? 'admin' + reports[:sales_total] = { :name => "Sales Total", :description => "Sales Total For All Orders" } + end + reports + end end diff --git a/spec/features/admin/reports_spec.rb b/spec/features/admin/reports_spec.rb index f29b09c5a3..8e51a9a727 100644 --- a/spec/features/admin/reports_spec.rb +++ b/spec/features/admin/reports_spec.rb @@ -7,6 +7,27 @@ feature %q{ include AuthenticationWorkflow include WebHelper + context "Permissions for different reports" do + context "As an enterprise user" do + let(:user) do + create_enterprise_user([ + create(:distributor_enterprise) + ]) + end + it "should not show the Sales Total report" do + login_to_admin_as user + click_link "Reports" + page.should_not have_content "Sales Total" + end + end + context "As an admin user" do + it "shows the Sales Total report" do + login_to_admin_section + click_link "Reports" + page.should have_content "Sales Total" + end + end + end scenario "orders and distributors report" do login_to_admin_section diff --git a/spec/support/request/authentication_workflow.rb b/spec/support/request/authentication_workflow.rb index 65428a60e3..850157e2a0 100644 --- a/spec/support/request/authentication_workflow.rb +++ b/spec/support/request/authentication_workflow.rb @@ -14,9 +14,12 @@ module AuthenticationWorkflow login_to_admin_as admin_user end - def create_enterprise_user + def create_enterprise_user(enterprises = []) new_user = create(:user, email: 'enterprise@hub.com', password: 'blahblah', :password_confirmation => 'blahblah', ) new_user.spree_roles = [] # for some reason unbeknown to me, this new user gets admin permissions by default. + for enterprise in enterprises do + new_user.enterprise_roles.build(enterprise: enterprise).save + end new_user.save new_user end