Skip to content

Instantly share code, notes, and snippets.

@pacifists
Created November 6, 2011 12:29
Show Gist options
  • Save pacifists/1342811 to your computer and use it in GitHub Desktop.
Save pacifists/1342811 to your computer and use it in GitHub Desktop.
Files needed to render locomotive pages from your own controllers
# config/application.rb
...
module Myliufoto
class Application < Rails::Application
def self.activate
if Rails.env.development?
load File.join(config.root, 'lib', 'locomotive', 'rails', 'render.rb')
else
require File.join(config.root, 'lib', 'locomotive', 'rails', 'render.rb')
end
...
end
....
config.to_prepare &method(:activate).to_proc
end
end
# app/controllers/photos_controller.rb
class PhotosController < ApplicationController
include Locomotive::Routing::SiteDispatcher
def index
@album = current_site.albums.where(:slug => params[:album]).first
render_my_locomotive_page 'portfolio/album'
end
def show
@album = current_site.albums.where(:slug => params[:album]).first
@photo = @album.photos.where(:pos => params[:pos]).first
render_my_locomotive_page 'portfolio/photo'
end
end
# lib/locomotive/rails/render.rb
# Extends the controller to support liquid resource (page, stylesheet, javascript) rendering
ActionController::Base.class_eval do
include Locomotive::Render::InstanceMethods
def render_my_locomotive_page(path)
params[:path] = path
if request.fullpath =~ /^\/admin\//
render :template => '/admin/errors/404', :layout => '/admin/layouts/box', :status => :not_found
else
@page = locomotive_page
redirect_to(@page.redirect_url) and return if @page.present? && @page.redirect?
render_no_page_error and return if @page.nil?
# output = @page.render(locomotive_context)
output = @page.render(my_context)
self.prepare_and_set_response(output)
end
end
def my_context
context = locomotive_context
assigns = self.instance_variables.inject({}) do |h, v|
if !ActionController::Base.protected_instance_variables.include?(v)
h.merge!(v.to_s.gsub('@', '') => self.instance_variable_get(v))
else
h
end
end
context.scopes[0].merge!(assigns)
context
end
end
# config/routes.rb
Myliufoto::Application.routes.draw do
...
match '/portfolio/:album/:pos', :to => 'photos#show'
match '/portfolio/:album', :to => 'photos#index'
...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment