Skip to content

Instantly share code, notes, and snippets.

@joker1007
Created May 15, 2013 11:57
Show Gist options
  • Save joker1007/5583465 to your computer and use it in GitHub Desktop.
Save joker1007/5583465 to your computer and use it in GitHub Desktop.
class SampleResolver < ActionView::Resolver
include Singleton
def find_templates(name, prefix, partial, details)
scope = details[:database] # ActiveRecord::Relation
scope.map(&:to_template)
end
end
class Template < ActiveRecord::Base
def to_template
handler = ActionView::Template.handler_for_extension(File.extname(path))
Template.new(source, path, handler, {
virtual_path: "template/#{path}",
updated_at: updated_at,
format: "html"
})
end
end
class ApplicationController
before_filter :prepend_database_template
before_filter :register_database_detail
protected
def prepend_database_template
prepend_view_path SampleResolver.instance
end
def register_database_detail
lookup_context.class.register_detail(:database) { nil }
end
end
class PostsController < ApplicationController
def show
@post = Post.find(params[:id])
render database: Template.order("created_at desc")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment