-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move contents of Spree::Core::ControllerHelpers module into Spree::Ba…
…seController This is so that the methods can be more easily overriden. Methods provided by a module cannot be overriden on what they are included into. For example, if Spree::BaseController kept including ControllerHelpers which was defining a spree_current_user method and an authentication extension attempted to override this method, it would not work. The method that would be called would be the dummy one included in ControllerHelpers. It never made sense to me to have ControllerHelpers only being included in one place. Therefore, let's just throw everything in the BaseController so it's more obvious to people where things come from. This will probably fix the issue where spree_current_user is nil on custom-auth apps
- Loading branch information
Showing
3 changed files
with
154 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,157 @@ | ||
class Spree::BaseController < ApplicationController | ||
include Spree::Core::ControllerHelpers | ||
include Spree::Core::RespondWith | ||
|
||
layout :get_layout | ||
before_filter :set_user_language | ||
before_filter :set_current_order | ||
|
||
helper_method :title | ||
helper_method :title= | ||
helper_method :accurate_title | ||
helper_method :get_taxonomies | ||
helper_method :current_order | ||
|
||
include SslRequirement | ||
include Spree::Core::CurrentOrder | ||
|
||
rescue_from CanCan::AccessDenied do |exception| | ||
return unauthorized | ||
end | ||
|
||
def access_forbidden | ||
render :text => 'Access Forbidden', :layout => true, :status => 401 | ||
end | ||
|
||
# can be used in views as well as controllers. | ||
# e.g. <% title = 'This is a custom title for this view' %> | ||
attr_writer :title | ||
|
||
def title | ||
title_string = @title.present? ? @title : accurate_title | ||
if title_string.present? | ||
if Spree::Config[:always_put_site_name_in_title] | ||
[default_title, title_string].join(' - ') | ||
else | ||
title_string | ||
end | ||
else | ||
default_title | ||
end | ||
end | ||
|
||
protected | ||
|
||
def set_current_order | ||
if spree_current_user | ||
last_incomplete_order = spree_current_user.last_incomplete_spree_order | ||
if session[:order_id].nil? && last_incomplete_order | ||
session[:order_id] = last_incomplete_order.id | ||
elsif current_order && last_incomplete_order && current_order != last_incomplete_order | ||
current_order.merge!(last_incomplete_order) | ||
end | ||
end | ||
end | ||
|
||
# Needs to be overriden so that we use Spree's Ability rather than anyone else's. | ||
def current_ability | ||
@current_ability ||= Spree::Ability.new(spree_current_user) | ||
end | ||
|
||
def store_location | ||
# disallow return to login, logout, signup pages | ||
authentication_routes = [:spree_signup_path, :spree_login_path, :spree_logout_path] | ||
disallowed_urls = [] | ||
authentication_routes.each do |route| | ||
if respond_to?(route) | ||
disallowed_urls << send(route) | ||
end | ||
end | ||
|
||
disallowed_urls.map!{ |url| url[/\/\w+$/] } | ||
unless disallowed_urls.include?(request.fullpath) | ||
session['user_return_to'] = request.fullpath.gsub('//', '/') | ||
end | ||
end | ||
|
||
# Redirect as appropriate when an access request fails. The default action is to redirect to the login screen. | ||
# Override this method in your controllers if you want to have special behavior in case the user is not authorized | ||
# to access the requested action. For example, a popup window might simply close itself. | ||
def unauthorized | ||
respond_to do |format| | ||
format.html do | ||
if spree_current_user | ||
flash.now[:error] = t(:authorization_failure) | ||
render 'spree/shared/unauthorized', :layout => '/spree/layouts/spree_application', :status => 401 | ||
else | ||
store_location | ||
url = respond_to?(:spree_login_path) ? spree_login_path : root_path | ||
redirect_to url | ||
end | ||
end | ||
format.xml do | ||
request_http_basic_authentication 'Web Password' | ||
end | ||
format.json do | ||
render :text => "Not Authorized \n", :status => 401 | ||
end | ||
end | ||
end | ||
|
||
def default_title | ||
Spree::Config[:site_name] | ||
end | ||
|
||
# this is a hook for subclasses to provide title | ||
def accurate_title | ||
Spree::Config[:default_seo_title] | ||
end | ||
|
||
def render_404(exception = nil) | ||
respond_to do |type| | ||
type.html { render :status => :not_found, :file => "#{::Rails.root}/public/404", :formats => [:html], :layout => nil} | ||
type.all { render :status => :not_found, :nothing => true } | ||
end | ||
end | ||
|
||
# Convenience method for firing instrumentation events with the default payload hash | ||
def fire_event(name, extra_payload = {}) | ||
ActiveSupport::Notifications.instrument(name, default_notification_payload.merge(extra_payload)) | ||
end | ||
|
||
# Creates the hash that is sent as the payload for all notifications. Specific notifications will | ||
# add additional keys as appropriate. Override this method if you need additional data when | ||
# responding to a notification | ||
def default_notification_payload | ||
{:user => spree_current_user, :order => current_order} | ||
end | ||
|
||
private | ||
|
||
def redirect_back_or_default(default) | ||
redirect_to(session["user_return_to"] || default) | ||
session["user_return_to"] = nil | ||
end | ||
|
||
def get_taxonomies | ||
@taxonomies ||= Taxonomy.includes(:root => :children).joins(:root) | ||
end | ||
|
||
def set_user_language | ||
locale = session[:locale] | ||
locale ||= Spree::Config[:default_locale] unless Spree::Config[:default_locale].blank? | ||
locale ||= Rails.application.config.i18n.default_locale | ||
locale ||= I18n.default_locale unless I18n.available_locales.include?(locale.to_sym) | ||
I18n.locale = locale.to_sym | ||
end | ||
|
||
# Returns which layout to render. | ||
# | ||
# You can set the layout you want to render inside your Spree configuration with the +:layout+ option. | ||
# | ||
# Default layout is: +app/views/spree/layouts/spree_application+ | ||
# | ||
def get_layout | ||
layout ||= Spree::Config[:layout] | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters