module UsersHelper

File

users_helper.rb

(C)

Hipposoft 2008

Purpose

Support functions for views related to User objects. See controllers/users_controller.rb for more.


03-Jan-2008 (ADH): Created.

Public Instance Methods

userhelp_actions( user ) click to toggle source

Return list actions appropriate for the given user

# File app/helpers/users_helper.rb, line 170
def userhelp_actions( user )
  actions = [ 'edit' ]
  actions.push( 'delete' ) unless user.admin?
  actions.push( 'show' )
  return actions
end
userhelp_active_selector( form, user ) click to toggle source

Output HTML for the given form and user which produces a selection list allowing the user’s ‘active’ flag to be changed. It is assumed that the caller has permission to edit the user in this way.

# File app/helpers/users_helper.rb, line 80
def userhelp_active_selector( form, user )
  return apphelp_select(
    form,
    :active,
    [
      [ 'Active',      true  ],
      [ 'Deactivated', false ]
    ],
    false
  )
end
userhelp_default_customer_selector( cp, user ) click to toggle source

Output HTML for the given control panel form (fields_for…) and user which lets the user choose the default customer associated with new projects.

The including template should use appropriate container tags for the output (e.g. a paragraph or table cell).

# File app/helpers/users_helper.rb, line 131
def userhelp_default_customer_selector( cp, user )
  customers = Customer.active

  unless( customers.empty? )
    output = apphelp_collection_select( cp, :customer_id, customers, :id, :title, false )

    if( user.restricted? )
      output << "\n\n"
      output << "          <p>\n".html_safe()
      output << "            This list is only relevant to users with a\n"
      output << "            privileged account type because normal users\n"
      output << "            cannot create new tasks.\n"
      output << "          </p>".html_safe()
    end
  else
    output  = "          There are no active customers. Please\n"
    output << "          #{ link_to( 'create at least one', new_customer_path() ) }.".html_safe()
  end

  return output.html_safe()
end
userhelp_default_project_selector( user ) click to toggle source

Output HTML for the given user’s control panel editing section within a wider user editing form, which lets the user choose the default project associated with new tasks.

The including template should use appropriate container tags for the output (e.g. a paragraph or table cell).

# File app/helpers/users_helper.rb, line 99
def userhelp_default_project_selector( user )
  unless( Project.active.count.zero? )
    output = apphelp_project_selector(
      'control_panel_project_id',
      'control_panel[project_id]',
      user.control_panel.nil? ? nil : user.control_panel.project_id,
      true
    )

    if( user.restricted? )
      output << "\n\n"
      output << "          <p>\n".html_safe()
      output << "            This list is only relevant to users with a\n"
      output << "            privileged account type because normal users\n"
      output << "            cannot create new tasks.\n"
      output << "          </p>".html_safe()
    end
  else
    output  = "          There are no active projects. Please\n"
    output << "          #{ link_to( 'create at least one', new_project_path() ) }.".html_safe()
  end

  return output.html_safe()
end
userhelp_email( user ) click to toggle source

List view helper - format the given user’s e-mail address as a link; do so using auto_link so we don’t have to worry about malformed addresses. That’s the main reason for doing this in a helper rather than manually, within the model.

# File app/helpers/users_helper.rb, line 158
def userhelp_email( user )
  return auto_link( h( user.email ) )
end
userhelp_identity_url( user ) click to toggle source

As above, but for the user’s identity URL

# File app/helpers/users_helper.rb, line 164
def userhelp_identity_url( user )
  return auto_link( h( user.identity_url ) )
end
userhelp_user_type_selector( form, user ) click to toggle source

Output HTML for the given form and user which produces a selection list allowing the user account type to be chosen. It is assumed that the caller has permission to edit the user in this way.

The including template should use appropriate container tags for the output (e.g. a paragraph or table cell).

# File app/helpers/users_helper.rb, line 20
def userhelp_user_type_selector( form, user )
  if ( @current_user.id == user.id and user.admin? )

    # You can't change your own account type if you are an administrator.
    # This is an easy way to prevent all administrators losing privilege,
    # leaving no administrator in the system.

    output  = "          Administrators cannot change account type. If you\n"
    output << "          want to stop being an administrator, first assign\n"
    output << "          administrative rights to another account, then\n"
    output << "          have that user change your account type for you."

  elsif ( not @current_user.admin? and user.admin? )

    # If the editing account is administrative but the user is not,
    # then the account type can't be changed (managers can only assign
    # privilege up to manager).

    output  = "          Administrator. Only another administrator can change\n"
    output << "          this user's account type.\n"

  else

    # OK, this is your account and you're not an administrator, or
    # it is someone else's account and they're not an administrator.

    if ( @current_user.admin? )
      warn    = false
      options = [
                  [ 'Normal',        User::USER_TYPE_NORMAL  ],
                  [ 'Manager',       User::USER_TYPE_MANAGER ],
                  [ 'Administrator', User::USER_TYPE_ADMIN   ]
                ]
    else
      warn    = true
      options = [
                  [ 'Normal',  User::USER_TYPE_NORMAL  ],
                  [ 'Manager', User::USER_TYPE_MANAGER ]
                ]
    end

    output = apphelp_select( form, :user_type, options, false )

    if ( warn and @current_user.id == user.id )
      output << "\n\n"
      output << "          <p>\n".html_safe()
      output << "            Note that if you revoke your own account privileges,\n"
      output << "            you will need the help of another manager or an\n"
      output << "            administrator if you want to restore them later.\n"
      output << "          </p>".html_safe()
    end
  end

  return output.html_safe()
end