[I'm a newbie so please let me know if I'm contributing something bad/wrong/not optimal]
First, thanks a bunch for this code, it's been working great and has helped me learn about Rails.
As I was testing this out, it bothered me that when you go to see each user's roles, it would not print out "none" or something to tell you that there were no roles assigned to the user under the "Roles assigned" (see below) for the rendered HTML of a user without a role assigned:
Roles for <user>
Roles assigned:
Roles available:
* administrator assign role
If you're picky like me, I wanted a way to tell if a user had any role, and print something out if not. So here's what I changed/added:
Addition to user.rb model:
def has_any_role?
roles = Role.find(:all)
roles.each do |role|
if self.roles.find_by_rolename(role.rolename) # OR it can call has_role?(role.rolename)
return true
end
end
return false
end
Additions to roles' index.rhtml (lines: 4, 6,7,8, 11, 13, 14, 15)
<h2>Roles for <%=h @user.login.capitalize %></h2>
<h3>Roles assigned:</h3>
<% if @user.has_any_role? %>
<ul><%= render :partial => 'role', :collection => @user.roles %></ul>
<% else %>
<ul>None</ul>
<% end %><h3>Roles available:</h3>
<% if (@all_roles - @user.roles).length > 0 %>
<ul><%= render :partial => 'role', :collection => (@all_roles - @user.roles) %></ul>
<% else %>
<ul>None</ul>
<% end %>
Note: I wish I could color lines inside the 'code' tags of BBCode, it would make seeing what I changed/added much easier.
Please let me know if this is good or is there a better way of doing it.
Cheers