lib/canard/adapters/mongoid.rb in canard-0.4.3 vs lib/canard/adapters/mongoid.rb in canard-0.5.0.pre
- old
+ new
@@ -2,13 +2,14 @@
module Adapters
module Mongoid
private
- def add_role_scopes
+ def add_role_scopes(*args)
+ options = args.extract_options!
valid_roles.each do |role|
- define_scopes_for_role role
+ define_scopes_for_role role, options[:prefix]
end
def with_any_role(*roles)
where("(this.#{roles_attribute_name} & #{mask_for(*roles)}) > 0")
end
@@ -24,17 +25,16 @@
def has_roles_mask_accessors?
fields.include?(roles_attribute_name.to_s) || super
end
- def define_scopes_for_role(role)
- include_scope = role.to_s.pluralize
- exclude_scope = "non_#{include_scope}"
-
- scope include_scope, lambda { where("(this.#{roles_attribute_name} & #{mask_for(role)}) > 0") }
- scope exclude_scope, lambda { any_of({roles_attribute_name => { "$exists" => false }}, {roles_attribute_name => nil}, {"$where" => "(this.#{roles_attribute_name} & #{mask_for(role)}) === 0"}) }
- end
+ def define_scopes_for_role(role, prefix=nil)
+ include_scope = [prefix, String(role).pluralize].compact.join('_')
+ exclude_scope = "non_#{include_scope}"
+ scope include_scope, -> { where("(this.#{roles_attribute_name} & #{mask_for(role)}) > 0") }
+ scope exclude_scope, -> { any_of({roles_attribute_name => { "$exists" => false }}, {roles_attribute_name => nil}, {"$where" => "(this.#{roles_attribute_name} & #{mask_for(role)}) === 0"}) }
+ end
end
end
end
Mongoid::Document::ClassMethods.send :include, Canard::Adapters::Mongoid
\ No newline at end of file