2011-10-30 23:02:41 +00:00
|
|
|
module Locomotive
|
2010-07-13 00:46:17 +00:00
|
|
|
class BaseController < InheritedResources::Base
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
include Locomotive::Routing::SiteDispatcher
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-10-30 23:02:41 +00:00
|
|
|
layout '/locomotive/layouts/application'
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-10-30 23:02:41 +00:00
|
|
|
before_filter :require_account
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
before_filter :require_site
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
before_filter :validate_site_membership
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-08-27 17:14:02 +00:00
|
|
|
load_and_authorize_resource
|
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
before_filter :set_locale
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-09-17 14:23:43 +00:00
|
|
|
before_filter :set_current_thread_variables
|
|
|
|
|
2011-06-25 16:25:31 +00:00
|
|
|
helper_method :sections, :current_site_url, :site_url, :page_url, :current_ability
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-06-12 01:13:58 +00:00
|
|
|
# https://rails.lighthouseapp.com/projects/8994/tickets/1905-apphelpers-within-plugin-not-being-mixed-in
|
2011-11-03 13:01:08 +00:00
|
|
|
helper Locomotive::BaseHelper #, Locomotive::BoxHelper
|
2011-10-31 23:44:23 +00:00
|
|
|
# Dir[File.dirname(__FILE__) + "/../../helpers/**/*_helper.rb"].each do |file|
|
|
|
|
# helper "locomotive/#{File.basename(file, '.rb').gsub(/_helper$/, '')}"
|
|
|
|
# end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-13 00:46:17 +00:00
|
|
|
self.responder = Locomotive::AdminResponder # custom responder
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-10-30 23:02:41 +00:00
|
|
|
defaults :route_prefix => 'locomotive'
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-13 00:46:17 +00:00
|
|
|
respond_to :html
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-06-25 16:25:31 +00:00
|
|
|
rescue_from CanCan::AccessDenied do |exception|
|
2011-07-26 19:20:03 +00:00
|
|
|
::Locomotive.log "[CanCan::AccessDenied] #{exception.inspect}"
|
2011-06-25 16:25:31 +00:00
|
|
|
|
|
|
|
if request.xhr?
|
|
|
|
render :json => { :error => exception.message }
|
|
|
|
else
|
|
|
|
flash[:alert] = exception.message
|
|
|
|
|
2011-10-31 23:44:23 +00:00
|
|
|
redirect_to pages_url
|
2011-06-25 16:25:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
protected
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-09-17 14:23:43 +00:00
|
|
|
def set_current_thread_variables
|
2011-11-04 15:55:51 +00:00
|
|
|
Thread.current[:account] = current_locomotive_account
|
2011-09-17 14:23:43 +00:00
|
|
|
Thread.current[:site] = current_site
|
|
|
|
end
|
|
|
|
|
2011-06-25 16:25:31 +00:00
|
|
|
def current_ability
|
2011-11-04 15:55:51 +00:00
|
|
|
@current_ability ||= Ability.new(current_locomotive_account, current_site)
|
2011-06-25 16:25:31 +00:00
|
|
|
end
|
|
|
|
|
2011-10-30 23:02:41 +00:00
|
|
|
def require_account
|
2011-11-04 15:55:51 +00:00
|
|
|
authenticate_locomotive_account!
|
2011-05-28 17:52:36 +00:00
|
|
|
end
|
|
|
|
|
2010-07-13 00:46:17 +00:00
|
|
|
def begin_of_association_chain
|
|
|
|
current_site
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
def self.sections(main, sub = nil)
|
|
|
|
before_filter do |c|
|
|
|
|
sub = sub.call(c) if sub.respond_to?(:call)
|
|
|
|
sections = { :main => main, :sub => sub }
|
|
|
|
c.instance_variable_set(:@admin_sections, sections)
|
|
|
|
end
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
def sections(key = nil)
|
|
|
|
if !key.nil? && key.to_sym == :sub
|
|
|
|
@admin_sections[:sub] || self.controller_name.dasherize
|
2010-07-23 20:09:54 +00:00
|
|
|
else
|
2010-05-10 22:39:52 +00:00
|
|
|
@admin_sections[:main]
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|
2010-04-24 00:32:36 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
def set_locale
|
2011-11-04 15:55:51 +00:00
|
|
|
I18n.locale = current_locomotive_account.locale rescue Locomotive.config.default_locale
|
2010-05-10 22:39:52 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-04-24 23:21:38 +00:00
|
|
|
# ___ site/page urls builder ___
|
|
|
|
|
|
|
|
def current_site_url
|
|
|
|
request.protocol + request.host_with_port
|
|
|
|
end
|
|
|
|
|
|
|
|
def site_url(site, options = {})
|
|
|
|
options = { :fullpath => true, :protocol => true }.merge(options)
|
|
|
|
|
|
|
|
url = "#{site.subdomain}.#{Locomotive.config.domain}"
|
|
|
|
url += ":#{request.port}" if request.port != 80
|
|
|
|
|
|
|
|
url = File.join(url, request.fullpath) if options[:fullpath]
|
|
|
|
url = "http://#{url}" if options[:protocol]
|
|
|
|
url
|
|
|
|
end
|
|
|
|
|
|
|
|
def page_url(page, options = {})
|
|
|
|
if content = options.delete(:content)
|
|
|
|
File.join(current_site_url, page.fullpath.gsub('content_type_template', ''), content._slug)
|
|
|
|
else
|
|
|
|
File.join(current_site_url, page.fullpath)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|