Module: CommandProposal::ParamsHelper

Included in:
IterationsController, RunnerController, TasksController
Defined in:
app/helpers/command_proposal/params_helper.rb

Instance Method Summary collapse

Instance Method Details

#command_paginate(paged_collection) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/helpers/command_proposal/params_helper.rb', line 77

def command_paginate(paged_collection)
  collection = paged_collection.unscope(:limit, :offset)

  per = ::CommandProposal::PAGINATION_PER
  total_pages = (collection.count / per.to_f).ceil
  current_page = params[:page].presence&.to_i || 1

  return if total_pages <= 1

  div(class: "cmd-pagination") do
    links = []
    links << ["<<", { page: 1 }] if current_page > 1
    links << ["<", { page: current_page - 1 }] if current_page > 1

    (current_page-2..current_page+2).each do |page_window|
      next if page_window < 1
      next if page_window > total_pages

      links << [page_window, { page: page_window }]
    end

    links << [">", page: current_page + 1] if current_page < total_pages
    links << [">>", page: total_pages] if current_page < total_pages

    links.map do |link_text, link_params|
      "<a class=\"cmd-pagination-link #{'current-page' if link_params[:page] == current_page}\" href=\"#{current_path(link_params)}\">#{link_text}</a>"
    end.join("\n")
  end
end

#current_params(merged = {}) ⇒ Object



10
11
12
# File 'app/helpers/command_proposal/params_helper.rb', line 10

def current_params(merged={})
  params.except(:action, :controller, :host, :port, :authenticity_token, :utf8, :commit).to_unsafe_h.merge(merged)
end

#current_path(new_params = {}) ⇒ Object



25
26
27
28
29
30
31
32
# File 'app/helpers/command_proposal/params_helper.rb', line 25

def current_path(new_params={})
  if @task.present?
    new_params.merge!(iteration: @iteration.id) if @iteration.present? && !@iteration.primary_iteration?
    cmd_path(@task, new_params)
  else
    cmd_path(:tasks, current_params(new_params))
  end
end

#div(opts = {}, &block) ⇒ Object



73
74
75
# File 'app/helpers/command_proposal/params_helper.rb', line 73

def div(opts={}, &block)
  "<div #{opts.map { |k, v| "#{k}=\"#{v}\"" }.join(" ")}>#{yield}</div>".html_safe
end

#humanized_duration(seconds) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/helpers/command_proposal/params_helper.rb', line 42

def humanized_duration(seconds)
  return "N/A" if seconds.blank?

  remaining = seconds.round
  str_parts = []

  durations = {
    w: 7 * 24 * 60 * 60,
    d: 24 * 60 * 60,
    h: 60 * 60,
    m: 60,
    s: 1,
  }

  durations.each do |label, length|
    count_at_length = 0

    while remaining > length do
      remaining -= length
      count_at_length += 1
    end

    next if count_at_length == 0

    str_parts.push("#{count_at_length}#{label}")
  end

  return "< 1s" if str_parts.none?
  str_parts.join(" ")
end

#sort_orderObject



2
3
4
# File 'app/helpers/command_proposal/params_helper.rb', line 2

def sort_order
  params[:order] == "desc" ? "desc" : "asc"
end

#toggled_param(toggle_h) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'app/helpers/command_proposal/params_helper.rb', line 14

def toggled_param(toggle_h)
  toggle_key = toggle_h.keys.first
  toggle_val = toggle_h.values.first

  if params[toggle_key].to_s == toggle_val.to_s
    cmd_path(:tasks, current_params.except(toggle_key))
  else
    cmd_path(:tasks, current_params(toggle_h))
  end
end

#toggled_sort_orderObject



6
7
8
# File 'app/helpers/command_proposal/params_helper.rb', line 6

def toggled_sort_order
  params[:order] == "desc" ? "asc" : "desc"
end

#true_param?(*param_keys) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'app/helpers/command_proposal/params_helper.rb', line 38

def true_param?(*param_keys)
  truthy?(params&.dig(*param_keys))
end

#truthy?(val) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'app/helpers/command_proposal/params_helper.rb', line 34

def truthy?(val)
  val.to_s.downcase.in?(["true", "t", "1"])
end