Sha256: 802b87f96a74b4db08d7ccc1f783f36ee5485c334823c924396045c35196143f

Contents?: true

Size: 914 Bytes

Versions: 12

Compression:

Stored size: 914 Bytes

Contents

module SwitchUser
  class UserLoader
    attr_reader :scope
    attr_accessor :id

    def self.prepare(*args)
      options = args.extract_options!

      if options[:scope_identifier]
        options[:scope_identifier] =~ /^(.*)_([^_]+)$/
        scope, id = $1, $2
      else
        scope, id = args
      end
      new(scope, id)
    end

    def initialize(scope, id)
      self.scope = scope
      self.id = id
    end

    def user
      user_class.where(column_name => id).first
    end

    private

    def scope=(scope)
      if scope && SwitchUser.available_scopes.include?(scope.to_sym)
        @scope = scope
      else
        raise InvalidScope.new("#{scope} is invalid and is not listed in SwitchUser#available_users")
      end
    end

    def user_class
      scope.classify.constantize
    end

    def column_name
      SwitchUser.available_users_identifiers[scope.to_sym]
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
switch_user-1.5.0 lib/switch_user/user_loader.rb
switch_user-1.4.0 lib/switch_user/user_loader.rb
switch_user-1.3.1 lib/switch_user/user_loader.rb
switch_user-1.3.0 lib/switch_user/user_loader.rb
switch_user-1.2.1 lib/switch_user/user_loader.rb
switch_user-1.2.0 lib/switch_user/user_loader.rb
switch_user-1.1.0 lib/switch_user/user_loader.rb
switch_user-1.0.2 lib/switch_user/user_loader.rb
switch_user-1.0.1 lib/switch_user/user_loader.rb
switch_user-1.0.0 lib/switch_user/user_loader.rb
switch_user-0.9.5 lib/switch_user/user_loader.rb
switch_user-0.9.4 lib/switch_user/user_loader.rb