2010-05-30 23:57:33 +00:00
|
|
|
module Locomotive
|
|
|
|
module Render
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
extend ActiveSupport::Concern
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2012-02-09 23:57:57 +00:00
|
|
|
protected
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2012-02-09 23:57:57 +00:00
|
|
|
def render_locomotive_page
|
2012-02-10 09:05:56 +00:00
|
|
|
if request.fullpath =~ /^\/#{Locomotive.mounted_on}\//
|
|
|
|
render :template => '/locomotive/errors/404', :layout => '/locomotive/layouts/not_logged_in', :status => :not_found
|
2012-02-09 23:57:57 +00:00
|
|
|
else
|
|
|
|
@page = locomotive_page
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2012-02-09 23:57:57 +00:00
|
|
|
redirect_to(@page.redirect_url) and return if @page.present? && @page.redirect?
|
2011-01-06 14:48:41 +00:00
|
|
|
|
2012-02-09 23:57:57 +00:00
|
|
|
render_no_page_error and return if @page.nil?
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2012-02-09 23:57:57 +00:00
|
|
|
output = @page.render(locomotive_context)
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2012-02-09 23:57:57 +00:00
|
|
|
self.prepare_and_set_response(output)
|
2010-05-30 23:57:33 +00:00
|
|
|
end
|
2012-02-09 23:57:57 +00:00
|
|
|
end
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2012-02-09 23:57:57 +00:00
|
|
|
def render_no_page_error
|
|
|
|
render :template => '/locomotive/errors/no_page', :layout => false
|
|
|
|
end
|
2010-09-21 11:45:34 +00:00
|
|
|
|
2012-02-09 23:57:57 +00:00
|
|
|
def locomotive_page
|
2012-03-26 14:31:28 +00:00
|
|
|
current_site.fetch_page self.locomotive_page_path, current_locomotive_account.present?
|
2012-02-09 23:57:57 +00:00
|
|
|
end
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2012-02-10 23:39:06 +00:00
|
|
|
def locomotive_page_path
|
|
|
|
path = (params[:path] || params[:page_path] || request.fullpath).clone # TODO: params[:path] is more consistent
|
|
|
|
path = path.split('?').first # take everything before the query string or the lookup fails
|
|
|
|
path.gsub!(/\.[a-zA-Z][a-zA-Z0-9]{2,}$/, '') # remove the page extension
|
|
|
|
path.gsub!(/^\//, '') # remove the leading slash
|
|
|
|
|
|
|
|
path = 'index' if path.blank? || path == '_edit'
|
|
|
|
|
|
|
|
path
|
|
|
|
end
|
|
|
|
|
2012-02-09 23:57:57 +00:00
|
|
|
def locomotive_context
|
|
|
|
assigns = {
|
|
|
|
'site' => current_site,
|
|
|
|
'page' => @page,
|
|
|
|
'models' => Locomotive::Liquid::Drops::ContentTypes.new,
|
|
|
|
'contents' => Locomotive::Liquid::Drops::ContentTypes.new, # DEPRECATED
|
|
|
|
'current_page' => self.params[:page],
|
|
|
|
'params' => self.params,
|
|
|
|
'path' => request.path,
|
2012-03-26 14:31:28 +00:00
|
|
|
'fullpath' => request.fullpath,
|
2012-02-09 23:57:57 +00:00
|
|
|
'url' => request.url,
|
|
|
|
'now' => Time.now.utc,
|
|
|
|
'today' => Date.today,
|
|
|
|
'locale' => I18n.locale,
|
|
|
|
'default_locale' => current_site.default_locale.to_s,
|
2012-04-20 22:54:54 +00:00
|
|
|
'locales' => current_site.locales,
|
2012-05-23 22:03:21 +00:00
|
|
|
'current_user' => Locomotive::Liquid::Drops::CurrentUser.new(current_locomotive_account)
|
2012-02-09 23:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assigns.merge!(Locomotive.config.context_assign_extensions)
|
|
|
|
|
|
|
|
assigns.merge!(flash.to_hash.stringify_keys) # data from public submissions
|
|
|
|
|
|
|
|
if @page.templatized? # add instance from content type
|
2012-03-26 14:31:28 +00:00
|
|
|
assigns['entry'] = @page.content_entry
|
|
|
|
assigns[@page.target_entry_name] = @page.content_entry # just here to help to write readable liquid code
|
2010-05-30 23:57:33 +00:00
|
|
|
end
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2012-02-09 23:57:57 +00:00
|
|
|
registers = {
|
|
|
|
:controller => self,
|
|
|
|
:site => current_site,
|
|
|
|
:page => @page,
|
|
|
|
:inline_editor => self.editing_page?,
|
|
|
|
:current_locomotive_account => current_locomotive_account
|
|
|
|
}
|
2010-10-28 23:36:45 +00:00
|
|
|
|
2012-02-09 23:57:57 +00:00
|
|
|
::Liquid::Context.new({}, assigns, registers, false) # switch from false to true to enable the re-thrown exception flag
|
|
|
|
end
|
2010-07-09 10:38:50 +00:00
|
|
|
|
2012-02-09 23:57:57 +00:00
|
|
|
def prepare_and_set_response(output)
|
|
|
|
flash.discard
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2012-03-22 18:20:31 +00:00
|
|
|
response.headers['Content-Type'] = "#{@page.response_type}; charset=utf-8"
|
2012-03-01 10:07:22 +00:00
|
|
|
response.headers['Editable'] = 'true' unless self.editing_page? || current_locomotive_account.nil?
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2012-02-09 23:57:57 +00:00
|
|
|
if @page.with_cache?
|
|
|
|
fresh_when :etag => @page, :last_modified => @page.updated_at.utc, :public => true
|
2011-01-09 02:08:32 +00:00
|
|
|
|
2012-02-09 23:57:57 +00:00
|
|
|
if @page.cache_strategy != 'simple' # varnish
|
2012-04-16 09:49:04 +00:00
|
|
|
response.headers['Editable'] = ''
|
|
|
|
response.cache_control[:max_age] = @page.cache_strategy
|
2012-02-09 23:57:57 +00:00
|
|
|
end
|
2010-07-22 22:10:40 +00:00
|
|
|
end
|
|
|
|
|
2012-02-09 23:57:57 +00:00
|
|
|
render :text => output, :layout => false, :status => page_status unless performed?
|
|
|
|
end
|
2010-09-02 23:03:20 +00:00
|
|
|
|
2012-02-09 23:57:57 +00:00
|
|
|
def editing_page?
|
|
|
|
!!@editing
|
|
|
|
end
|
2011-01-09 02:08:32 +00:00
|
|
|
|
2012-02-09 23:57:57 +00:00
|
|
|
def page_status
|
|
|
|
@page.not_found? ? :not_found : :ok
|
2010-05-30 23:57:33 +00:00
|
|
|
end
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
end
|
2012-04-20 22:54:54 +00:00
|
|
|
end
|