Created
May 15, 2013 11:57
-
-
Save joker1007/5583465 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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