Sha256: cc558af97176d4846e99e5cc050861fb3f30fc13c81f95276a29d139d9144959

Contents?: true

Size: 981 Bytes

Versions: 12

Compression:

Stored size: 981 Bytes

Contents

module Rolify
  module Finders
    def with_role(role_name, resource = nil)
      self.adapter.scope(self, :name => role_name, :resource => resource)
    end

    def with_all_roles(*args)
      users = []
      parse_args(args, users) do |users_to_add|
        users = users_to_add if users.empty?
        users &= users_to_add
        return [] if users.empty?
      end
      users
    end

    def with_any_role(*args)
      users = []
      parse_args(args, users) do |users_to_add|
        users += users_to_add
      end
      users.uniq
    end
  end
  
  private
  
  def parse_args(args, users, &block)
    args.each do |arg|
      if arg.is_a? Hash
        users_to_add = self.with_role(arg[:name], arg[:resource])
      elsif arg.is_a?(String) || arg.is_a?(Symbol)
        users_to_add = self.with_role(arg)
      else
        raise ArgumentError, "Invalid argument type: only hash or string or symbol allowed"
      end
      block.call(users_to_add)
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
rolify-4.0.0 lib/rolify/finders.rb
rolify-3.5.2 lib/rolify/finders.rb
rolify-3.5.1 lib/rolify/finders.rb
rolify-3.5.0 lib/rolify/finders.rb
rolify-3.4.1 lib/rolify/finders.rb
rolify-3.4.0 lib/rolify/finders.rb
rolify-3.3.0 lib/rolify/finders.rb
third-prestige-rolify-3.3.0.rc5 lib/rolify/finders.rb
rolify-3.3.0.rc4 lib/rolify/finders.rb
rolify-3.3.0.rc3 lib/rolify/finders.rb
rolify-3.3.0.rc2 lib/rolify/finders.rb
rolify-3.3.0.rc1 lib/rolify/finders.rb