Sha256: b9870f2b92a302163e2f4ff6a90bc944bfef1c5e671ca6a5701624c128287923
Contents?: true
Size: 1.76 KB
Versions: 1
Compression:
Stored size: 1.76 KB
Contents
require 'roles_active_record/strategy/multi' class Role < ActiveRecord::Base scope :named, lambda{|role_names| where(:name.in => role_names.flatten)} has_many :users, :through => :user_roles has_many :user_roles end class UserRole < ActiveRecord::Base belongs_to :user belongs_to :role end module RoleStrategy::ActiveRecord module ManyRoles def self.default_role_attribute :many_roles end def self.included base base.extend Roles::Generic::Role::ClassMethods base.extend ClassMethods base.has_many :many_roles, :through => :user_roles, :source => :role base.has_many :user_roles end module ClassMethods def in_role(role_name) in_any_role(role_name) end def in_any_role(*role_names) joins(:many_roles) & Role.named(role_names.to_strings) end end module Implementation include Roles::ActiveRecord::Strategy::Multi # assign multiple roles def roles=(*role_names) puts "Assign roles #{role_names}" role_names = role_names.flat_uniq role_names = extract_roles(role_names) return nil if role_names.empty? puts "role names #{role_names}" valids = role_class.find_roles(role_names).to_a puts "valids: #{valids}" vrs = select_valid_roles role_names puts "Set valid roles #{vrs}" set_roles(vrs) end def new_roles *role_names role_class.find_roles(extract_roles role_names) end def present_roles roles_names roles_names.to_a.map{|role| role.name.to_s.to_sym} end def set_empty_roles self.send("#{role_attribute}=", []) end end extend Roles::Generic::User::Configuration configure :type => :role_class end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
roles_active_record-0.3.1 | lib/roles_active_record/strategy/multi/many_roles.rb |