Skip to content

Commit efe53bb

Browse files
Lar Van Der Jagthashrocketeer
authored andcommitted
Example of a role based view resolver
1 parent 8cc48b4 commit efe53bb

File tree

9 files changed

+36
-4
lines changed

9 files changed

+36
-4
lines changed

app/assets/stylesheets/application.css.sass

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,17 @@ body
1515
background-color: $the_gray_area
1616
margin-top: 20px
1717

18-
#content
18+
#content, nav
1919
background-color: $days_of_heaven
2020
margin: 0 auto
2121
width: 960px
2222

23-
#items
23+
nav
24+
text-align: center
25+
margin-bottom: 20px
26+
a
27+
display: block
28+
padding: 10px 0
29+
30+
#items, #empty
2431
padding: 20px

app/controllers/admin/feed_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ module Admin
22
class FeedController < ::ApplicationController
33
append_view_path ItemsResolver.new(Item.subclass_names)
44

5+
before_filter do
6+
prepend_view_path RoleResolver.new(params[:role])
7+
end
8+
59
expose(:things) { Item.all }
610
end
711
end

app/resolvers/role_resolver.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class RoleResolver < ::ActionView::FileSystemResolver
2+
attr_reader :role
3+
4+
def initialize(role)
5+
@role = role
6+
super("app/views")
7+
end
8+
9+
def find_templates(name, prefix, partial, details)
10+
prefix = [prefix, role].compact * '/'
11+
super(name, prefix, partial, details)
12+
end
13+
end

app/views/admin/feed/index.html.haml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
%ul#items
2-
= render partial: things, as: :item
1+
#empty
2+
Sorry, you can't view this page!
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
%ul#items
2+
= render partial: things, as: :item

app/views/application/_nav.html.haml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
%nav
2+
= link_to 'Sign In', '/admin/owner'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
%nav
2+
= link_to 'Sign Out', '/admin'

app/views/layouts/application.html.haml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
= javascript_include_tag 'html5'
1111
%body
1212
= render 'flashes'
13+
= render 'nav'
1314
#content
1415
= yield

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
ViewResolverExample::Application.routes.draw do
22
match '/ui(/:action)', controller: 'ui'
33
namespace :admin do
4+
get '/:role', to: 'feed#index'
45
root to: 'feed#index'
56
end
67
root to: 'feed#index'

0 commit comments

Comments
 (0)