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\//
|
|
|
|
render :template => "/admin/errors/404", :layout => 'admin/box', :status => 404
|
|
|
|
else
|
|
|
|
@page = locomotive_page
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-07-29 10:46:13 +00:00
|
|
|
redirect_to application_root_url 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-07-29 10:46:13 +00:00
|
|
|
prepare_and_set_response(output)
|
|
|
|
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
|
|
|
def locomotive_page
|
|
|
|
path = request.fullpath.clone
|
|
|
|
path.gsub!(/\.[a-zA-Z][a-zA-Z0-9]{2,}$/, '')
|
|
|
|
path.gsub!(/^\//, '')
|
|
|
|
path = 'index' if path.blank?
|
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
|
2010-06-10 22:07:59 +00:00
|
|
|
if not page.published? and current_admin.nil?
|
2010-06-01 00:06:46 +00:00
|
|
|
page = nil
|
2010-07-16 20:36:07 +00:00
|
|
|
else
|
|
|
|
if page.templatized?
|
|
|
|
@content_instance = page.content_type.contents.where(:_slug => File.basename(path.first)).first
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-07-19 00:09:10 +00:00
|
|
|
if @content_instance.nil? || (!@content_instance.visible? && current_admin.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
|
|
|
|
|
2010-07-16 21:28:44 +00:00
|
|
|
page || current_site.pages.not_found.published.first
|
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,
|
2010-05-30 23:57:33 +00:00
|
|
|
'asset_collections' => Locomotive::Liquid::Drops::AssetCollections.new(current_site),
|
|
|
|
'stylesheets' => Locomotive::Liquid::Drops::Stylesheets.new(current_site),
|
|
|
|
'javascripts' => Locomotive::Liquid::Drops::Javascripts.new(current_site),
|
2010-07-22 22:10:40 +00:00
|
|
|
'theme_images' => Locomotive::Liquid::Drops::ThemeImages.new(current_site),
|
2010-05-30 23:57:33 +00:00
|
|
|
'contents' => Locomotive::Liquid::Drops::Contents.new(current_site),
|
|
|
|
'current_page' => self.params[:page]
|
|
|
|
}
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-07-16 20:36:07 +00:00
|
|
|
if @page.templatized? # add instance from content type
|
|
|
|
assigns['content_instance'] = @content_instance
|
|
|
|
assigns[@page.content_type.slug.singularize] = @content_instance # just here to help to write readable liquid code
|
|
|
|
end
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-06-16 14:43:29 +00:00
|
|
|
registers = { :controller => self, :site => current_site, :page => @page }
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
::Liquid::Context.new(assigns, registers)
|
|
|
|
end
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-07-09 10:38:50 +00:00
|
|
|
def prepare_and_set_response(output)
|
2010-06-16 14:43:29 +00:00
|
|
|
response.headers['Content-Type'] = 'text/html; charset=utf-8'
|
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
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
render :text => output, :layout => false, :status => :ok
|
2010-07-22 22:10:40 +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
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|