class Object
Public Class Methods
Get roles accessible by the current user Usage:
Role.reflect_on_all_associations(:has_and_belongs_to_many).first.class_name => "User" Role.reflect_on_all_associations(:has_many).first.class_name
@return [Array] Array of permissions for the current user
# File lib/generators/devisable/templates/partials/_accessible_permissions_model.rb, line 6 def self.accessible_permissions @accessible_permissions = [] controllers = Dir.new("#{RAILS_ROOT}/app/controllers").entries controllers = controllers.map { |controller| controller.downcase.gsub("_controller.rb","").singularize if controller =~ /_controller/ }.compact models = Dir.new("#{RAILS_ROOT}/app/models").entries models.each do |model| mod = model.downcase.gsub(".rb","") if controllers.include?(mod) @accessible_permissions << mod.camelize.pluralize end end @accessible_permissions end
Public Instance Methods
Equality check based on the role id, controller, and ability
@param another_permission The permission to compare against @return [Boolean] True if the permissions match role id, controller and ability
# File lib/generators/devisable/templates/partials/_permission_equals.rb, line 5 def ==(another_permission) self.role_id == another_permission.role_id && self.model == another_permission.model && self.ability == another_permission.ability ? true : false end
Role.reflect_on_all_associations(:has_and_belongs_to_many).first.class_name => "User"
ruby-1.8.7-p302 > Role.reflect_on_all_associations(:has_many).first.class_name
# File lib/generators/devisable/templates/partials/_accessible_permissions_controller.rb, line 5 def accessible_permissions @accessible_permissions = Role.accessible_permissions end
Determines whether the given controller is the current controller
@param Name of the controller to check against @return [Boolean] True if the controller_name parameter matches the current controller nam
# File lib/generators/devisable/templates/partials/_application_current_tab.rb, line 5 def current_tab?(controller_name) controller.controller_name == controller_name end
Creates checkboxes for a has and belongs to many relationship between ?
@param obj An instance of a model with the specified field @param column The attribute of the obj parameter used to determine if the assignment_object is assigned to the obj parameter @param assignment_objects A list of objects with a habtm relationship with the obj parameter @param assignment_object_display_column The field on the assignment_objects used to create the label for the checkboxes @return [String] An html string of checkboxes for the relationship between the obj and assignment_objects
# File lib/generators/devisable/templates/partials/_role_permission.rb, line 8 def habtm_checkboxes(obj, column, assignment_objects, assignment_object_display_column) obj_to_s = obj.class.to_s.split("::").last.underscore field_name = "#{obj_to_s}[#{column}][]" html = hidden_field_tag(field_name, "") assignment_objects.each do |assignment_obj| cbx_id = "#{obj_to_s}_#{column}_#{assignment_obj.id}" html += check_box_tag field_name, assignment_obj.id, obj.send(column).include?(assignment_obj.id), :id => cbx_id html += label_tag cbx_id, h(assignment_obj.send(assignment_object_display_column)) html += content_tag(:br) end html end
Creates permission checkboxes for each type of permission and permission category. Permission types include manage, read, create, update, and destroy. They are hardcoded in this method.
@param obj An instance of the Role model or any model with a habtm relationship with Permission @param column Not used @param controllers A list of controllers that can have permissions applied to them @param role_id Id that corresponds to an instance of the role model. Should refer to the same object as the obj parameter. @return [String] Html safe string of permissions checkboxes for each controller and action
# File lib/generators/devisable/templates/partials/_role_permission.rb, line 30 def permissions_checkboxes(obj, column, controllers, role_id) perms = obj.permissions html = "" abilities = ['manage','read','create','update','destroy'] html += content_tag(:table) do html_table = "" controllers.each do |controller| controller.strip! html_table += content_tag(:tr) do html_tr = "" html_tr += content_tag(:th, controller) html_tr += content_tag(:th, "Use") html_tr += content_tag(:th, "View") html_tr += content_tag(:th, "Add") html_tr += content_tag(:th, "Edit") html_tr += content_tag(:th, "Delete") html_tr.html_safe end html_table += content_tag(:tr) do html_tr = "" html_tr += content_tag(:td," ") abilities.each do |ability| p = { :role_id => role_id, :model => controller.singularize, :ability => ability } perm = Permission.new(p) checked = perms.include?(perm) #checked = false html_tr += content_tag(:td) do check_box_tag 'role_ids[]',p.to_json,checked, {:id => "permission_#{controller}_#{ability}", :class => "permission_#{ability}"} end end html_tr.html_safe end end html_table.html_safe end html.html_safe end
Determines whether or not a user has a role
@param role Name of the role to search for @return [Boolean] True if the user has the specified role
# File lib/generators/devisable/templates/partials/_user_role.rb, line 5 def role?(role) return !!self.roles.find_by_name(role.to_s.camelize) end
Save permissions all permissions for a single role First deletes all permissions for the role, then loops through the input and saves new permissions
@param role Role To reset permisisons on @param role_ids A list of permissions to apply to the role
# File lib/generators/devisable/templates/partials/_accessible_permissions_model.rb, line 25 def save_permissions(role_ids) permissions.map{|perm| perm.delete } unless permissions.nil? unless role_ids.nil? role_ids.each do |permission| p = Permission.new(JSON.parse(permission)) (p.class.reflect_on_all_associations(:has_many) & p.class.reflect_on_all_associations(:has_and_belongs_to_many)).each { |association| permissions << Permission.new( :role_id => id, :controller => association.class_name.singularize, :ability => p.ability ) } permissions << p end end end