Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of git://github.com/resolve/refinerycms
- Loading branch information
Showing
5 changed files
with
102 additions
and
84 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
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
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
160 changes: 85 additions & 75 deletions
160
vendor/refinerycms/core/lib/refinery/application_controller.rb
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,101 +1,111 @@ | ||
require 'action_controller' | ||
|
||
class Refinery::ApplicationController < ActionController::Base | ||
module Refinery::ApplicationController | ||
|
||
helper_method :home_page?, | ||
:local_request?, | ||
:just_installed?, | ||
:from_dialog?, | ||
:admin?, | ||
:login? | ||
def self.included(controller) | ||
controller.send :include, InstanceMethods | ||
controller.send :include, ClassMethods | ||
end | ||
|
||
protect_from_forgery # See ActionController::RequestForgeryProtection | ||
module ClassMethods | ||
def self.included(c) # Extend controller | ||
c.helper_method :home_page?, | ||
:local_request?, | ||
:just_installed?, | ||
:from_dialog?, | ||
:admin?, | ||
:login? | ||
|
||
include Crud # basic create, read, update and delete methods | ||
include AuthenticatedSystem | ||
c.protect_from_forgery # See ActionController::RequestForgeryProtection | ||
|
||
before_filter :find_pages_for_menu, | ||
:show_welcome_page? | ||
c.send :include, Crud # basic create, read, update and delete methods | ||
c.send :include, AuthenticatedSystem | ||
|
||
after_filter :store_current_location!, | ||
:if => Proc.new {|c| c.send(:refinery_user?) rescue false } | ||
c.send :before_filter, :find_pages_for_menu, | ||
:show_welcome_page? | ||
|
||
rescue_from ActiveRecord::RecordNotFound, | ||
ActionController::UnknownAction, | ||
ActionView::MissingTemplate, | ||
:with => :error_404 | ||
c.send :after_filter, :store_current_location!, | ||
:if => Proc.new {|c| c.send(:refinery_user?) rescue false } | ||
|
||
def admin? | ||
controller_name =~ %r{^admin/} | ||
c.send :rescue_from, ActiveRecord::RecordNotFound, | ||
ActionController::UnknownAction, | ||
ActionView::MissingTemplate, | ||
:with => :error_404 | ||
end | ||
end | ||
|
||
def error_404(exception=nil) | ||
if (@page = Page.where(:menu_match => "^/404$").includes(:parts, :slugs).first).present? | ||
# render the application's custom 404 page with layout and meta. | ||
render :template => "/pages/show", | ||
:format => 'html', | ||
:status => 404 | ||
else | ||
# fallback to the default 404.html page. | ||
file = Rails.root.join('public', '404.html') | ||
file = Refinery.root.join('vendor', 'refinerycms', 'core', 'public', '404.html') unless file.exist? | ||
render :file => file.cleanpath.to_s, | ||
:layout => false, | ||
:status => 404 | ||
module InstanceMethods | ||
def admin? | ||
controller_name =~ %r{^admin/} | ||
end | ||
end | ||
|
||
def from_dialog? | ||
params[:dialog] == "true" or params[:modal] == "true" | ||
end | ||
def error_404(exception=nil) | ||
if (@page = Page.where(:menu_match => "^/404$").includes(:parts, :slugs).first).present? | ||
# render the application's custom 404 page with layout and meta. | ||
render :template => "/pages/show", | ||
:format => 'html', | ||
:status => 404 | ||
else | ||
# fallback to the default 404.html page. | ||
file = Rails.root.join('public', '404.html') | ||
file = Refinery.root.join('vendor', 'refinerycms', 'core', 'public', '404.html') unless file.exist? | ||
render :file => file.cleanpath.to_s, | ||
:layout => false, | ||
:status => 404 | ||
end | ||
end | ||
|
||
def home_page? | ||
root_url(:only_path => true) == request.path | ||
end | ||
def from_dialog? | ||
params[:dialog] == "true" or params[:modal] == "true" | ||
end | ||
|
||
def just_installed? | ||
Role[:refinery].users.empty? | ||
end | ||
def home_page? | ||
root_url(:only_path => true) == request.path | ||
end | ||
|
||
def local_request? | ||
Rails.env.development? or request.remote_ip =~ /(::1)|(127.0.0.1)|((192.168).*)/ | ||
end | ||
def just_installed? | ||
Role[:refinery].users.empty? | ||
end | ||
|
||
def login? | ||
(controller_name =~ /^(user|session)(|s)/ and not admin?) or just_installed? | ||
end | ||
def local_request? | ||
Rails.env.development? or request.remote_ip =~ /(::1)|(127.0.0.1)|((192.168).*)/ | ||
end | ||
|
||
protected | ||
def login? | ||
(controller_name =~ /^(user|session)(|s)/ and not admin?) or just_installed? | ||
end | ||
|
||
# get all the pages to be displayed in the site menu. | ||
def find_pages_for_menu | ||
@menu_pages = Page.top_level | ||
end | ||
protected | ||
|
||
# use a different model for the meta information. | ||
def present(model) | ||
presenter = (Object.const_get("#{model.class}Presenter") rescue ::Refinery::BasePresenter) | ||
@meta = presenter.new(model) | ||
end | ||
# get all the pages to be displayed in the site menu. | ||
def find_pages_for_menu | ||
@menu_pages = Page.top_level | ||
end | ||
|
||
# this hooks into the Rails render method. | ||
def render(action = nil, options = {}, &blk) | ||
present(@page) unless admin? or @meta.present? | ||
super | ||
end | ||
# use a different model for the meta information. | ||
def present(model) | ||
presenter = (Object.const_get("#{model.class}Presenter") rescue ::Refinery::BasePresenter) | ||
@meta = presenter.new(model) | ||
end | ||
|
||
def show_welcome_page? | ||
render :template => "/welcome", :layout => "login" if just_installed? and controller_name != "users" | ||
end | ||
# this hooks into the Rails render method. | ||
def render(action = nil, options = {}, &blk) | ||
present(@page) unless admin? or @meta.present? | ||
super | ||
end | ||
|
||
private | ||
def store_current_location! | ||
if admin? | ||
# ensure that we don't redirect to AJAX or POST/PUT/DELETE urls | ||
session[:refinery_return_to] = request.path if request.get? and !request.xhr? and !from_dialog? | ||
elsif defined?(@page) and @page.present? | ||
session[:website_return_to] = @page.url | ||
def show_welcome_page? | ||
render :template => "/welcome", :layout => "login" if just_installed? and controller_name != "users" | ||
end | ||
end | ||
|
||
private | ||
def store_current_location! | ||
if admin? | ||
# ensure that we don't redirect to AJAX or POST/PUT/DELETE urls | ||
session[:refinery_return_to] = request.path if request.get? and !request.xhr? and !from_dialog? | ||
elsif defined?(@page) and @page.present? | ||
session[:website_return_to] = @page.url | ||
end | ||
end | ||
end | ||
end |
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