Created
July 29, 2012 11:29
-
-
Save asanghi/3197784 to your computer and use it in GitHub Desktop.
Action View Resolver -- Company scoped
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 CarsController < ApplicationController | |
def index | |
@user = load_user # Load the user | |
prepend_view_path @user.resolver # Get the resolver | |
end | |
end |
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 User < ActiveRecord::Base | |
attr_accessible :name | |
has_many :message_templates, dependent: :destroy | |
def resolver | |
# get an action view resolver for the itself # perhaps move this to a helper? | |
@resolver ||= UserViewResolver.new(self) | |
end | |
end |
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 UserViewResolver < ActionView::Resolver | |
attr_accessor :document, :user | |
def initialize(user) | |
@user = user | |
super() | |
end | |
def find_templates(name, prefix, partial, details) | |
search_args = details.merge({ :name => name, :prefix => prefix, :partial => partial, :user_id => user.id }) | |
MessageTemplate.where(search_args).all.map do |d| | |
ActionView::Template.new(*template_args(d)) | |
end | |
end | |
def template_args(document) | |
args = [document.source] | |
args << "view_template-#{document.id}-#{document.prefix}-#{document.name}" | |
args << ::ActionView::Template.registered_template_handler(document.handlers) | |
args << { :format => Mime[document.formats], :virtual_path => virtual_path(document) } | |
end | |
def virtual_path(document) | |
virtual = "#{document.prefix.to_s}/" | |
virtual << "_" if document.partial | |
virtual << document.name.to_s | |
end | |
end |
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 MessageTemplate < ActiveRecord::Base | |
belongs_to :user | |
attr_accessible :formats, :handlers, :locale, :name, :partial, :prefix, :source | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment