katalyst-navigation 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6424bb96df0ceb96b40aa6d225dbe12461784edb41c1b83863435a56db02fd68
4
- data.tar.gz: 6a46bbd339f8c171fe7a9791ad94281c6e0cb6f846f8bab04bd62edb8e8c339c
3
+ metadata.gz: 368bcecb2264a1a536d2bbeb6c05f2dfa6762dcd693a215db21ef93db4da26f8
4
+ data.tar.gz: 83ef6762cddd830df2acc4ef1404c8037211c4de4b3dbe5d8b5501bb4cf42fd7
5
5
  SHA512:
6
- metadata.gz: 173290becb5111777b887baeefe3a93642d8b154cb6463fd0a84a83e0246be7159568cc836d3eb2ca4ca7b9d7a2b616eff69c284d7abbaa93f7f2a26e0f81f76
7
- data.tar.gz: cad3f1cf87f00b7868aac073b71d192345b341feb73a0d28de332fccd932278988c256cc9228d2f30eadc55b2de0c4070d8a263c323e235b22f18f087bbc9abb
6
+ metadata.gz: a4e9965e0b7e49c055e405d90dd13dcb764f2b4b1b0fb62191347eb066e784dea5c1ee069b9a848fe283d87da1241fa840663d8152a92fe2b1f73607bae11b9b
7
+ data.tar.gz: df13a7692f558a2eb5937a4e85d7137540d175ca6be500a743bfbd1caa53fc4fbba1be0acc27cc6bedf665009a8ff58439bc1d2fc35ae76028059ec0ea0614f4
@@ -41,8 +41,17 @@ module Katalyst
41
41
  params[:type] || Link.name
42
42
  end
43
43
 
44
+ def item_params_type
45
+ type = params.require(:item).fetch(:type, "")
46
+ if Katalyst::Navigation.config.items.include?(type)
47
+ type.safe_constantize
48
+ else
49
+ Item
50
+ end
51
+ end
52
+
44
53
  def item_params
45
- params.require(:item).permit(%i[title url visible http_method new_tab type])
54
+ params.require(:item).permit(item_params_type.permitted_params)
46
55
  end
47
56
 
48
57
  def set_menu
@@ -4,11 +4,10 @@ module Katalyst
4
4
  module Navigation
5
5
  module EditorHelper
6
6
  def navigation_editor_new_items(menu)
7
- [
8
- Heading.new(menu: menu),
9
- Link.new(menu: menu),
10
- Button.new(menu: menu),
11
- ]
7
+ Katalyst::Navigation.config.items.map do |item_class|
8
+ item_class = item_class.is_a?(String) ? item_class.safe_constantize : item_class
9
+ item_class.new(menu: menu)
10
+ end
12
11
  end
13
12
 
14
13
  def navigation_editor_menu(menu:, **options, &block)
@@ -1,22 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # rubocop:disable Rails/HelperInstanceVariable
3
4
  module Katalyst
4
5
  module Navigation
5
6
  module Frontend
6
7
  class Builder
7
- attr_accessor :template, :menu_options, :list_options, :item_options
8
+ attr_accessor :template
8
9
 
9
10
  delegate_missing_to :@template
10
11
 
11
12
  def initialize(template, list: {}, item: {}, **menu_options)
12
- self.template = template
13
- self.menu_options = menu_options
14
- self.list_options = list
15
- self.item_options = item
13
+ self.template = template
14
+ @menu_options = menu_options.freeze
15
+ @list_options = list.freeze
16
+ @item_options = item.freeze
16
17
  end
17
18
 
18
19
  def render(tree)
19
- tag.ul **menu_options do
20
+ tag.ul(**menu_options(tree)) do
20
21
  tree.each do |item|
21
22
  concat render_item(item)
22
23
  end
@@ -26,32 +27,47 @@ module Katalyst
26
27
  def render_item(item)
27
28
  return unless item.visible?
28
29
 
29
- tag.li **item_options do
30
+ tag.li(**item_options(item)) do
30
31
  concat public_send("render_#{item.model_name.param_key}", item)
31
- concat render_list(item.children) if item.children.any?
32
+ concat render_children(item) if item.children.any?
32
33
  end
33
34
  end
34
35
 
35
- def render_list(items)
36
- tag.ul **list_options do
37
- items.each do |child|
36
+ def render_children(item)
37
+ tag.ul(**list_options(item)) do
38
+ item.children.each do |child|
38
39
  concat render_item(child)
39
40
  end
40
41
  end
41
42
  end
42
43
 
43
44
  def render_heading(heading)
44
- tag.header { tag.p heading.title }
45
+ tag.span(heading.title)
45
46
  end
46
47
 
47
48
  def render_link(link)
48
- link_to(link.title, link.url, item_options)
49
+ link_to(link.title, link.url)
49
50
  end
50
51
 
51
52
  def render_button(link)
52
- link_to(link.title, link.url, **item_options, method: link.http_method)
53
+ link_to(link.title, link.url, method: link.http_method)
54
+ end
55
+
56
+ private
57
+
58
+ def menu_options(_tree)
59
+ @menu_options
60
+ end
61
+
62
+ def list_options(_item)
63
+ @list_options
64
+ end
65
+
66
+ def item_options(_item)
67
+ @item_options
53
68
  end
54
69
  end
55
70
  end
56
71
  end
57
72
  end
73
+ # rubocop:enable Rails/HelperInstanceVariable
@@ -10,6 +10,10 @@ module Katalyst
10
10
 
11
11
  validates :title, :url, :http_method, presence: true
12
12
  validates :http_method, inclusion: { in: HTTP_METHODS.values.map(&:to_s) }
13
+
14
+ def self.permitted_params
15
+ super + %i[http_method]
16
+ end
13
17
  end
14
18
  end
15
19
  end
@@ -10,6 +10,16 @@ module Katalyst
10
10
 
11
11
  attr_accessor :parent, :children, :index, :depth
12
12
 
13
+ def self.permitted_params
14
+ %i[
15
+ title
16
+ url
17
+ visible
18
+ new_tab
19
+ type
20
+ ]
21
+ end
22
+
13
23
  def layout?
14
24
  is_a? Layout
15
25
  end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/configurable"
4
+
5
+ module Katalyst
6
+ module Navigation
7
+ class Config
8
+ include ActiveSupport::Configurable
9
+
10
+ config_accessor(:items) do
11
+ %w[
12
+ Katalyst::Navigation::Heading
13
+ Katalyst::Navigation::Link
14
+ Katalyst::Navigation::Button
15
+ ]
16
+ end
17
+ end
18
+ end
19
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Katalyst
4
4
  module Navigation
5
- VERSION = "1.2.0"
5
+ VERSION = "1.3.0"
6
6
  end
7
7
  end
@@ -1,9 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "katalyst/navigation/config"
3
4
  require "katalyst/navigation/engine"
4
5
  require "katalyst/navigation/version"
5
6
 
6
7
  module Katalyst
7
8
  module Navigation # :nodoc:
9
+ extend self
10
+
11
+ def config
12
+ @config ||= Config.new
13
+ end
14
+
15
+ def configure
16
+ yield config
17
+ end
8
18
  end
9
19
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: katalyst-navigation
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katalyst Interactive
@@ -78,6 +78,7 @@ files:
78
78
  - db/migrate/20220826034507_create_katalyst_navigation_items.rb
79
79
  - db/migrate/20220908044500_add_depth_limit_to_menus.rb
80
80
  - lib/katalyst/navigation.rb
81
+ - lib/katalyst/navigation/config.rb
81
82
  - lib/katalyst/navigation/engine.rb
82
83
  - lib/katalyst/navigation/version.rb
83
84
  - lib/tasks/yarn.rake