Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring spree_backend base controller to OFN #4512

Merged
merged 8 commits into from
Dec 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop_manual_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ Metrics/LineLength:
- app/controllers/application_controller.rb
- app/controllers/checkout_controller.rb
- app/controllers/spree/admin/adjustments_controller_decorator.rb
- app/controllers/spree/admin/base_controller_decorator.rb
- app/controllers/spree/admin/orders_controller_decorator.rb
- app/controllers/spree/admin/payments_controller_decorator.rb
- app/controllers/spree/credit_cards_controller.rb
Expand Down Expand Up @@ -644,6 +643,7 @@ Metrics/ClassLength:
- app/controllers/admin/subscriptions_controller.rb
- app/controllers/api/products_controller.rb
- app/controllers/checkout_controller.rb
- app/controllers/spree/admin/base_controller.rb
- app/controllers/spree/admin/payment_methods_controller.rb
- app/controllers/spree/admin/users_controller.rb
- app/controllers/spree/orders_controller.rb
Expand Down
142 changes: 142 additions & 0 deletions app/controllers/spree/admin/base_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
module Spree
module Admin
class BaseController < Spree::BaseController
ssl_required

helper 'spree/admin/navigation'
layout '/spree/layouts/admin'

include I18nHelper

before_filter :authorize_admin
before_filter :set_locale
before_filter :warn_invalid_order_cycles, if: :html_request?

# Warn the user when they have an active order cycle with hubs that are not ready
# for checkout (ie. does not have valid shipping and payment methods).
def warn_invalid_order_cycles
distributors = active_distributors_not_ready_for_checkout

return if distributors.empty? || flash[:notice].present?

flash[:notice] = active_distributors_not_ready_for_checkout_message(distributors)
end

# This is in Spree::Core::ControllerHelpers::Auth
# But you can't easily reopen modules in Ruby
def unauthorized
if try_spree_current_user
flash[:error] = t(:authorization_failure)
redirect_to '/unauthorized'
else
store_location
redirect_to root_path(anchor: "login?after_login=#{request.env['PATH_INFO']}")
end
end

protected

def model_class
const_name = controller_name.classify
return "Spree::#{const_name}".constantize if Spree.const_defined?(const_name)

nil
end

def action
params[:action].to_sym
end

def authorize_admin
if respond_to?(:model_class, true) && model_class
record = model_class
else
# This allows specificity for each non-resource controller
# (to be consistent with "authorize_resource :class => false", see https://github.com/ryanb/cancan/blob/60cf6a67ef59c0c9b63bc27ea0101125c4193ea6/lib/cancan/controller_resource.rb#L146)
record = self.class.to_s.
sub("Controller", "").
underscore.split('/').last.singularize.to_sym
end
authorize! :admin, record
authorize! resource_authorize_action, record
end

def resource_authorize_action
action
end

def flash_message_for(object, event_sym)
resource_desc = object.class.model_name.human
resource_desc += " \"#{object.name}\"" if object.respond_to?(:name) && object.name.present?
Spree.t(event_sym, resource: resource_desc)
end

def render_js_for_destroy
render partial: '/spree/admin/shared/destroy'
end

# Index request for JSON needs to pass a CSRF token in order to prevent JSON Hijacking
def check_json_authenticity
return unless request.format.js? || request.format.json?

return unless protect_against_forgery?

auth_token = params[request_forgery_protection_token]
return if auth_token && form_authenticity_token == CGI.unescape(auth_token)

raise(ActionController::InvalidAuthenticityToken)
end

def config_locale
Spree::Backend::Config[:locale]
end

private

def active_distributors_not_ready_for_checkout
ocs = OrderCycle.managed_by(spree_current_user).active
distributors = ocs.includes(:distributors).map(&:distributors).flatten.uniq
Enterprise.where('enterprises.id IN (?)', distributors).not_ready_for_checkout
end

def active_distributors_not_ready_for_checkout_message(distributors)
distributor_names = distributors.map(&:name).join ', '

if distributors.count > 1
I18n.t(:active_distributors_not_ready_for_checkout_message_plural,
distributor_names: distributor_names)
else
I18n.t(:active_distributors_not_ready_for_checkout_message_singular,
distributor_names: distributor_names)
end
end

def html_request?
request.format.html?
end

def json_request?
request.format.json?
end

def render_as_json(data, options = {})
ams_prefix = options.delete :ams_prefix
if [Array, ActiveRecord::Relation].include? data.class
render options.merge(json: data, each_serializer: serializer(ams_prefix))
else
render options.merge(json: data, serializer: serializer(ams_prefix))
end
end

def serializer(ams_prefix)
unless ams_prefix.nil? || ams_prefix_whitelist.include?(ams_prefix.to_sym)
raise "Suffix '#{ams_prefix}' not found in ams_prefix_whitelist for #{self.class.name}."
end

prefix = ams_prefix.andand.classify || ""
name = controller_name.classify
"::Api::Admin::#{prefix}#{name}Serializer".constantize
end
end
end
end
105 changes: 0 additions & 105 deletions app/controllers/spree/admin/base_controller_decorator.rb

This file was deleted.

15 changes: 1 addition & 14 deletions app/controllers/spree/admin/general_settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ def edit
@preferences_general = [:site_name, :default_seo_title, :default_meta_keywords,
:default_meta_description, :site_url, :bugherd_api_key]
@preferences_security = [:allow_ssl_in_production,
:allow_ssl_in_staging, :allow_ssl_in_development_and_test,
:check_for_spree_alerts]
:allow_ssl_in_staging, :allow_ssl_in_development_and_test]
@preferences_currency = [:display_currency, :hide_cents]
end

Expand All @@ -20,18 +19,6 @@ def update

redirect_to edit_admin_general_settings_path
end

def dismiss_alert
return unless request.xhr? && params[:alert_id]

dismissed = Spree::Config[:dismissed_spree_alerts] || ''
Spree::Config.set(dismissed_spree_alerts: dismissed.
split(',').
push(params[:alert_id]).
join(','))
filter_dismissed_alerts
render nothing: true
end
end
end
end
2 changes: 0 additions & 2 deletions app/views/spree/layouts/_admin_body.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
= Spree.t(:loading)
\...

= render :partial => 'spree/admin/shared/alert', :collection => session[:alerts]

%header#header{"data-hook" => ""}
.container
%figure.columns.five{"data-hook" => "logo-wrapper"}
Expand Down
1 change: 0 additions & 1 deletion app/views/spree/layouts/bare_admin.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
.progress-message
= t(:loading)
\...
= render :partial => 'spree/admin/shared/alert', :collection => session[:alerts]

%header#header{"data-hook" => ""}
.container
Expand Down
1 change: 0 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2851,7 +2851,6 @@ See the %{link} to find out more about %{sitename}'s features and to start using
allow_ssl_in_development_and_test: "Allow SSL to be used when in development and test modes"
allow_ssl_in_production: "Allow SSL to be used in production mode"
allow_ssl_in_staging: "Allow SSL to be used in staging mode"
check_for_spree_alerts: "Check for Spree alerts"
currency_decimal_mark: "Currency decimal mark"
currency_settings: "Currency Settings"
currency_symbol_position: Put "currency symbol before or after dollar amount?"
Expand Down
6 changes: 1 addition & 5 deletions config/routes/spree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,7 @@
end

# Configuration section
resource :general_settings do
collection do
post :dismiss_alert
end
end
resource :general_settings
resource :mail_method, :only => [:edit, :update] do
post :testmail, :on => :collection
end
Expand Down