Sha256: b721965b89f221b938d58d26d3bf4f537fbd3050a0faf85114530b063aa95d95

Contents?: true

Size: 1.36 KB

Versions: 3

Compression:

Stored size: 1.36 KB

Contents

class Role < ActiveRecord::Base  
  scope :named, lambda{|role_names| where(:name.in => role_names.flatten)}  
  belongs_to :user
end  

module RoleStrategy::ActiveRecord
  module OneRole
    def self.default_role_attribute
      :one_role
    end

    def self.included base
      base.extend Roles::Generic::Role::ClassMethods
      base.extend ClassMethods            
      base.has_one :one_role, :foreign_key => :role_id, :class_name => 'Role'      
    end

    module ClassMethods    
      def in_role(role_name)                          
        in_roles(role_name)
      end

      def in_roles(*role_names)                          
        joins(:one_role) & Role.named(role_names)
      end
    end

    module Implementation      
      # assign roles
      def roles=(*roles)      
        raise "Role class #{role_class} does not have a #find_role(role) method" if !role_class.respond_to? :find_role
        first_role = roles.flatten.first
        role_relation = role_class.find_role(first_role) 
        self.send("#{role_attribute}=", role_relation)
      end
      alias_method :role=, :roles=
      
      # query assigned roles
      def roles
        [self.send(role_attribute).name.to_sym]
      end
      alias_method :roles_list, :roles
      
    end

    extend Roles::Generic::User::Configuration
    configure :num => :single, :type => :role_class    
  end  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
roles_active_record-0.2.1 lib/roles_active_record/strategy/single/one_role.rb
roles_active_record-0.2.0 lib/roles_active_record/strategy/single/one_role.rb
roles_active_record-0.1.0 lib/roles_active_record/strategy/single/one_role.rb