Sha256: 5baa4a5690df53607c0ecc412d2c9d28ec1dfe36c61d6cab3bdfdeef98277d92

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

module SwitchUserHelper
  SelectOption = Struct.new(:label, :scope_id)
  def switch_user_select(options = {})
    return unless available?

    selected_user = nil

    grouped_options_container = {}.tap do |h|
      SwitchUser.all_users.each do |record|
        scope = record.is_a?(SwitchUser::GuestRecord) ? :Guest : record.scope.to_s.capitalize
        h[scope] ||= []
        h[scope] << [record.label, record.scope_id]

        if selected_user.nil?
          unless record.is_a?(SwitchUser::GuestRecord)
            if provider.current_user?(record.user, record.scope)
              selected_user = record.scope_id
            end
          end
        end
      end
    end

    option_tags = grouped_options_for_select(grouped_options_container.to_a, selected_user)

    render :partial => "switch_user/widget",
           :locals => {
             :option_tags => option_tags,
             :classes => options[:class],
             :styles => options[:style],
           }
  end

  private

  def user_tag_value(user, id_name, scope)
    identifier = user.send(id_name)

    "#{scope}_#{identifier}"
  end

  def user_tag_label(user, name)
    name.respond_to?(:call) ? name.call(user) : user.send(name)
  end

  def available?
    SwitchUser.guard_class.new(controller, provider).view_available?
  end

  def provider
    SwitchUser::Provider.init(controller)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
switch_user-1.4.0 app/helpers/switch_user_helper.rb
switch_user-1.3.1 app/helpers/switch_user_helper.rb