Module: Roleplayer
- Defined in:
- lib/roleplayer.rb,
lib/generators/roleplayer.rb,
lib/generators/roleplayer/migration/migration_generator.rb
Overview
:nodoc:#
Defined Under Namespace
Modules: Generators, InstanceMethods
Constant Summary collapse
- DEFAULT_OPTIONS =
:stopdoc:
{ }.freeze
Instance Method Summary collapse
-
#roleplayer(*args) ⇒ Object
Defines a model as a roleplayer.
Instance Method Details
#roleplayer(*args) ⇒ Object
Defines a model as a roleplayer.
Associations
-
roles
- Contains the roles assigned to the model instance. -
role_assignments
- Polymorphic join model for roles.
Scopes
-
with_any_roles( *args )
- Scope for filtering results based on a list of roles using the OR operator. -
with_all_roles( *args )
- Scope for filtering results based on a list of roles using the AND operator.
Examples
Foo.find(1).roles #=> [ :editor ]
Foo.find(2).roles #=> [ :admin ]
Foo.find(3).roles #=> [ :editor, :admin ]
Foo.with_any_roles( :editor, :admin ) #=> [ <Foo:1 ...>, <Foo:2 ...>, <Foo:3 ...> ]
Foo.with_any_roles( :editor ) #=> [ <Foo:1 ...>, <Foo:3 ...> ]
Foo.with_all_roles( :editor, :admin ) #=> [ <Foo:3 ...> ]
Scopes are chainable:
Foo.with_any_roles( :editor, :admin ).limit( 1 ) #=> [ <Foo:1 ...> ]
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/roleplayer.rb', line 43 def roleplayer( *args ) self..merge!( args. ) has_many :role_assignments, :class_name => 'RoleAssignment', :as => :assignee, :dependent => :destroy has_many :roles, :through => :role_assignments scope :with_any_roles, lambda { |*args| joins( :roles ). where( "roles.name IN (?) ", args.collect { |arg| arg.to_s } ) } scope :with_all_roles, lambda { |*args| joins(:roles). where( "roles.name IN (?) ", args.collect { |arg| arg.to_s } ). group( "accounts.id" ). having( "count(*) = ?", args.size ) } include Roleplayer::InstanceMethods end |