Permalink
Browse files

using latest role helpers in cream views for better DSL experience

  • Loading branch information...
1 parent ceea6b5 commit 565e79b782478b93e080861a630b0ce2d40e378e @kristianmandrup committed Jan 8, 2011
View
@@ -47,8 +47,7 @@ The "Cream user group":http://groups.google.com/group/rails-cream is a google gr
h2. Inspiration and Ideas
-_Tony Amoyal_ has written a two part series ("Part I":http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/, "Part II":http://www.tonyamoyal.com/2010/09/29/rails-authentication-with-devise-and-cancan-part-2-restful-resources-for-administrators/
-) on how to setup a fully integrated Devise/CanCan solution with Roles. You might want to take a look at this for further customization needs. It might also provide food for thought for suggestions on improvements to the Cream framework ;)
+_Tony Amoyal_ has written a two part series ("Part I":http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/, "Part II":http://www.tonyamoyal.com/2010/09/29/rails-authentication-with-devise-and-cancan-part-2-restful-resources-for-administrators/) on how to setup a fully integrated Devise/CanCan solution with Roles. You might want to take a look at this for further customization needs. It might also provide food for thought for suggestions on improvements to the Cream framework ;) I have started work on a _devise:customize_ generator to facilitate some of these customization needs...
h3. Design ideas
@@ -264,17 +263,23 @@ Main generator:
Config generators:
-* _devise:config_ - Configure Rails 3 application with Devise
-* _devise:users_ - Configure Rails 3 application with Devise users
-* _cancan:config_ - Configures app with CanCan
-* _permits:config_ - Configures app with CanCan Permits
-* _roles:config_ - Configures app with Roles
+Devise (authentication)
+
+* _devise:config_ - Configure Rails 3 application with Devise
+* _devise:users_ - Configure Rails 3 application with Devise users
+* _devise:customize_ - Customize devise configuration
+
+Authorization incl. roles
+
+* _cancan:config_ - Configures app with CanCan
+* _permits:config_ - Configures app with CanCan Permits
+* _roles:config_ - Configures app with Roles
Other generators:
-* _cancan:restlinks_ - Create REST links locale file
-* _devise:links_ - Create devise links locale file (should I rename this to authlinks?)
-* _cream:views_ - Generates partials for menu items (outdated)
+* _cancan:restlinks_ - Create REST links locale file
+* _devise:links_ - Create devise links locale file (should I rename this to authlinks?)
+* _cream:views_ - Generates partials for menu items (outdated)
All the above generators have specs included in cream that demonstrate how to use them and should verify that they work as expected.
@@ -1,9 +1,9 @@
-<% not_for_role :guest do %>
+<% for_any_user :logged_in do %>
<li>
<%= link_to('Logout', destroy_user_session_path) %>
</li>
<% end %>
-<% for_role :guest do %>
+<% for_any_user :not_logged_in do %>
<li>
<%= link_to('Login', new_user_session_path) %>
</li>
@@ -1,9 +1,9 @@
-<% for_any_role do %>
+<% for_any_user :logged_in do %>
<li>
<%= link_to('Edit registration', edit_user_registration_path) %>
</li>
<% end %>
-<% for_guest do %>
+<% for_any_user :not_logged_in do %>
<li>
<%= link_to('Register', new_user_registration_path) %>
</li>
View
@@ -45,30 +45,42 @@ def owner? obj, relation=nil
# execute block if user DOES have any of the given roles
def for_roles *user_roles, &block
user_roles = user_roles.flatten
- yield if has_role?(user_roles) && block
+ yield if has_roles?(user_roles) && block
end
def for_any_role *user_roles, &block
user_roles = user_roles.flatten
yield if has_role?(user_roles) && block
end
- def for_role user_role, &block
+ def for_role user_role, &block
+ if is_negation_role?(user_role)
+ not_for_role(user_role, &block)
+ return
+ end
yield if has_role?(user_role) && block
end
# execute block if user DOES NOT have any of the given roles
def not_for_roles(*user_roles, &block)
user_roles = user_roles.flatten
- yield if !has_role?(user_roles) && block
+ yield if !has_roles?(user_roles) && block
end
def not_for_role(user_role, &block)
+ if is_negation_role?(user_role)
+ for_role(user_role, &block)
+ return
+ end
yield if !has_role?(user_role) && block
end
protected
+ def is_negation_role? user_role
+ !(:user_role.to_s =~ /^not_/).nil?
+ end
+
def user_relation? obj, relation
raise ArgumentError, "User method must be a Symbol or String" if !relation.kind_of_label?
current_user && is_owner?(current_user, obj, relation)

0 comments on commit 565e79b

Please sign in to comment.