engine/lib/locomotive/action_controller/helpers.rb

66 lines
1.8 KiB
Ruby

module Locomotive
module ActionController
module Helpers
extend ActiveSupport::Concern
included do
helper_method :current_site_public_url, :switch_to_site_url, :public_page_url
end
module InstanceMethods
def set_locale
I18n.locale = current_locomotive_account.locale rescue Locomotive.config.default_locale
end
def sections(key = nil)
if !key.nil? && key.to_sym == :sub
@locomotive_sections[:sub] || self.controller_name.dasherize
else
@locomotive_sections[:main]
end
end
# ___ site/page urls builder ___
def current_site_public_url
request.protocol + request.host_with_port
end
def switch_to_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 public_page_url(page, options = {})
if content = options.delete(:content)
File.join(current_site_public_url, page.fullpath.gsub('content_type_template', ''), content._slug)
else
File.join(current_site_public_url, page.fullpath)
end
end
end
module ClassMethods
def 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(:@locomotive_sections, sections)
end
end
end
end
end
end