Last active
July 6, 2019 03:33
-
-
Save kevcha/c5aebe73849bb56a94ed to your computer and use it in GitHub Desktop.
Revisions
-
Kevin Chavanne revised this gist
Jun 13, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,7 @@ The researched behavior is to try to render the `views/admin/projects/show.html. # The solution Use a **ViewResolver** in `/whateveryouwant`. I choosed to put it in `lib/resolvers/admin_views_resolver.rb` : ```ruby class Resolvers::AdminViewsResolver < ::ActionView::FileSystemResolver @@ -42,5 +42,5 @@ end ## What's happening here ? We're telling to Rails to use a this view resolver for our `Admin::BaseController`, which is the parent controller for all our controllers under `Admin` namespace. This will not break anything, because we call `super('app/views')` in the `initialize` function of the resolver. So, Rails will try to find the `views/admin/projects/show.html.erb`, but then, **only if** there is no such view, will call the `find_templates` method from the resolver, which trim `admin` from prefix variable (the prefix variable contains the original path to the template). -
Kevin Chavanne revised this gist
Jun 13, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,7 @@ The researched behavior is to try to render the `views/admin/projects/show.html. # The solution Use a ***ViewResolver*** in `/whateveryouwant`. I choosed to put it in `lib/resolvers/admin_views_resolver.rb` : ```ruby class Resolvers::AdminViewsResolver < ::ActionView::FileSystemResolver @@ -42,5 +42,5 @@ end ## What's happening here ? We're telling to Rails to use a this view resolver for our `Admin::BaseController`, which is the parent controller for all our controllers under `Admin` namespace. This will not break anything, because we call `super('app/views')` in the `initialize` function of the resolver. So, Rails will try to find the `views/admin/projects/show.html.erb`, but then, ***only if*** there is no such view, will call the `find_templates` method from the resolver, which trim `admin` from prefix variable (the prefix variable contains the original path to the template). -
Kevin Chavanne created this gist
Jun 13, 2014 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,46 @@ # The problem Using multiple namespaces will require to have also namespaces views. For exemple, if you have an `Admin` namespace, with a `Admin::ProjectsController` with a `show` action that also exists in non namespaced `ProjecsController`, you cannot render `views/projects/show.html.erv` from `Admin::ProjecsController`. # What we want to do The researched behavior is to try to render the `views/admin/projects/show.html.erb`, and, if it doesn't exist, fallback to `views/projects/show.html.erb` # The solution Use a *ViewResolver* in `/whateveryouwant`. I choosed to put it in `lib/resolvers/admin_views_resolver.rb` : ```ruby class Resolvers::AdminViewsResolver < ::ActionView::FileSystemResolver def initialize super('app/views') end def find_templates(name, prefix, partial, details) prefix = prefix.sub(/^admin\//, '') super(name, prefix, partial, details) end end ``` Then make it autoload in `application/config.rb` : ```ruby # Add lib directory in autoload_paths config.autoload_paths << File.join(config.root, 'lib') ``` And finally, instanciate it in our(s) controller(s) : ```ruby class Admin::BaseController < ApplicationController append_view_path Resolvers::AdminViewsResolver.new end ``` ## What's happening here ? We're telling to Rails to use a this view resolver for our `Admin::BaseController`, which is the parent controller for all our controllers under `Admin` namespace. This will not break anything, because we call `super('app/views')` in the `initialize` function of the resolver. So, Rails will try to find the `views/admin/projects/show.html.erb`, but then, *only if* there is no such view, will call the `find_templates` method from the resolver, which trim `admin` from prefix variable (the prefix variable contains the original path to the template).