Sha256: 5f37b5d467f4dab38dbdf129056f8065f9a15ad793f64be997143775a840535f

Contents?: true

Size: 1.31 KB

Versions: 7

Compression:

Stored size: 1.31 KB

Contents

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
      def role_attribute
        strategy_class.roles_attribute_name
      end
          
      # assign roles
      def roles=(*new_roles)                                 
        first_role = new_roles.flatten.first
        if valid_role?(first_role)
          self.send("#{role_attribute}=", new_roles.flatten.first.admin?) 
        else
          raise ArgumentError, "The role #{first_role} is not a valid role"
        end
      end

      # query assigned roles
      def roles
        role = self.send(role_attribute) ? strategy_class.admin_role_key : strategy_class.default_role_key
        [role]
      end
      alias_method :roles_list, :roles

    end # Implementation
    
    extend Roles::Generic::User::Configuration
    configure :num => :single
  end   
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
roles_active_record-0.2.5 lib/roles_active_record/strategy/single/admin_flag.rb
roles_active_record-0.2.4 lib/roles_active_record/strategy/single/admin_flag.rb
roles_active_record-0.2.3 lib/roles_active_record/strategy/single/admin_flag.rb
roles_active_record-0.2.2 lib/roles_active_record/strategy/single/admin_flag.rb
roles_active_record-0.2.1 lib/roles_active_record/strategy/single/admin_flag.rb
roles_active_record-0.2.0 lib/roles_active_record/strategy/single/admin_flag.rb
roles_active_record-0.1.0 lib/roles_active_record/strategy/single/admin_flag.rb