Module: SolidQueueUi::WebHelpers

Defined in:
lib/solid_queue_ui/web/helpers.rb

Overview

This is not a public API

Constant Summary collapse

SAFE_QPARAMS =
%w[page direction]

Instance Method Summary collapse

Instance Method Details

#add_to_headObject

This view helper provide ability display you html code in to head of page. Example:

<% add_to_head do %>
  <link rel="stylesheet" .../>
  <meta .../>
<% end %>


87
88
89
90
# File 'lib/solid_queue_ui/web/helpers.rb', line 87

def add_to_head
  @head_html ||= []
  @head_html << yield.dup if block_given?
end

#available_localesObject



46
47
48
# File 'lib/solid_queue_ui/web/helpers.rb', line 46

def available_locales
  @available_locales ||= locale_files.map { |path| File.basename(path, ".yml") }.uniq
end

#clear_cachesObject



34
35
36
37
38
# File 'lib/solid_queue_ui/web/helpers.rb', line 34

def clear_caches
  @strings = nil
  @locale_files = nil
  @available_locales = nil
end

#csrf_tagObject



187
188
189
# File 'lib/solid_queue_ui/web/helpers.rb', line 187

def csrf_tag
  "<input type='hidden' name='authenticity_token' value='#{env[:csrf_token]}'/>"
end

#current_pathObject



159
160
161
# File 'lib/solid_queue_ui/web/helpers.rb', line 159

def current_path
  @current_path ||= request.path_info.gsub(/^\//, "")
end

#display_custom_headObject



92
93
94
# File 'lib/solid_queue_ui/web/helpers.rb', line 92

def display_custom_head
  @head_html.join if defined?(@head_html)
end

#display_tags(job, within = "retries") ⇒ Object



73
74
75
76
77
# File 'lib/solid_queue_ui/web/helpers.rb', line 73

def display_tags(job, within = "retries")
  job.tags.map { |tag|
    "<span class='label label-info jobtag'>#{filter_link(tag, within)}</span>"
  }.join(" ")
end


65
66
67
68
69
70
71
# File 'lib/solid_queue_ui/web/helpers.rb', line 65

def filter_link(jid, within = "retries")
  if within.nil?
    ::Rack::Utils.escape_html(jid)
  else
    "<a href='#{root_path}filter/#{within}?substr=#{jid}'>#{::Rack::Utils.escape_html(jid)}</a>"
  end
end

#filtering(which) ⇒ Object



61
62
63
# File 'lib/solid_queue_ui/web/helpers.rb', line 61

def filtering(which)
  erb(:filtering, locals: {which: which})
end

#find_locale_files(lang) ⇒ Object



50
51
52
# File 'lib/solid_queue_ui/web/helpers.rb', line 50

def find_locale_files(lang)
  locale_files.select { |file| file =~ /\/#{lang}\.yml$/ }
end

#get_localeObject



137
138
139
# File 'lib/solid_queue_ui/web/helpers.rb', line 137

def get_locale
  strings(locale)
end

#localeObject

Given an Accept-Language header like “fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4,ru;q=0.2” this method will try to best match the available locales to the user’s preferred languages.

Inspiration taken from github.com/iain/http_accept_language/blob/master/lib/http_accept_language/parser.rb



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/solid_queue_ui/web/helpers.rb', line 121

def locale
  @locale ||= begin
    matched_locale = user_preferred_languages.map { |preferred|
      preferred_language = preferred.split("-", 2).first

      lang_group = available_locales.select { |available|
        preferred_language == available.split("-", 2).first
      }

      lang_group.find { |lang| lang == preferred } || lang_group.min_by(&:length)
    }.compact.first

    matched_locale || "en"
  end
end

#locale_filesObject



40
41
42
43
44
# File 'lib/solid_queue_ui/web/helpers.rb', line 40

def locale_files
  @locale_files ||= settings.locales.flat_map { |path|
    Dir["#{path}/*.yml"]
  }
end

#product_versionObject



192
193
194
# File 'lib/solid_queue_ui/web/helpers.rb', line 192

def product_version
  "SolidQueueUi v#{SolidQueueUi::VERSION}"
end

#qparams(options) ⇒ Object

Merge options with current params, filter safe params, and stringify to query string



175
176
177
178
179
# File 'lib/solid_queue_ui/web/helpers.rb', line 175

def qparams(options)
  stringified_options = options.transform_keys(&:to_s)

  to_query_string(params.merge(stringified_options))
end

#relative_time(time) ⇒ Object

def current_status

(workset.size == 0) ? "idle" : "active"

end



167
168
169
170
# File 'lib/solid_queue_ui/web/helpers.rb', line 167

def relative_time(time)
  stamp = time.getutc.iso8601
  %(<time class="ltr" dir="ltr" title="#{stamp}" datetime="#{stamp}">#{time}</time>)
end

#root_pathObject



155
156
157
# File 'lib/solid_queue_ui/web/helpers.rb', line 155

def root_path
  "#{env["SCRIPT_NAME"]}/"
end

#rtl?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/solid_queue_ui/web/helpers.rb', line 100

def rtl?
  text_direction == "rtl"
end

#search(jobset, substr) ⇒ Object



54
55
56
57
58
59
# File 'lib/solid_queue_ui/web/helpers.rb', line 54

def search(jobset, substr)
  resultset = jobset.scan(substr).to_a
  @current_page = 1
  @count = @total_size = resultset.size
  resultset
end

#server_utc_timeObject



196
197
198
# File 'lib/solid_queue_ui/web/helpers.rb', line 196

def server_utc_time
  Time.now.utc.strftime("%H:%M:%S UTC")
end

#singularize(str, count) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/solid_queue_ui/web/helpers.rb', line 26

def singularize(str, count)
  if count == 1 && str.respond_to?(:singularize) # rails
    str.singularize
  else
    str
  end
end

#sort_direction_labelObject



150
151
152
# File 'lib/solid_queue_ui/web/helpers.rb', line 150

def sort_direction_label
  (params[:direction] == "asc") ? "&uarr;" : "&darr;"
end

#strings(lang) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/solid_queue_ui/web/helpers.rb', line 11

def strings(lang)
  @strings ||= {}

  @strings[lang] ||= settings.locales.each_with_object({}) do |path, global|
    find_locale_files(lang).each do |file|
      strs = YAML.safe_load(File.read(file))
      global.merge!(strs[lang])
    end
  end
end

#t(msg, options = {}) ⇒ Object



141
142
143
144
145
146
147
148
# File 'lib/solid_queue_ui/web/helpers.rb', line 141

def t(msg, options = {})
  string = get_locale[msg] || strings("en")[msg] || msg
  if options.empty?
    string
  else
    string % options
  end
end

#text_directionObject



96
97
98
# File 'lib/solid_queue_ui/web/helpers.rb', line 96

def text_direction
  get_locale["TextDirection"] || "ltr"
end

#to_json(x) ⇒ Object



22
23
24
# File 'lib/solid_queue_ui/web/helpers.rb', line 22

def to_json(x)
  SolidQueueUi.dump_json(x)
end

#to_query_string(params) ⇒ Object



181
182
183
184
185
# File 'lib/solid_queue_ui/web/helpers.rb', line 181

def to_query_string(params)
  params.map { |key, value|
    SAFE_QPARAMS.include?(key) ? "#{key}=#{CGI.escape(value.to_s)}" : next
  }.compact.join("&")
end

#user_preferred_languagesObject



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/solid_queue_ui/web/helpers.rb', line 105

def user_preferred_languages
  languages = env["HTTP_ACCEPT_LANGUAGE"]
  languages.to_s.downcase.gsub(/\s+/, "").split(",").map { |language|
    locale, quality = language.split(";q=", 2)
    locale = nil if locale == "*" # Ignore wildcards
    quality = quality ? quality.to_f : 1.0
    [locale, quality]
  }.sort { |(_, left), (_, right)|
    right <=> left
  }.map(&:first).compact
end