Module: Karafka::Web::Ui::Helpers::PathsHelper

Included in:
Base
Defined in:
lib/karafka/web/ui/helpers/paths_helper.rb

Overview

Helper for web ui paths builders

Instance Method Summary collapse

Instance Method Details

#action?(*args) ⇒ Boolean

Method that can be used to have conditions in breadcrumbs, etc based on the action we’re in

Parameters:

  • args (Array<Symbol>)

    action names we want to check

Returns:

  • (Boolean)

    true if any matches the current action name



14
15
16
# File 'lib/karafka/web/ui/helpers/paths_helper.rb', line 14

def action?(*args)
  args.include?(@current_action_name)
end

#asset_path(local_path) ⇒ String

Generates a full path to any asset with our web-ui version. We ship all assets with the version in the url to prevent those assets from being used after update. After each web-ui update, assets are going to be re-fetched as the url will change

Parameters:

  • local_path (String)

    local path to the asset

Returns:

  • (String)

    full path to the asst including correct root path



60
61
62
# File 'lib/karafka/web/ui/helpers/paths_helper.rb', line 60

def asset_path(local_path)
  root_path("assets/#{Karafka::Web::VERSION}/#{local_path}")
end

#consumer_path(consumer_id, *args) ⇒ String

Helps build per-consumer scope paths

Parameters:

  • consumer_id (String)

    consumer process id

  • args (Array<String>)

    other path components

Returns:

  • (String)

    per consumer specific path



109
110
111
# File 'lib/karafka/web/ui/helpers/paths_helper.rb', line 109

def consumer_path(consumer_id, *args)
  consumers_path(consumer_id, *args)
end

#consumers_path(*args) ⇒ String

Helps build consumers paths

Parameters:

  • args (Array<String>)

    path params for consumers scope

Returns:

  • (String)

    consumers scope path



100
101
102
# File 'lib/karafka/web/ui/helpers/paths_helper.rb', line 100

def consumers_path(*args)
  root_path('consumers', *args)
end

#explorer_messages_path(*args) ⇒ String

Generates routes for explorer messages paths

Parameters:

  • args (Array<String>)

    sub-paths

Returns:

  • (String)

    explorer messages path



84
85
86
# File 'lib/karafka/web/ui/helpers/paths_helper.rb', line 84

def explorer_messages_path(*args)
  explorer_path(*['messages', args.compact].flatten)
end

#explorer_path(*args) ⇒ String

Helps build explorer paths. We often link offsets to proper messages, etc so this allows us to short-track this

Parameters:

  • args (Array<String>)

    sub-paths

Returns:

  • (String)

    path to the expected location



68
69
70
# File 'lib/karafka/web/ui/helpers/paths_helper.rb', line 68

def explorer_path(*args)
  root_path(*['explorer', args.compact].flatten)
end

#explorer_topics_path(*args) ⇒ Object

Generates routes for explorer topics paths

Parameters:

  • args (Array<String>)

    sub-paths

Returns:

  • explorer topics path



76
77
78
# File 'lib/karafka/web/ui/helpers/paths_helper.rb', line 76

def explorer_topics_path(*args)
  explorer_path(*['topics', args.compact].flatten)
end

#flatten_params(prefix, hash, result = {}) ⇒ Hash

Helper method to flatten nested hashes and arrays

Parameters:

  • prefix (String)

    The prefix for nested keys, initially an empty string.

  • hash (Hash, Array)

    The nested hash or array to be flattened.

  • result (Hash) (defaults to: {})

    The hash to store the flattened key-value pairs.

Returns:

  • (Hash)

    The flattened hash with keys in bracket notation suitable for URL encoding.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/karafka/web/ui/helpers/paths_helper.rb', line 24

def flatten_params(prefix, hash, result = {})
  if hash.is_a?(Hash)
    hash.each do |k, v|
      new_prefix = prefix.empty? ? k.to_s : "#{prefix}[#{k}]"
      flatten_params(new_prefix, v, result)
    end
  elsif hash.is_a?(Array)
    hash.each_with_index do |v, i|
      new_prefix = "#{prefix}[#{i}]"
      flatten_params(new_prefix, v, result)
    end
  else
    result[prefix] = hash.to_s
  end

  result
end

#root_path(*args) ⇒ String

Note:

This needs to be done that way with the #root_path because the web UI can be mounted in a sub-path and we need to make sure our all paths are relative to “our” root, not the root of the app in which it was mounted.

Generates a full path with the root path out of the provided arguments

Parameters:

  • args (Array<String, Numeric>)

    arguments that will make the path

Returns:

  • (String)

    path from the root



50
51
52
# File 'lib/karafka/web/ui/helpers/paths_helper.rb', line 50

def root_path(*args)
  "#{env.fetch('SCRIPT_NAME')}/#{args.join('/')}"
end

#scheduled_messages_explorer_path(topic_name = nil, partition_id = nil, offset = nil, action = nil) ⇒ String

Helps build scheduled messages paths. Similar to the explorer helper one

Parameters:

  • topic_name (String) (defaults to: nil)
  • partition_id (Integer, nil) (defaults to: nil)
  • offset (Integer, nil) (defaults to: nil)
  • action (String, nil) (defaults to: nil)

Returns:

  • (String)

    path to the expected location



120
121
122
123
124
125
126
127
128
129
# File 'lib/karafka/web/ui/helpers/paths_helper.rb', line 120

def scheduled_messages_explorer_path(
  topic_name = nil,
  partition_id = nil,
  offset = nil,
  action = nil
)
  root_path(
    *['scheduled_messages', 'explorer', 'topics', topic_name, partition_id, offset, action].compact
  )
end

#topics_path(*args) ⇒ String

Helps build topics paths

Parameters:

  • args (Array<String>)

    path params for the topics scope

Returns:

  • (String)

    topics scope path



92
93
94
# File 'lib/karafka/web/ui/helpers/paths_helper.rb', line 92

def topics_path(*args)
  root_path('topics', *args)
end