Module: ApplicationHelper

Defined in:
app/helpers/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#actions_table_headingObject



63
64
65
# File 'app/helpers/application_helper.rb', line 63

def actions_table_heading
  t '.actions', default: t("helpers.actions")
end

Variant of the new_button_link, but uses the title prefix ‘Add Another’ instead of ‘New’



201
202
203
204
# File 'app/helpers/application_helper.rb', line 201

def add_another_button_link(path_as_array, options={})
  options.merge!({ operation_title: 'Add Another' })
  new_button_link(path_as_array, options)   
end

We assume the last element in the provided array is a Symbol, since this is a variant of New. We’ll use that last element to be part of the title of the button.



209
210
211
212
213
214
215
# File 'app/helpers/application_helper.rb', line 209

def add_button_link(path_as_array, options={})
  css_classes = 'btn btn-primary'
  css_classes.concat(' btn-mini') if options[:as_mini] 
  resource_name = path_as_array.last.to_s.humanize.capitalize_words
  button_text = "Add #{resource_name}"
  link_to( button_text, path_as_array, class: css_classes)  
end

Generates a Back button, which is a back arrow followed by text inferred from the mandatory parameter provided, such as: [:mock, @commentable, :comments] We handle all the translate lookup and the CSS.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/helpers/application_helper.rb', line 116

def back_button_link(path_as_array, options={})
  last_element = path_as_array.last

  # Are we dealing with a Ruby Symbol?
  if last_element.is_a?(Symbol)
    resource_name = last_element.to_s.humanize
  else
    resource_name = last_element.class.model_name.human
    if last_element.is_singular_child_resource?
      # YES. So we need to swap it out in the path array with
      # a singular symbol of itself instead, so that when passed to url_for(),
      # a correct URL path will be constructed.
      # TODO: This has been used elsewhere; DRY it up by making this logic available somewhere common
      path_as_array[-1] = path_as_array.last.class.to_s.tableize.singularize.to_sym
    end
  end

  provided_resource_name = options[:resource_name] || resource_name

  # Should we put an 'All' in front of the resource name?
  if resource_name.plural?
    button_text = "All #{provided_resource_name.pluralize}"
  else 
    button_text = provided_resource_name
  end


  # Are we dealing with a non-nested resource path that isn't going back
  # to the root? We always prefer using a parent/child nesting route.
  if path_as_array.length == 2 && !root_model_titles.include?(resource_name.singularize.titleize)
    Rails.logger.debug "ROOT_MODEL_NAMES did not include: #{resource_name.singularize.titleize}" 
    # We need to insert into the path as array, a parent object to the last element.
    # e.g. if we received the array: 
    #   [:mock, @phoneable]
    # we would effectively convert it to:
    #   [:mock, <parent of @phoneable>, @phoneable]
    path_as_array.insert(1, last_element.parent_resource_model_object)
  end

  # Build the actual link that will look like a button, thanks to CSS:
  link_to( raw("&larr; #{button_text.titleize}"), path_as_array, class: 'btn') 
end

#back_button_to_patientObject

———- Button Links: Resource Specific ———-



350
351
352
# File 'app/helpers/application_helper.rb', line 350

def back_button_to_patient
  back_button_link [:mock, @patient]
end

#cancel_button(path_as_array) ⇒ Object

Generates the link_to for the Cancel button.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/helpers/application_helper.rb', line 161

def cancel_button(path_as_array)
  modifiable_object = resolved_last_object_from_path(path_as_array)

  if modifiable_object && modifiable_object.new_record?
    # YES: So turn the last element from a model object into a plural symbol.
    last_element = path_as_array.last
    cancel_path = path_as_array - [last_element]
    cancel_path << last_element.class.model_name.tableize.gsub('/', '_').to_sym
  else
    cancel_path = path_as_array
  end

  link_to 'Cancel', cancel_path, class: 'btn'
end

#clear_form_submit_button_tagObject



95
96
97
# File 'app/helpers/application_helper.rb', line 95

def clear_form_submit_button_tag
  submit_tag 'Clear', name: 'clear', class: 'btn btn-primary' 
end

In the options hash, pass in as_mini: true if you want the smaller version.



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'app/helpers/application_helper.rb', line 254

def destroy_button_link(path_as_array, options={})
  css_classes = 'btn btn-danger'
  css_classes.concat(' btn-mini') if options[:as_mini]

  modifiable_object = resolved_last_object_from_path(path_as_array)
  resource_name = modifiable_object.class.model_name.human.capitalize_words
  default_confirmation_msg = "Are you sure you'd like to delete this #{resource_name}?"

  if modifiable_object.marked_as_protected?
    default_confirmation_msg.concat " This is a PROTECTED record."
  end

  link_to t('.destroy', default: t("helpers.links.destroy")),
    path_as_array,
    method: :delete,
    data: { confirm: t('.confirm', default: t("helpers.links.confirm", default: default_confirmation_msg)) },
    class: css_classes
end

#display_for(model_object) {|helper| ... } ⇒ Object

Call this with a block, to which we’ll yield a helper object, much like you would form_for or simple_form_for. We instantiate a ResourceDisplayHelper and yield it into the block, so like you would in building a form for editing an attribute, you have now have a convenient helper to show an attribute.

Yields:

  • (helper)


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

def display_for(model_object)
  helper = ResourceDisplayHelper.new(model_object, self)
  yield helper
end

In the options hash, pass in as_mini: true if you want the smaller version.



238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'app/helpers/application_helper.rb', line 238

def edit_button_link(path_as_array, options={})
  css_classes = 'btn'
  css_classes.concat(' btn-mini') if options[:as_mini] 
  display_title = t('.edit', default: t("helpers.links.edit"))
  options = { class: css_classes }

  # Determine whom the actual editable model object is, so we can determine if it
  # claims to be marked as protected.
  modifiable_object = resolved_last_object_from_path(path_as_array)
  options[:data] = { confirm: 'This is a PROTECTED record. Are you sure?' } if modifiable_object.marked_as_protected?

  link_to display_title, path_as_array, options
end

#edit_resource_heading(model_class) ⇒ Object



57
58
59
60
# File 'app/helpers/application_helper.rb', line 57

def edit_resource_heading(model_class)
  t '.title', default: t('helpers.titles.edit', model: model_class.model_name.human,
                                 default: "Edit #{model_class.model_name.human.titleize}")
end

#export_button(model) ⇒ Object

———- Button Links: General ———-



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

def export_button(model)
  render partial: 'mock/common/export_button', locals: {model: model}
end

#format_newlines(text_with_newlines) ⇒ Object

Replace newlines with HTML break tags to achieve the equivalent intention, targeting this web UI.



7
8
9
# File 'app/helpers/application_helper.rb', line 7

def format_newlines(text_with_newlines)
  text_with_newlines.gsub('\n', '<br/>') if text_with_newlines
end

In the options hash, pass in as_mini: true if you want the smaller version.



229
230
231
232
233
234
# File 'app/helpers/application_helper.rb', line 229

def go_button_link(path_as_array, options={})
  css_classes = 'btn btn-success'
  css_classes.concat(' btn-mini') if options[:as_mini] 
  display_title = options[:title] || 'Go'
  link_to display_title, path_as_array, class: css_classes
end

#index_resource_heading(model_class, additional_text = nil) ⇒ Object



44
45
46
47
48
# File 'app/helpers/application_helper.rb', line 44

def index_resource_heading(model_class, additional_text=nil)
  default_heading = "#{model_class.model_name.human.pluralize.titleize}"
  default_heading.concat(": #{additional_text}") if additional_text
  t '.title', default: default_heading    
end

#modify_reference_list(reference_class, options = {}) ⇒ Object

Public: This inserts a mini button entitled ‘Modify List’ inside a nested div structure that mimics our layout into rows and columns, per Twitter Bootstrap. The button is actually a styled hyperlink. Since the Twitter Bootstrap layout we have already adopted is rather tight, we don’t have a good way to include a button to the side of an input form control. By default, anything we add goes to the next line.

In the case of our modify list button, being on the next line would look out of place. So, although we are technically on the next line, we employ some inline CSS (via the ‘style’) attribute, to float our button back into the ideal position (only ever obscured if the user makes the browser window really tiny).

reference_class - The Class object for the reference list we're making available for modification.
                    An example would be Reference::LabGroup or Reference::AllergyReactionType.
options         - An optional Hash of values. Keys accepted:
                    :class          - A String of additional CSS classes to include for the underlying link.
                    :tooltip_title  - A String to override the tooltip shown when the user hovers over the
                                        button we render.


320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'app/helpers/application_helper.rb', line 320

def modify_reference_list(reference_class, options = {})
  root_data_list = reference_class.root_data_list

  unless root_data_list
    Rails.logger.debug "#{self.class.name} modify_reference_list(): Could not retrieve root_data_list. Skipping insertion of option."
    return nil
  end

  data_set = root_data_list.data_listable
  css_classes = 'btn btn-info btn-mini'
  css_classes.concat(options[:class]) if options[:class]
  tooltip_title = options[:tooltip_title] || "Manage the reference #{root_data_list.display_name} list in a new tab"

  # Build the hyperlink, styled as a button.
  # TODO: Move the inline style to actual CSS and then just refer to it with a CSS class
  value = link_to 'Modify List', [:reference, data_set, root_data_list],
          target: 'Reference Window',
          rel: 'tooltip',
          title: tooltip_title,
          class: css_classes,
          style: 'float: top; margin-top: -90px; margin-left: 10px'

  # Now render the partial that has some templated DIV structures into which we'll insert the 'Modify List' button:
  render partial: 'mock/common/modify_reference_list', locals: {value: value}
end

We display a button that says ‘New <resource name>’. By definition, the array passed in will have the last element being a symbol representing the new thing to be created.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'app/helpers/application_helper.rb', line 179

def new_button_link(path_as_array, options={})
  resource_name = options[:resource_name] || path_as_array.last.to_s.humanize
  operation_title = if options[:operation_title] then options[:operation_title] else 'New' end
  button_text = "#{operation_title} #{resource_name.titleize}" 
  
  css_classes = 'btn btn-primary'
  css_classes.concat(' btn-mini') if options[:as_mini]
  css_classes.concat(' ').concat(options[:class]) if options[:class]

  options = { class: css_classes }
  parent_resource = path_as_array[-2] unless path_as_array.length < 2
  
  # Do we need to indicate with a confirmation that the parent resource we'd create under, is protected?
  if parent_resource && !parent_resource.is_a?(Symbol) && parent_resource.marked_as_protected?
    options[:data] = { confirm: "This would add a new #{resource_name.titleize} to a PROTECTED record. Are you sure?" } 
  end

  link_to button_text, path_as_array, options
end

#new_resource_heading(model_class) ⇒ Object



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

def new_resource_heading(model_class)
  t '.title', default: t('helpers.titles.new', model: model_class.model_name.human,
                               default: "New #{model_class.model_name.human.titleize}")
end

#resolved_last_object_from_path(path_as_array) ⇒ Object

Since some path arrays for resources can have a singular symbol at the end to indicate a singular resource, this method checks for that and backs up a level to invoke that resource symbol as a method on the preceding object (presumably the parent) to ultimately get a handle to the object in question.



361
362
363
364
365
366
367
368
369
370
371
372
# File 'app/helpers/application_helper.rb', line 361

def resolved_last_object_from_path(path_as_array)
  if path_as_array.last.is_a?(Symbol) && path_as_array.size > 2
    # YES: A symbol in the last position means we must be editing a singular resource.
    # We'll retrieve that resource by asking the object preceding it for the relation
    # given by that last element (the symbol). E.g. if we're given a path_as_array of
    # [:edit, :mock, @chart_reviewable, :chart_review], then we'll call
    # @chart_reviewable.send(:chart_review) to get at the ChartReview singular object.
    path_as_array[-2].send(path_as_array.last)
  else
    path_as_array.last
  end
end

#root_model_titlesObject

Determines which of our models are root resources. We then return an Array of humanized model class titles for these.



377
378
379
380
381
382
383
384
385
# File 'app/helpers/application_helper.rb', line 377

def root_model_titles
  Rails.application.eager_load! # Needed to ensure all models are loaded for our asking them if they are root resources.

  all_model_classes = ActiveRecord::Base.descendants
  root_model_classes = all_model_classes.select { |model_class| model_class.included_modules.include? Mock::RootModelConcern }
  names = root_model_classes.collect { |model_class| model_class.to_s.titleize }
  Rails.logger.debug "ApplicationHelper root_model_titles(): Returning #{names}"
  names
end

#search_submit_button_tagObject



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

def search_submit_button_tag
  submit_tag t('.search', default: t("helpers.links.search")), 
    name: nil, class: 'btn btn-primary' 
end

In the options hash, pass in as_mini: true if you want the smaller version.



219
220
221
222
223
224
225
# File 'app/helpers/application_helper.rb', line 219

def show_button_link(path_as_array, options={})
  css_classes = 'btn btn-success '
  css_classes.concat options[:class] if options[:class]
  css_classes.concat(' btn-mini') if options[:as_mini] 
  button_text = options[:title] || t('.show', default: t("helpers.links.show"))
  link_to button_text, path_as_array, class: css_classes
end

#show_resource_heading(model_class, resource_summary = nil) ⇒ Object

model_class

An actual class, or a String title.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/helpers/application_helper.rb', line 30

def show_resource_heading(model_class, resource_summary=nil)
  default_heading =
      if model_class.instance_of?(String)
        model_class.titleize
      else
        model_class.model_name.human.titleize
      end

  optional_newline = default_heading.length > 15 ? '<br/>' : ''
  default_heading.concat(" #{optional_newline}<small>#{resource_summary}</small>") if resource_summary
  raw default_heading
end

#submit_button(form, model_instance, singularize = true) ⇒ Object

Creates a submit button for the given form and model object passed in, handling the logic of ‘Create’ vs. ‘Update’ in the button title. We also handle titleizing the model name.



102
103
104
105
106
107
108
109
110
111
# File 'app/helpers/application_helper.rb', line 102

def submit_button(form, model_instance, singularize = true)
  if singularize
    resource_name = model_instance.class.model_name.underscore.singularize.humanize.titleize.split('/').last
  else
    resource_name = model_instance.class.model_name.underscore.humanize.titleize.split('/').last
  end
  operation_title = if model_instance.new_record? then 'Create' else 'Update' end
  button_title = "#{operation_title} #{resource_name}"
  form.button :submit, value: button_title, class: 'btn-primary'
end

#truncated_id(full_id) ⇒ Object

Shows the last four characters of the provided value. Typically used to truncate unsightly Mongo DB BSON IDs.



13
14
15
# File 'app/helpers/application_helper.rb', line 13

def truncated_id(full_id)
	"...#{full_id.to_s[-5..-1]}" if full_id
end

A wrapper for link_to that uses a truncated value of the provided full text, surfacing the full_text in a tooltip that shows on-hover.



20
21
22
23
24
# File 'app/helpers/application_helper.rb', line 20

def truncated_link_to(full_text, path, html_options={})
	tooltip_options = {rel: 'tooltip', title: full_text }
	html_options.merge! tooltip_options
	link_to truncated_id(full_text), path, html_options
end

Public: In the options hash, you can provide a :class key to specify a string of additional CSS classes to use. For example, to have the button right aligned:

{class: 'pull-right'}

Returns String of HTML command to insert a link.



280
281
282
283
284
285
# File 'app/helpers/application_helper.rb', line 280

def view_api_response_button_link(path, options={})
  css_classes = 'btn btn-info '
  css_classes.concat(options[:class]) if options[:class]
  title = options[:title] || 'View API Response'
  link_to title, path, target: 'API Window', rel: 'tooltip', title: path, class: css_classes 
end


288
289
290
291
292
293
# File 'app/helpers/application_helper.rb', line 288

def view_automation_data_button_link(path, options={})
  css_classes = 'btn btn-info '
  css_classes.concat(options[:class]) if options[:class]
  title = options[:title] || 'View Automation Data'
  link_to title, path, target: 'Data Window', rel: 'tooltip', title: path, class: css_classes 
end


296
297
298
299
300
301
302
# File 'app/helpers/application_helper.rb', line 296

def view_content_button_link(path, options={})
  css_classes = 'btn btn-info '
  css_classes.concat(options[:class]) if options[:class]
  title = options[:title] || 'Show'
  tooltip_title = options[:tooltip_title] || 'View content in new tab'
  link_to title, path, target: 'Content Window', rel: 'tooltip', title: tooltip_title, class: css_classes
end