From c8d359a0dacdda727365558266d5de7c70faea1a Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Mon, 11 Nov 2019 16:31:04 +0000 Subject: [PATCH] Merge spree/admin/overview_controller with its decorator --- .../spree/admin/overview_controller.rb | 72 +++++++++++++++++- .../admin/overview_controller_decorator.rb | 74 ------------------- 2 files changed, 69 insertions(+), 77 deletions(-) delete mode 100644 app/controllers/spree/admin/overview_controller_decorator.rb diff --git a/app/controllers/spree/admin/overview_controller.rb b/app/controllers/spree/admin/overview_controller.rb index 5dc9a90cda..d8bc38293a 100644 --- a/app/controllers/spree/admin/overview_controller.rb +++ b/app/controllers/spree/admin/overview_controller.rb @@ -2,12 +2,78 @@ module Spree module Admin class OverviewController < Spree::Admin::BaseController - #todo, add rss feed of information that is happening - def index - @users = User.all + @enterprises = Enterprise + .managed_by(spree_current_user) + .order('is_primary_producer ASC, name') + @product_count = Spree::Product.active.managed_by(spree_current_user).count + @order_cycle_count = OrderCycle.active.managed_by(spree_current_user).count + + if first_access + redirect_to enterprises_path + else + render dashboard_view + end end + private + + # Checks whether the user is accessing the admin for the first time + # + # @return [Boolean] + def first_access + outside_referral && incomplete_enterprise_registration? + end + + # Checks whether the request comes from another admin page or not + # + # @return [Boolean] + def outside_referral + !URI(request.referer.to_s).path.match(%r{/admin}) + end + + # Checks that all of the enterprises owned by the current user have a 'sells' + # property specified, which indicates that the registration process has been + # completed + # + # @return [Boolean] + def incomplete_enterprise_registration? + @incomplete_enterprise_registration ||= spree_current_user + .owned_enterprises + .where(sells: 'unspecified') + .exists? + end + + # Returns the appropriate enterprise path for the current user + # + # @return [String] + def enterprises_path + if managed_enterprises.size == 1 + @enterprise = @enterprises.first + main_app.welcome_admin_enterprise_path(@enterprise) + else + main_app.admin_enterprises_path + end + end + + # Returns the appropriate dashboard view for the current user + # + # @return [String] + def dashboard_view + if managed_enterprises.size == 1 + @enterprise = @enterprises.first + :single_enterprise_dashboard + else + :multi_enterprise_dashboard + end + end + + # Returns the list of enterprises the current user is manager of + # + # @return [ActiveRecord::Relation] + def managed_enterprises + spree_current_user.enterprises + end end end end diff --git a/app/controllers/spree/admin/overview_controller_decorator.rb b/app/controllers/spree/admin/overview_controller_decorator.rb deleted file mode 100644 index 548631aaf4..0000000000 --- a/app/controllers/spree/admin/overview_controller_decorator.rb +++ /dev/null @@ -1,74 +0,0 @@ -Spree::Admin::OverviewController.class_eval do - def index - @enterprises = Enterprise - .managed_by(spree_current_user) - .order('is_primary_producer ASC, name') - @product_count = Spree::Product.active.managed_by(spree_current_user).count - @order_cycle_count = OrderCycle.active.managed_by(spree_current_user).count - - if first_access - redirect_to enterprises_path - else - render dashboard_view - end - end - - private - - # Checks whether the user is accessing the admin for the first time - # - # @return [Boolean] - def first_access - outside_referral && incomplete_enterprise_registration? - end - - # Checks whether the request comes from another admin page or not - # - # @return [Boolean] - def outside_referral - !URI(request.referer.to_s).path.match(%r{/admin}) - end - - # Checks that all of the enterprises owned by the current user have a 'sells' - # property specified, which indicates that the registration process has been - # completed - # - # @return [Boolean] - def incomplete_enterprise_registration? - @incomplete_enterprise_registration ||= spree_current_user - .owned_enterprises - .where(sells: 'unspecified') - .exists? - end - - # Returns the appropriate enterprise path for the current user - # - # @return [String] - def enterprises_path - if managed_enterprises.size == 1 - @enterprise = @enterprises.first - main_app.welcome_admin_enterprise_path(@enterprise) - else - main_app.admin_enterprises_path - end - end - - # Returns the appropriate dashboard view for the current user - # - # @return [String] - def dashboard_view - if managed_enterprises.size == 1 - @enterprise = @enterprises.first - :single_enterprise_dashboard - else - :multi_enterprise_dashboard - end - end - - # Returns the list of enterprises the current user is manager of - # - # @return [ActiveRecord::Relation] - def managed_enterprises - spree_current_user.enterprises - end -end