Module: ApplicationHelper

Defined in:
app/helpers/application_helper.rb

Overview

Application-wide helpers

Instance Method Summary collapse

  • #pdf_header_image ⇒ String

    Image to be used as header in pdf files.

  • #state_change_links(machine) ⇒ String

    Outputs a list of links to create every possible and authorized state transition for a given state machine.

  • #state_info(object_with_state) ⇒ String

    Outputs the state of a model instance with a help tooltip if needed.

  • #timestamped_state(object_with_state) ⇒ String

    Outputs the state of a model instance with the appropiate css class and the associated date.

  • #user_signed_in_by_ichain? ⇒ Boolean

    Checks if the current user logged in using iChain.

  • Instance Method Details

    #bootstrap_flash_messagesObject

    Outputs the flash messages in a bootstrap optimized way

    Stolen from https://github.com/seyhunak/twitter-bootstrap-rails

    
    
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    # File 'app/helpers/application_helper.rb', line 224
    
    def bootstrap_flash_messages
      flash_messages = []
      flash.each do |type, message|
        # Skip empty messages, e.g. for devise messages set to nothing in a locale file.
        next if message.blank?
    
        type = 'success' if type == 'notice'
        type = 'danger' if %w[error alert].include?(type)
        next unless %w[danger info success warning].include?(type)
    
        Array(message).each do |msg|
          text = (:div,
                             (:button, raw('×'), :class => 'close', 'data-dismiss' => 'alert') +
                             msg.html_safe, class: "alert fade in alert-#{type}")
          flash_messages << text if message
        end
      end
      flash_messages.join("\n").html_safe
    end

    Outputs the breadcrumbs based in the @breadcrumbs variable

    Returns:

    • (String)

      unordered list containing the breadcrumbs

    
    
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    # File 'app/helpers/application_helper.rb', line 284
    
    def breadcrumbs
      return '' unless @breadcrumbs&.respond_to?(:map)
    
      crumbs = @breadcrumbs.map do |b|
        # First of all, adjust the label,...
        label = b[:label]
        # ...that can be a string, but...
        unless label.is_a? String
          # ...also a symbol...
          label = if label.is_a? Symbol
                    I18n.t(label)
                  # ...or a more complex object.
                  # In that case, some methods are tried before
                  # falling back to 'classname #id'
                  elsif label.respond_to? :name
                    label.name
                  elsif label.respond_to? :title
                    label.title
                  else
                    "#{label.class.model_name.human} ##{label.id}"
                  end
        end
    
        if b[:url].blank? || current_page?(b[:url])
          (:li, label, class: 'active')
        else
          (:li, link_to(label, b[:url]))
        end
      end
    
      crumbs = crumbs.join(' ' + (:span, '>', class: 'divider') + ' ')
      (:ul, crumbs.html_safe, class: 'breadcrumb')
    end

    Outputs a trigger for (un)collapsing a target element

    Parameters:

    • target (String)

      id of the HTML element to (un)collapse

    • label (String) (defaults to: '')

      additional text to prepend to the icon

    Returns:

    • (String)

      HTML output

    
    
    78
    79
    80
    81
    # File 'app/helpers/application_helper.rb', line 78
    
    def collapse_link(target, label = '')
      link_to(label.html_safe + (:i, '', class: 'icon-resize-vertical'),
              "\##{target}", title: t(:collapse), data: { toggle: :collapse })
    end

    #country_label(country_code) ⇒ Object

    Outputs country code in a human readable and localized way, using the locale file for localized_country_select

    param [String] country_code as specified in locale file return [String] HTML output

    
    
    23
    24
    25
    26
    27
    28
    29
    # File 'app/helpers/application_helper.rb', line 23
    
    def country_label(country_code)
      if country_code.blank?
        t('show_for.blank')
      else
        country_code + ' - ' + t("countries.#{country_code}")
      end
    end

    #currencies_for_select(field, event = nil) ⇒ Array

    Currency codes that can be selected for a given field

    Parameters:

    • field (#to_s)

      Name of the field as specified in the site.yml file, like 'estimated' or 'approved'

    • event (Event) (defaults to: nil)

      An optional event in which context the currency is going to be defined. Only relevant if budget_limits are enabled.

    Returns:

    • (Array)

      Array with the currency codes

    
    
    251
    252
    253
    254
    255
    256
    257
    258
    259
    # File 'app/helpers/application_helper.rb', line 251
    
    def currencies_for_select(field, event = nil)
      if Rails.configuration.site['budget_limits'] &&
         event && event.budget && event.budget.currency &&
         %w[approved authorized].include?(field.to_s)
        currencies = [event.budget.currency]
      end
      currencies ||= Rails.configuration.site["currencies_for_#{field}"]
      currencies ||= I18n.translate(:currencies).keys.sort
    end

    #current_roleObject

    Outputs the role of the current user. Useful, for example, for deciding which partial view should be used.

    
    
    116
    117
    118
    # File 'app/helpers/application_helper.rb', line 116
    
    def current_role
      current_user.find_profile.role_name
    end

    #current_role?(role) ⇒ Boolean

    Checks if the current user belongs to a given role

    Parameters:

    • role (#to_s)

    Returns:

    • (Boolean)

      true if the current users has the given role

    
    
    124
    125
    126
    # File 'app/helpers/application_helper.rb', line 124
    
    def current_role?(role)
      current_role == role.to_s
    end

    Outputs a Bootstrap's button dropdown menu

    Parameters:

    • label (String)

      the title of the button

    • links (Array)

      list of options for the dropdown. Empty strings are considered dividers

    Returns:

    • (String)

      a btn-group div

    
    
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    # File 'app/helpers/application_helper.rb', line 170
    
    def dropdown(label, links)
      if links.empty?
        ''
      else
        li_tags = links.map do |l|
          if l.empty?
            (:li, '', class: 'divider')
          else
            (:li, l)
          end
        end
        (:div,
                    link_to(label.html_safe + ' ' + (:span, '', class: 'caret'),
                            '#',
                            class: 'btn dropdown-toggle', data: { toggle: 'dropdown' }) +
                    (:ul, li_tags.join("\n").html_safe, class: 'dropdown-menu'),
                    class: 'btn-group')
      end
    end

    #enabled?(*setting_path) ⇒ Boolean

    Checks whether a concrete setting is enabled in the config file

    Returns:

    • (Boolean)

      value of the :enabled key for the provided path in the configuration file

    
    
    322
    323
    324
    # File 'app/helpers/application_helper.rb', line 322
    
    def enabled?(*setting_path)
      Rails.configuration.site.dig(*setting_path)['enabled']
    end

    #expenses_sum(object, attr) ⇒ String

    Outputs the sum of the expenses of a request or a reimbursement (or for a collection of requests/reimbursents), as a list of comma separated number_to_currency (one for every used currency)

    Parameters:

    • object (Object)

      a request, a reimbursement or a collection (reimbursements or request, not mixed)

    • attr (Symbol)

      can be :estimated, :approved, :total or :authorized

    Returns:

    • (String)

      HTML output

    
    
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    # File 'app/helpers/application_helper.rb', line 101
    
    def expenses_sum(object, attr)
      sum = if object.respond_to?(:size)
              if first = object.first
                first.class.expenses_sum(attr, object)
              else
                []
              end
            else
              object.expenses_sum(attr)
            end
      sum.map { |k, v| number_to_currency(v, unit: (k || '?')) }.join(', ')
    end

    Outputs a link with the "go to" label and an icon

    Parameters:

    • path (String)

      url for the link

    Returns:

    • (String)

      HTML output

    
    
    87
    88
    89
    90
    # File 'app/helpers/application_helper.rb', line 87
    
    def goto_link(path)
      link_to("#{t(:goto)} ".html_safe + (:i, '', class: 'icon-share-alt'),
              path, title: t(:goto))
    end

    #icon_to(name, path, options = {}) ⇒ String

    Outputs a link with an icon inside (and visible no text)

    Parameters:

    Returns:

    • (String)

      HTML output

    
    
    69
    70
    71
    # File 'app/helpers/application_helper.rb', line 69
    
    def icon_to(name, path, options = {})
      link_to((:i, '', class: "icon-#{name}"), path, options)
    end

    #machine_url(machine) ⇒ Object

    URL for accesing to a given request or reimbursement

    This helper deals with the singleton issues in the reimbursement routes definition.

    
    
    194
    195
    196
    197
    198
    199
    200
    # File 'app/helpers/application_helper.rb', line 194
    
    def machine_url(machine)
      if machine.is_a?(Reimbursement)
        request_reimbursement_url(machine.request)
      else
        send(:"#{machine.class.model_name.element}_url", machine)
      end
    end

    #markdown(text) ⇒ Object

    Output the text in a markdown manner

    
    
    57
    58
    59
    60
    61
    # File 'app/helpers/application_helper.rb', line 57
    
    def markdown(text)
      renderer = Redcarpet::Render::HTML.new(hard_wrap: true)
      markdown = Redcarpet::Markdown.new(renderer, autolink: true)
      markdown.render(text).html_safe
    end

    Path to the list of requests or reimbursements with the ransack filters already configured based on the assign_state definitions of the corresponding class.

    See Also:

    • HasState.assign_state
    
    
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    # File 'app/helpers/application_helper.rb', line 207
    
    def menu_path_to(machine_class)
      helper = machine_class.model_name.to_s.tableize + '_path'
      # Assistants are a special case, they should have the same default filters than tsp
      role = current_role.to_s == 'assistant' ? :tsp : current_role
      states = machine_class.states_assigned_to(role)
      # FIXME: requesters are also an special case nowadays. Let's look for a
      # better solution afterward.
      if current_role?(:none) || states.blank?
        send helper
      else
        send(helper, q: { state_in: states })
      end
    end

    Outputs a

  • element suitable for use in Bootstrap nav menus, that is, with the 'active' css class when needed

    param [String] label Text to display param [String] path Target URL return [String] HTML output

  • 
    
    13
    14
    15
    16
    # File 'app/helpers/application_helper.rb', line 13
    
    def nav_tab(label, path)
      css = current_page?(path) ? 'active' : nil
      (:li, link_to(t(label), path), class: css)
    end

    #pdf_header_imageString

    Image to be used as header in pdf files

    Returns:

    • (String)

      local path of the image

    
    
    264
    265
    266
    267
    268
    269
    270
    # File 'app/helpers/application_helper.rb', line 264
    
    def pdf_header_image
      if theme = Rails.configuration.site['theme']
        path = File.join(Rails.root.to_s, 'app', 'themes', theme, 'assets', 'images', 'pdf', 'header.png')
        return path if File.exist?(path)
      end
      File.join(Rails.root.to_s, 'app', 'assets', 'images', 'pdf', 'header.png')
    end

    Outputs a list of links to create every possible and authorized state transition for a given state machine. If the user is authorized, it also adds the 'cancel' and 'adjust state' options.

    Parameters:

    • machine (#state_events)

      a request or a reimbursement (or any other object including the HasState mixin)

    Returns:

    • (String)

      a bootstrap-based button dropdown menu

    
    
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    # File 'app/helpers/application_helper.rb', line 136
    
    def state_change_links(machine)
      resource_path = case machine.class.name
                      when 'Shipment'
                        shipment_path(machine)
                      when 'Reimbursement'
                        request_reimbursement_path(machine.request)
                      when 'TravelSponsorship'
                        travel_sponsorship_path(machine)
                      end
      trans_path = resource_path + '/state_transitions/new.js?state_transition[state_event]='
      links = machine.state_events.map do |event|
        next unless can? event, machine
    
        link_to(t("activerecord.state_machines.events.#{event}").titleize, trans_path + event.to_s, remote: true)
      end.compact
      # Add cancel link
      if can? :cancel, machine
        links << ''
        links << link_to(t('helpers.links.cancel'), "#{trans_path}cancel", remote: true)
      end
      # Add adjust_state link
      if can? :adjust_state, machine
        links << ''
        links << link_to(t('helpers.links.adjust_state'), resource_path + '/state_adjustments/new.js', remote: true)
      end
      dropdown t('helpers.workflow_actions'), links
    end

    #state_info(object_with_state) ⇒ String

    Outputs the state of a model instance with a help tooltip if needed

    Parameters:

    • object_with_state (#state)

      the request, reimbursement or any other object with state

    Returns:

    • (String)

      HTML output

    
    
    46
    47
    48
    49
    50
    51
    52
    53
    54
    # File 'app/helpers/application_helper.rb', line 46
    
    def state_info(object_with_state)
      msg = (:span, object_with_state.human_state_name, class: object_with_state.state)
      msg += " (#{object_with_state.human_state_description})"
      if object_with_state.state_updated_at.blank?
        msg += ' '
        msg += (:span, '!', title: t(:state_help), class: 'badge with-tooltip')
      end
      raw(msg)
    end

    #timestamped_state(object_with_state) ⇒ String

    Outputs the state of a model instance with the appropiate css class and the associated date

    Parameters:

    • object_with_state (#state)

      the request, reimbursement or any other object with state

    Returns:

    • (String)

      HTML output

    
    
    36
    37
    38
    39
    40
    # File 'app/helpers/application_helper.rb', line 36
    
    def timestamped_state(object_with_state)
      msg = (:span, object_with_state.human_state_name, class: object_with_state.state)
      msg += ' ' +  t(:since, date: l(object_with_state.state_updated_at, format: :long)) unless object_with_state.state_updated_at.blank?
      raw(msg)
    end

    #user_signed_in_by_ichain?Boolean

    Checks if the current user logged in using iChain

    Returns:

    • (Boolean)

      true if signed in by means of iChain

    
    
    275
    276
    277
    278
    279
    # File 'app/helpers/application_helper.rb', line 275
    
    def user_signed_in_by_ichain?
      return false unless user_signed_in?
    
      current_user.respond_to?(:signed_in_by_ichain?) && current_user.signed_in_by_ichain?
    end