Sha256: ffe30e8db0569083ac91d0b49d520d4cf756e488df1dc71d560d82286df7ccad

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

module SwitchUserHelper
  def switch_user_select
    if available?
      if current_user
        options = "<option value=''>Guest</option>"
      else
        options = "<option selected='selected' value=''>Guest</option>"
      end
      SwitchUser.available_users.each do |scope, user_proc|
        user_proc.call.each do |user|
          current = send("current_#{scope}")
          if current and current.id == user.id
            options += "<option selected='selected' value='#{scope}_#{user.id}'>#{user.send(SwitchUser.display_field)}</option>"
          else
            options += "<option value='#{scope}_#{user.id}'>#{user.send(SwitchUser.display_field)}</option>"
          end
        end
      end
      if options.respond_to?(:html_safe)
        options = options.html_safe
      end
      select_tag "switch_user_id", options,
                 :onchange => "location.href = '/switch_user?scope_id=' + encodeURIComponent(this.options[this.selectedIndex].value)"
    end
  end

  def available?
    user = nil
    SwitchUser.available_users.keys.each do |scope|
      user = send("current_#{scope}")
      break if user
    end
    SwitchUser.view_guard.call(user, request)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
switch_user-0.5.1 app/helpers/switch_user_helper.rb
switch_user-0.5.0 app/helpers/switch_user_helper.rb