Module: Karafka::Web::Ui::Helpers::ApplicationHelper

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

Overview

Application-wide helpers: navigation, layout, icons, empty states and generic hash utilities. More specific concerns live in dedicated helpers ([[SortingHelper]], [[FormattingHelper]], [[BadgesHelper]], [[PartitionsHelper]]).

Instance Method Summary collapse

Instance Method Details

#deep_merge(hash1, hash2) ⇒ Hash

Merges two hashes deeply, combining nested hashes recursively.

Parameters:

  • hash1 (Hash)

    The first hash to merge.

  • hash2 (Hash)

    The second hash to merge.

Returns:

  • (Hash)

    A new hash that is the result of a deep merge of the two provided hashes.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/karafka/web/ui/helpers/application_helper.rb', line 78

def deep_merge(hash1, hash2)
  merged_hash = hash1.dup

  hash2.each_pair do |k, v|
    tv = merged_hash[k]

    merged_hash[k] = if tv.is_a?(Hash) && v.is_a?(Hash)
      deep_merge(tv, v)
    else
      v
    end
  end

  merged_hash
end

#empty_state(message, icon: "inbox", description: nil, action: nil, action_path: nil, card: nil, &block) ⇒ String

Renders the standardized "nothing here" empty state (icon + message, optionally with a description and a call-to-action button). The description can also be given as a block, in which case it may contain arbitrary markup (e.g. a checklist).

Parameters:

  • message (String)

    primary message describing what's empty

  • icon (String, Symbol) (defaults to: "inbox")

    one of: inbox, document, search, folder, key

  • description (String, nil) (defaults to: nil)

    optional secondary, smaller explanatory text

  • action (String, nil) (defaults to: nil)

    optional call-to-action button label

  • action_path (String, nil) (defaults to: nil)

    path the call-to-action button should link to

  • card (Boolean, nil) (defaults to: nil)

    whether to wrap the state in a bordered card (default true)

  • block (Proc)

    optional block producing the description markup

Returns:

  • (String)

    empty state html



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/karafka/web/ui/helpers/application_helper.rb', line 49

def empty_state(
  message,
  icon: "inbox",
  description: nil,
  action: nil,
  action_path: nil,
  card: nil,
  &block
)
  description = capture_erb(&block) if block

  inject_erb partial(
    "shared/empty_state",
    locals: {
      message: message,
      icon_name: icon,
      description: description,
      action: action,
      action_path: action_path,
      card: card
    }
  )
end

#icon(name, size: nil) ⇒ String

Renders the svg icon out of our icon set

Parameters:

  • name (String, Symbol)

    name of the icon

  • size (Integer, nil) (defaults to: nil)

    optional size override (defaults to the icon's own size)

Returns:

  • (String)

    svg icon



33
34
35
# File 'lib/karafka/web/ui/helpers/application_helper.rb', line 33

def icon(name, size: nil)
  render "shared/icons/_#{name}", locals: { size: size }
end

Adds active class to the current location in the nav if needed

Parameters:

  • location (Hash)


14
15
16
17
18
19
# File 'lib/karafka/web/ui/helpers/application_helper.rb', line 14

def nav_class(location)
  comparator, value = location.to_a.first

  local_location = request.path.gsub(env.fetch("SCRIPT_NAME"), "")
  local_location.public_send(:"#{comparator}?", value) ? "active" : ""
end

#view_title(title) ⇒ String

Sets the particular page title

Parameters:

  • title (String)

    page title

Returns:

  • (String)

    title html



25
26
27
# File 'lib/karafka/web/ui/helpers/application_helper.rb', line 25

def view_title(title)
  content_for(:title) { title }
end