Created
March 22, 2013 04:14
-
-
Save linjian/5218895 to your computer and use it in GitHub Desktop.
Add scope with_role to Role Model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copy from easy_roles | |
# https://github.com/platform45/easy_roles/blob/cf9c5f6e5db46adf8c48cc1a2e43c83ece3c2f4f/lib/methods/bitmask.rb | |
# | |
# Put it to config/initializers/ | |
module RoleModel | |
class << self | |
alias_method :origin_included, :included | |
end | |
def self.included(base) | |
origin_included(base) | |
base.class_eval do | |
scope :with_role, proc { |role| | |
role = role.to_sym | |
raise ArgumentError unless valid_roles.include? role | |
role_bit_index = valid_roles.index(role) | |
valid_mask_integers = (0..2**valid_roles.count-1).select {|i| i[role_bit_index] == 1 } | |
where(roles_attribute_name => valid_mask_integers) | |
} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment