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
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
module InstanceMethods
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
protected
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
def render_locomotive_page
|
2010-07-29 10:46:13 +00:00
|
|
|
if request.fullpath =~ /^\/admin\//
|
2011-11-08 14:34:25 +00:00
|
|
|
render :template => '/locomotive/errors/404', :layout => '/admin/layouts/not_logged_in', :status => :not_found
|
2010-07-29 10:46:13 +00:00
|
|
|
else
|
|
|
|
@page = locomotive_page
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2011-01-06 14:48:41 +00:00
|
|
|
redirect_to(@page.redirect_url) and return if @page.present? && @page.redirect?
|
|
|
|
|
2010-09-21 11:45:34 +00:00
|
|
|
render_no_page_error and return if @page.nil?
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-07-29 10:46:13 +00:00
|
|
|
output = @page.render(locomotive_context)
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-09-02 23:03:20 +00:00
|
|
|
self.prepare_and_set_response(output)
|
2010-07-29 10:46:13 +00:00
|
|
|
end
|
2010-05-30 23:57:33 +00:00
|
|
|
end
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-09-21 11:45:34 +00:00
|
|
|
def render_no_page_error
|
2011-11-08 14:34:25 +00:00
|
|
|
render :template => '/locomotive/errors/no_page', :layout => false
|
2010-09-21 11:45:34 +00:00
|
|
|
end
|
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
def locomotive_page
|
2012-01-26 01:33:39 +00:00
|
|
|
path = (params[:path] || params[:page_path] || request.fullpath).clone # TODO: params[:path] is more consistent
|
2011-07-28 11:51:59 +00:00
|
|
|
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
|
|
|
|
|
2012-01-17 11:05:39 +00:00
|
|
|
path = 'index' if path.blank? || path == '_edit'
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-07-16 20:36:07 +00:00
|
|
|
if path != 'index'
|
|
|
|
dirname = File.dirname(path).gsub(/^\.$/, '') # also look for templatized page path
|
|
|
|
path = [path, File.join(dirname, 'content_type_template').gsub(/^\//, '')]
|
|
|
|
end
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-07-16 20:36:07 +00:00
|
|
|
if page = current_site.pages.any_in(:fullpath => [*path]).first
|
2011-11-04 15:55:51 +00:00
|
|
|
if not page.published? and current_locomotive_account.nil?
|
2010-06-01 00:06:46 +00:00
|
|
|
page = nil
|
2010-07-16 20:36:07 +00:00
|
|
|
else
|
|
|
|
if page.templatized?
|
2011-12-22 23:45:32 +00:00
|
|
|
@content_entry = page.content_type.entries.where(:_slug => File.basename(path.first)).first
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2011-12-22 23:45:32 +00:00
|
|
|
if @content_entry.nil? || (!@content_entry.visible? && current_locomotive_account.nil?) # content instance not found or not visible
|
2010-07-16 20:36:07 +00:00
|
|
|
page = nil
|
|
|
|
end
|
|
|
|
end
|
2010-06-01 00:06:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-01-09 02:08:32 +00:00
|
|
|
page || not_found_page
|
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
|
|
|
def locomotive_context
|
|
|
|
assigns = {
|
|
|
|
'site' => current_site,
|
2010-06-16 14:43:29 +00:00
|
|
|
'page' => @page,
|
2012-01-09 14:49:59 +00:00
|
|
|
'models' => Locomotive::Liquid::Drops::ContentTypes.new,
|
|
|
|
'contents' => Locomotive::Liquid::Drops::ContentTypes.new, # DEPRECATED
|
2011-04-28 23:16:40 +00:00
|
|
|
'current_page' => self.params[:page],
|
2011-06-03 00:25:19 +00:00
|
|
|
'params' => self.params,
|
2011-10-24 15:37:39 +00:00
|
|
|
'path' => request.path,
|
2011-06-28 13:38:13 +00:00
|
|
|
'url' => request.url,
|
2011-07-05 22:33:34 +00:00
|
|
|
'now' => Time.now.utc,
|
2012-01-26 01:33:39 +00:00
|
|
|
'today' => Date.today,
|
|
|
|
'locale' => I18n.locale,
|
|
|
|
'default_locale' => current_site.default_locale.to_s,
|
|
|
|
'locales' => current_site.locales
|
2011-07-28 13:03:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assigns.merge!(Locomotive.config.context_assign_extensions)
|
|
|
|
|
2012-01-09 14:49:59 +00:00
|
|
|
assigns.merge!(flash.to_hash.stringify_keys) # data from public submissions
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-07-16 20:36:07 +00:00
|
|
|
if @page.templatized? # add instance from content type
|
2012-01-09 14:49:59 +00:00
|
|
|
assigns['entry'] = @content_entry
|
2011-12-22 23:45:32 +00:00
|
|
|
assigns[@page.content_type.slug.singularize] = @content_entry # just here to help to write readable liquid code
|
2010-07-16 20:36:07 +00:00
|
|
|
end
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-09-03 23:47:28 +00:00
|
|
|
registers = {
|
|
|
|
:controller => self,
|
|
|
|
:site => current_site,
|
|
|
|
:page => @page,
|
|
|
|
:inline_editor => self.editing_page?,
|
2012-01-26 01:33:39 +00:00
|
|
|
:current_locomotive_account => current_locomotive_account
|
2010-09-03 23:47:28 +00:00
|
|
|
}
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2012-01-26 01:43:55 +00:00
|
|
|
::Liquid::Context.new({}, assigns, registers, false) # pass false to true to enable the re-thrown exception flag
|
2010-05-30 23:57:33 +00:00
|
|
|
end
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-07-09 10:38:50 +00:00
|
|
|
def prepare_and_set_response(output)
|
2010-10-28 23:36:45 +00:00
|
|
|
flash.discard
|
|
|
|
|
2012-01-16 22:59:59 +00:00
|
|
|
response.headers['Content-Type'] = 'text/html; charset=utf-8'
|
|
|
|
response.headers['Editable'] = 'true' unless self.editing_page?
|
2010-07-09 10:38:50 +00:00
|
|
|
|
|
|
|
if @page.with_cache?
|
|
|
|
fresh_when :etag => @page, :last_modified => @page.updated_at.utc, :public => true
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-07-09 10:38:50 +00:00
|
|
|
if @page.cache_strategy != 'simple' # varnish
|
|
|
|
response.cache_control[:max_age] = @page.cache_strategy
|
|
|
|
end
|
|
|
|
end
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2011-08-25 14:11:57 +00:00
|
|
|
render :text => output, :layout => false, :status => page_status unless performed?
|
2011-01-09 02:08:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def not_found_page
|
|
|
|
current_site.pages.not_found.published.first
|
2010-07-22 22:10:40 +00:00
|
|
|
end
|
|
|
|
|
2010-09-02 23:03:20 +00:00
|
|
|
def editing_page?
|
2012-01-09 14:49:59 +00:00
|
|
|
!!@editing
|
2010-09-02 23:03:20 +00:00
|
|
|
end
|
|
|
|
|
2011-01-09 02:08:32 +00:00
|
|
|
def page_status
|
2011-06-20 19:05:12 +00:00
|
|
|
@page.not_found? ? :not_found : :ok
|
2011-01-09 02:08:32 +00:00
|
|
|
end
|
|
|
|
|
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
|
2011-08-11 20:45:46 +00:00
|
|
|
end
|