Sha256: 644d575290eaf92f45dd820a4b1a8f78b824ce2b94b992d7c59cb8fa1495f894

Contents?: true

Size: 1.31 KB

Versions: 16

Compression:

Stored size: 1.31 KB

Contents

module Cream
  class << self
    attr_accessor :available_roles  
  end
  
  module Role
    def self.available
      return Cream.available_roles if Cream.available_roles.present?
      return Cream.roles if Cream.roles.present?

      puts "Cream didn't have available roles configured, now trying alternatives: User and Role"

      return try_role if try_role
      return try_user if try_user
            
      raise %q{Available roles could not be determined. Please do either of the following:\n
1) Create a Cream.setup block
2) Set Cream.available_roles explicitly
3) Create Role instances and ensure a call to Role.all returns them
4) Set User.roles explicitly
}          
    end

    protected

    def self.try_user 
      if !defined? ::User
        puts "No User defined"
        return nil 
      end

      puts "User has roles available?"       
      return ::User.roles.to_symbols if ::User.respond_to?(:roles)             
      puts "User didn't have a #roles class method"
      nil
    end

    def self.try_role
      if !defined? ::Role
        puts "No Role defined"
        return nil 
      end

      puts "Role has roles available?"       
      return ::Role.all.map(&:name).to_symbols if ::Role.respond_to?(:all)        
      puts "Role didn't have an #all class method"
      nil
    end      
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
cream-0.8.9.1 lib/cream/role.rb
cream-0.8.9 lib/cream/role.rb
cream-0.8.8 lib/cream/role.rb
cream-0.8.7 lib/cream/role.rb
cream-0.8.6 lib/cream/role.rb
cream-0.8.5 lib/cream/role.rb
cream-0.8.4 lib/cream/role.rb
cream-0.8.3 lib/cream/role.rb
cream-0.8.2 lib/cream/role.rb
cream-0.8.1 lib/cream/role.rb
cream-0.8.0 lib/cream/role.rb
cream-0.7.10 lib/cream/role.rb
cream-0.7.8 lib/cream/role.rb
cream-0.7.7 lib/cream/role.rb
cream-0.7.6 lib/cream/role.rb
cream-0.7.5 lib/cream/role.rb