Module: TranzitoUtils::Helpers

Includes:
AdminHelper, GraphingHelper, SortableHelper
Defined in:
lib/tranzito_utils/helpers/helpers.rb

Constant Summary

Constants included from SortableHelper

SortableHelper::DEFAULT_SEARCH_KEYS

Instance Method Summary collapse

Methods included from SortableHelper

#sortable, #sortable_search_params, #sortable_search_params?, #sortable_search_params_without_sort

Methods included from GraphingHelper

#group_by_format, #group_by_method, #humanized_time_range, #humanized_time_range_column, #period_in_words, #time_range_amounts, #time_range_counts, #time_range_duration

Methods included from AdminHelper

#admin_number_display

Instance Method Details



24
25
26
27
28
29
# File 'lib/tranzito_utils/helpers/helpers.rb', line 24

def active_link(link_text, link_path, html_options = {})
  match_controller = html_options.delete(:match_controller)
  html_options[:class] ||= ""
  html_options[:class] += " active" if current_page_active?(link_path, match_controller)
  link_to(raw(link_text), link_path, html_options).html_safe
end

#alert_class_for_flash(flash_type) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/tranzito_utils/helpers/helpers.rb', line 58

def alert_class_for_flash(flash_type)
  case flash_type
  when "error" then "alert-danger"
  when "alert" then "alert-warning"
  when "notice" then "alert-info"
  else
    "alert-#{flash_type}"
  end
end

#current_page_active?(link_path, match_controller = false) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
# File 'lib/tranzito_utils/helpers/helpers.rb', line 31

def current_page_active?(link_path, match_controller = false)
  link_path = Rails.application.routes.recognize_path(link_path)
  active_path = Rails.application.routes.recognize_path(request.url)
  matches_controller = active_path[:controller] == link_path[:controller]
  return true if match_controller && matches_controller
  current_page?(link_path) || matches_controller && active_path[:action] == link_path[:action]
rescue # This mainly fails in testing - but why not rescue always
  false
end

#current_user_time_preference_scriptObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/tranzito_utils/helpers/helpers.rb', line 41

def current_user_time_preference_script
  time_preference = current_user&.time_preference || User.time_preferences.first
  scrpt = ""
  if %w[single_format_local single_format_event].include?(time_preference)
    scrpt += "window.timeParserSingleFormat=true;"
  end
  if %w[single_format_event variable_format_event].include?(time_preference)
    scrpt += 'window.localTimezone="America/Los_Angeles";'
  end
  scrpt
end

#in_admin?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/tranzito_utils/helpers/helpers.rb', line 9

def in_admin?
  controller_namespace == "admin"
end

#page_titleObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/tranzito_utils/helpers/helpers.rb', line 13

def page_title
  return @page_title if defined?(@page_title)
  prefix = (in_admin? ? "🧰" : TranzitoUtils::DEFAULT[:application_display_name])
  return "#{prefix} #{@prefixed_page_title}" if @prefixed_page_title.present?
  [
    prefix,
    default_action_name_title,
    controller_title_for_action
  ].compact.join(" ")
end

#pretty_print_json(data) ⇒ Object



53
54
55
56
# File 'lib/tranzito_utils/helpers/helpers.rb', line 53

def pretty_print_json(data)
  require "coderay"
  CodeRay.scan(JSON.pretty_generate(data), :json).div.html_safe
end

#set_active_period(period) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/tranzito_utils/helpers/helpers.rb', line 68

def set_active_period(period)
  periods = {
    hour: "Past hour",
    day: "Past day",
    month: "Past thirty days",
    year: "Past year",
    week: "Past seven days",
    next_month: "next thirty days",
    next_week: "next seven days",
    all: "All Time",
    custom: @start_time.strftime("%Y/%m/%d") + " ~ " + @end_time.strftime("%Y/%m/%d")
  }
  periods[period.to_sym] || periods[:all]
end