Sha256: c665cba3cf1e5307e1db4d3d5c56ddbda4a37e0bdaedcd09c7edc810755a81f6

Contents?: true

Size: 1.23 KB

Versions: 21

Compression:

Stored size: 1.23 KB

Contents

require 'roles_active_record/strategy/single'

module RoleStrategy::ActiveRecord
  module AdminFlag    
    def self.default_role_attribute
      :admin_flag
    end

    def self.included base
      base.extend ClassMethods
    end

    module ClassMethods 
      def role_attribute
        strategy_class.roles_attribute_name.to_sym
      end
           
      def in_role(role_name) 
        case role_name.downcase.to_sym
        when :admin
          where(role_attribute => true)
        else
          where(role_attribute => false)
        end          
      end
    end

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

      def new_role role
        role = role.kind_of?(Array) ? role.flatten.first : role
        role.admin?
      end
      
      def get_role
        self.send(role_attribute) ? strategy_class.admin_role_key : strategy_class.default_role_key
      end 
      
      def present_roles *roles
        roles = roles.flat_uniq
        roles.map{|role| role ? :admin : :guest}
      end   
      
      def set_empty_role
        self.send("#{role_attribute}=", false)
      end      
    end # Implementation
    
    extend Roles::Generic::User::Configuration
    configure :num => :single
  end   
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
roles_active_record-0.5.1 lib/roles_active_record/strategy/single/admin_flag.rb
roles_active_record-0.5.0 lib/roles_active_record/strategy/single/admin_flag.rb
roles_active_record-0.4.9 lib/roles_active_record/strategy/single/admin_flag.rb
roles_active_record-0.4.8 lib/roles_active_record/strategy/single/admin_flag.rb
roles_active_record-0.4.7 lib/roles_active_record/strategy/single/admin_flag.rb
roles_active_record-0.4.6.4 lib/roles_active_record/strategy/single/admin_flag.rb
roles_active_record-0.4.6.3 lib/roles_active_record/strategy/single/admin_flag.rb
roles_active_record-0.4.6.2 lib/roles_active_record/strategy/single/admin_flag.rb
roles_active_record-0.4.6.1 lib/roles_active_record/strategy/single/admin_flag.rb
roles_active_record-0.4.6 lib/roles_active_record/strategy/single/admin_flag.rb
roles_active_record-0.4.5 lib/roles_active_record/strategy/single/admin_flag.rb
roles_active_record-0.4.4 lib/roles_active_record/strategy/single/admin_flag.rb
roles_active_record-0.4.3 lib/roles_active_record/strategy/single/admin_flag.rb
roles_active_record-0.4.2 lib/roles_active_record/strategy/single/admin_flag.rb
roles_active_record-0.4.1 lib/roles_active_record/strategy/single/admin_flag.rb
roles_active_record-0.4.0 lib/roles_active_record/strategy/single/admin_flag.rb
roles_active_record-0.3.6 lib/roles_active_record/strategy/single/admin_flag.rb
roles_active_record-0.3.5 lib/roles_active_record/strategy/single/admin_flag.rb
roles_active_record-0.3.4 lib/roles_active_record/strategy/single/admin_flag.rb
roles_active_record-0.3.3 lib/roles_active_record/strategy/single/admin_flag.rb