Class: SolidQueueMonitor::BasePresenter

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::DateHelper, ActionView::Helpers::TextHelper
Defined in:
app/presenters/solid_queue_monitor/base_presenter.rb

Instance Method Summary collapse

Instance Method Details

#calculate_visible_pages(current_page, total_pages) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/presenters/solid_queue_monitor/base_presenter.rb', line 60

def calculate_visible_pages(current_page, total_pages)
  if total_pages <= 7
    (1..total_pages).to_a
  else
    case current_page
    when 1..3
      [1, 2, 3, 4, :gap, total_pages]
    when (total_pages - 2)..total_pages
      [1, :gap, total_pages - 3, total_pages - 2, total_pages - 1, total_pages]
    else
      [1, :gap, current_page - 1, current_page, current_page + 1, :gap, total_pages]
    end
  end
end

#default_url_optionsObject



10
11
12
# File 'app/presenters/solid_queue_monitor/base_presenter.rb', line 10

def default_url_options
  { only_path: true }
end

#engine_mount_pointObject

Helper method to get the mount point of the engine



117
118
119
120
121
122
123
124
# File 'app/presenters/solid_queue_monitor/base_presenter.rb', line 117

def engine_mount_point
  path_parts = request_path.split('/')
  if path_parts.length >= 3
    "/#{path_parts[1]}/#{path_parts[2]}"
  else
    '/solid_queue'
  end
end

#format_arguments(arguments) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/presenters/solid_queue_monitor/base_presenter.rb', line 81

def format_arguments(arguments)
  return '-' if arguments.blank?

  # For ActiveJob format
  if arguments.is_a?(Hash) && arguments['arguments'].present?
    return "<code>#{arguments['arguments'].inspect}</code>"
  elsif arguments.is_a?(Array) && arguments.length == 1 && arguments[0].is_a?(Hash) && arguments[0]['arguments'].present?
    return "<code>#{arguments[0]['arguments'].inspect}</code>"
  end

  # For regular arguments format
  "<code>#{arguments.inspect}</code>"
end

#format_datetime(datetime) ⇒ Object



75
76
77
78
79
# File 'app/presenters/solid_queue_monitor/base_presenter.rb', line 75

def format_datetime(datetime)
  return '-' unless datetime

  datetime.strftime('%Y-%m-%d %H:%M:%S')
end

#format_hash(hash) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'app/presenters/solid_queue_monitor/base_presenter.rb', line 95

def format_hash(hash)
  return '-' if hash.blank?

  formatted = hash.map do |key, value|
    "<strong>#{key}:</strong> #{value.to_s.truncate(50)}"
  end.join(', ')

  "<code>#{formatted}</code>"
end

#generate_pagination(current_page, total_pages) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/presenters/solid_queue_monitor/base_presenter.rb', line 24

def generate_pagination(current_page, total_pages)
  return '' if total_pages <= 1

  html = '<div class="pagination">'

  # Previous page link
  html += if current_page > 1
            "<a href=\"?page=#{current_page - 1}#{query_params}\" class=\"pagination-link pagination-nav\">Previous</a>"
          else
            '<span class="pagination-link pagination-nav disabled">Previous</span>'
          end

  # Page links
  visible_pages = calculate_visible_pages(current_page, total_pages)

  visible_pages.each do |page|
    html += if page == :gap
              '<span class="pagination-gap">...</span>'
            elsif page == current_page
              "<span class=\"pagination-current\">#{page}</span>"
            else
              "<a href=\"?page=#{page}#{query_params}\" class=\"pagination-link\">#{page}</a>"
            end
  end

  # Next page link
  html += if current_page < total_pages
            "<a href=\"?page=#{current_page + 1}#{query_params}\" class=\"pagination-link pagination-nav\">Next</a>"
          else
            '<span class="pagination-link pagination-nav disabled">Next</span>'
          end

  html += '</div>'
  html
end

#request_pathObject

Helper method to get the current request path



106
107
108
109
110
111
112
113
114
# File 'app/presenters/solid_queue_monitor/base_presenter.rb', line 106

def request_path
  # Try to get the current path from the controller's request
  if defined?(controller) && controller.respond_to?(:request)
    controller.request.path
  else
    # Fallback to a default path if we can't get the current path
    '/solid_queue'
  end
end

#section_wrapper(_title, content) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'app/presenters/solid_queue_monitor/base_presenter.rb', line 14

def section_wrapper(_title, content)
  <<-HTML
    <div class="section-wrapper">
      <div class="section">
        #{content}
      </div>
    </div>
  HTML
end