Sha256: 174b5e24b3218bde63d77f64653af1e895cefd0f842fda5049be496bb703924d

Contents?: true

Size: 1.39 KB

Versions: 3

Compression:

Stored size: 1.39 KB

Contents

require 'roles_active_record/strategy/single'

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
    end

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

      def in_any_role(*role_names)
        matching_roles = Role.named(role_names)
        User.where(:role_id => matching_roles.map(&:id))
      end
    end

    module Implementation
      include Roles::ActiveRecord::Strategy::Single

      def set_role role 
        # set_empty_role
        role = role.first if role.kind_of? Array
        self.send("#{role_attribute}=", role)
        # role.users << self
      end
      alias_method :set_roles, :set_role

      def new_role role
        role_class.find_role(extract_role role)
      end

      def new_roles *roles
        new_role roles.flatten.first
      end

      def remove_roles *role_names
        roles = role_names.flat_uniq
        set_empty_role if roles_diff(roles).empty?
        true
      end 

      def present_roles *roles
        roles.map{|role| extract_role role}
      end

      def set_empty_role
        self.send("#{role_attribute}=", nil)
      end
    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.4.8 lib/roles_active_record/strategy/single/one_role.rb
roles_active_record-0.4.7 lib/roles_active_record/strategy/single/one_role.rb
roles_active_record-0.4.6.4 lib/roles_active_record/strategy/single/one_role.rb