Module: ApplicationHelper

Defined in:
app/helpers/application_helper.rb

Overview

Contains general, simple view helper code. Designed to keep code out of our views as much as possible

Instance Method Summary collapse

Instance Method Details

#admin?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'app/helpers/application_helper.rb', line 142

def admin?
  current_user.try(:admin?)
end

#audited_value(value) ⇒ Object



186
187
188
189
190
191
192
# File 'app/helpers/application_helper.rb', line 186

def audited_value(value)
  if value.kind_of? Array
    value.last
  else
    value
  end
end

#body_class(class_name = "") ⇒ Object



92
93
94
95
# File 'app/helpers/application_helper.rb', line 92

def body_class(class_name="")
  class_name = "#{body_class_default} #{class_name}"
  content_for :body_class, class_name
end

#body_class_defaultObject



88
89
90
# File 'app/helpers/application_helper.rb', line 88

def body_class_default
  [controller_name, action_name].join('-') + "#{controller_name} #{action_name}"
end

#description_css_class(description) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/helpers/application_helper.rb', line 129

def description_css_class(description)
  klass = "item "
  case description.status_id
  when 2
    klass += "success"
  when 1
    klass += "warning"
  when 3
    klass += "danger"
  end
  klass
end

#devise_form_errors(errors) ⇒ Object

Unwraps Devise error messages so they look like flash messages, the way regular application alerts work

Parameters:

  • errors (ActiveModel::Errors)

    a list of model errors set by Devise



100
101
102
103
104
105
106
# File 'app/helpers/application_helper.rb', line 100

def devise_form_errors(errors)
  capture do
    errors.full_messages.each_with_index.map do |msg, idx|
      concat render partial: 'alert', locals: { flash_type: :error, flash_message: msg, flash_id: "devise_flash_#{idx}" }
    end
  end
end

#devise_mappingObject



84
85
86
# File 'app/helpers/application_helper.rb', line 84

def devise_mapping
  @devise_mapping ||= Devise.mappings[:user]
end

Returns HTML link with the given icon attached

Parameters:

  • text (String)

    the link text to show the user

  • path (String)

    the link target

  • icon_name (String) (defaults to: nil)

    what Font Awesome icon to associate with the link. Optional, defaults to nil.

  • options (Hash) (defaults to: {})

    passed on to the Rails link_to helper

Returns:

  • (String)

    HTML link with the given icon attached

See Also:



39
40
41
42
43
44
45
46
47
48
# File 'app/helpers/application_helper.rb', line 39

def drop_down_menu_link(text, path, icon_name = nil, options = {})
  classes = %w[fa fa-fw]
  classes << "fa-#{icon_name}" if icon_name

  link_to(path, options) do
    concat tag.i('', class: classes, :'aria-hidden' => true)
    concat "\n"
    concat text
  end
end

#endpoint_collectionArray<String, Integer>

Returns list of Endpoints suitable for use in select boxes in Representation forms

Returns:

  • (Array<String, Integer>)

    list of Endpoints suitable for use in select boxes in Representation forms

See Also:



9
10
11
# File 'app/helpers/application_helper.rb', line 9

def endpoint_collection
  Endpoint.sorted
end

#flash_class(level) ⇒ String

Returns CSS class to use when styling a flash message

Parameters:

  • level (String)

    level of flash message to be styled

Returns:

  • (String)

    CSS class to use when styling a flash message



110
111
112
# File 'app/helpers/application_helper.rb', line 110

def flash_class(level)
  FLASH_CLASSES.fetch(level.to_sym)
end

#image_status_css_class(status_code) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/helpers/application_helper.rb', line 114

def image_status_css_class(status_code)
  klass = ""
  case status_code
  when 0
    klass += "undescribed"
  when 1
    klass += "partial"
  when 2
    klass += "warning"
  when 3
    klass += "success"
  end
  return klass
end

#language_listArray<Array>

Returns a collection of languages suitable for use in a select box

Returns:

  • (Array<Array>)

    a collection of languages suitable for use in a select box

See Also:



29
30
31
# File 'app/helpers/application_helper.rb', line 29

def language_list
  @language_list ||= LanguageList::COMMON_LANGUAGES.map { |l| [l.common_name, l.iso_639_1] }
end

#language_name(language_code) ⇒ String

Returns human-friendly language name

Parameters:

  • language_code (String)

    language short code such as 'en'

Returns:

  • (String)

    human-friendly language name

See Also:



23
24
25
# File 'app/helpers/application_helper.rb', line 23

def language_name(language_code)
  LanguageList::LanguageInfo.find(language_code)&.name
end


194
195
196
# File 'app/helpers/application_helper.rb', line 194

def license_link(license)
  "https://choosealicense.com/licenses/" + license
end

#license_title(license) ⇒ Object



198
199
200
201
202
203
204
205
206
207
# File 'app/helpers/application_helper.rb', line 198

def license_title(license)
  case license
  when "cc0-1.0"
    "Creative Commons Zero v1.0 Universal"
  when "cc-by-sa-4.0"
    "Creative Commons Attribution Share Alike 4.0"
  when "cc-by-4.0"
    "Creative Commons Attribution 4.0"
  end
end

#minimum_password_lengthInteger

Returns minimum number of password characters we accept

Returns:

  • (Integer)

    minimum number of password characters we accept

See Also:



52
53
54
# File 'app/helpers/application_helper.rb', line 52

def minimum_password_length
  User.password_length.min
end

Used to render top-level navigation, so the current page gets an “active” CSS class applied

Parameters:

  • text (String)

    the link text to display

  • path (String)

    the target of the link



178
179
180
181
182
183
184
# File 'app/helpers/application_helper.rb', line 178

def nav_menu_link(text, path)
  link_class = current_page?(path) ? "active" : ""

  (:li, class: link_class) do
    link_to(text, path)
  end
end

#organizational_user_assignable_rolesHash

Returns a collection of User roles with human friendly labels that the current organizational user can assign

Returns:

  • (Hash)

    a collection of User roles with human friendly labels that the current organizational user can assign

See Also:



58
59
60
61
62
63
64
65
66
67
# File 'app/helpers/application_helper.rb', line 58

def organizational_user_assignable_roles
  roles = []

  Coyote::Membership.each_role do |label, role_name, _|
    break unless organization_user.send(:"#{role_name}?")
    roles << [label, role_name]
  end

  roles
end

#organizational_user_collectionArray<String, Integer>

Returns List of users in the current organization, sorted by name, suitable for use in a select box

Returns:

  • (Array<String, Integer>)

    List of users in the current organization, sorted by name, suitable for use in a select box



14
15
16
17
18
# File 'app/helpers/application_helper.rb', line 14

def organizational_user_collection
  # @return [Array<String, Integer>] list of users suitable for use in select boxes
  collection = current_organization.active_users.active.sort_by { |u| u.username.downcase }
  collection.map! { |u| [u.username, u.id] }
end

#resourceObject



80
81
82
# File 'app/helpers/application_helper.rb', line 80

def resource
  @resource ||= User.new
end

#resource_nameObject



76
77
78
# File 'app/helpers/application_helper.rb', line 76

def resource_name
  :user
end

#to_html(content) ⇒ Object



146
147
148
149
150
151
152
# File 'app/helpers/application_helper.rb', line 146

def to_html(content)
  if content.blank?
    ""
  else
    raw markdown.render(content)
  end
end

#to_html_attr(content) ⇒ Object



171
172
173
# File 'app/helpers/application_helper.rb', line 171

def to_html_attr(content)
  h to_text(content)
end

#to_html_title(content) ⇒ String

Note:

This is a hack to avoid <p> tags when rendering resource titles as H1, see github.com/vmg/redcarpet/issues/596

Returns HTML-formatted string, suitable for use in an H1 tag

Parameters:

  • content (String)

    a piece of text annotated with Markdown

Returns:

  • (String)

    HTML-formatted string, suitable for use in an H1 tag



165
166
167
168
169
# File 'app/helpers/application_helper.rb', line 165

def to_html_title(content)
  html = to_html(content)
  html.gsub!(%r{(?:^<p>|</p>\n)}i, '')
  html.html_safe
end

#to_text(content) ⇒ Object



154
155
156
157
158
159
160
# File 'app/helpers/application_helper.rb', line 154

def to_text(content)
  if content.blank?
    ""
  else
    strip_tags to_html(content)
  end
end

#welcome_messageString

Returns welcome message, including the user's name if someone is logged-in

Returns:

  • (String)

    welcome message, including the user's name if someone is logged-in



70
71
72
73
74
# File 'app/helpers/application_helper.rb', line 70

def welcome_message
  msg =  "Welcome to Coyote"
  msg << ", #{current_user}!" if current_user
  msg
end