Skip to content

Instantly share code, notes, and snippets.

@kevcha
Last active July 6, 2019 03:33
Show Gist options
  • Save kevcha/c5aebe73849bb56a94ed to your computer and use it in GitHub Desktop.
Save kevcha/c5aebe73849bb56a94ed to your computer and use it in GitHub Desktop.

Revisions

  1. Kevin Chavanne revised this gist Jun 13, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Explanations.md
    Original 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` :
    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).
    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).

  2. Kevin Chavanne revised this gist Jun 13, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Explanations.md
    Original 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` :
    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).
    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).

  3. Kevin Chavanne created this gist Jun 13, 2014.
    46 changes: 46 additions & 0 deletions Explanations.md
    Original 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).