activeadmin-menu_tree 0.1.2 → 0.2.2

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: b04ec690f0cea481508a2359ede3943c33c5353e32fd1ae07d0da5e43fc66a8a
4
- data.tar.gz: b8d28caed2ce7f24facabfd707b96e710d84a20a0ec96d89350f605e73763406
3
+ metadata.gz: f4c16b5f354db79d26599238e1b80d2bfb978c029a9209ea61ee37aaa5627f9e
4
+ data.tar.gz: 37937d45abb4268ec1a4ce475ab5b1c0cd4e0e5218ab7aeff5f43966945b209b
5
5
  SHA512:
6
- metadata.gz: ff9dd6bf9962dda1a7b96c3c42dbf7b369f3d74d13ee8ff660129f74e9d77ea66c43bd928a28eeb8b9ca802e240eaf1a612f903155f031f8f027e93de02ebf7d
7
- data.tar.gz: 697f1a4fd51a1832c2cfce26d363ee3f328cc953580a93229b4e3226145a0fbc4767dca6e02f025b4b23fbf1139bf5b0da29edce9b213ed4aada24f6e8a9ebb8
6
+ metadata.gz: 6ea399628b6146c8232f6af9162960bf83afa0cdd97a49809ee114c32467d3a476fd111c24019aab5325af184d78fae58467b41bcb7c41d82602af342a2bd5d5
7
+ data.tar.gz: 1d5382a9f0b3957688cf218ead03e55e04fe1f9d4846969dde586ba18ae832a8a00bddd47bec1c8b57834372c0fc08b07723783cc47222c96a8b357b2e5aabb9
@@ -18,27 +18,27 @@ jobs:
18
18
  - name: Set up Ruby
19
19
  uses: ruby/setup-ruby@v1
20
20
  with:
21
- ruby-version: 3.0.0
21
+ ruby-version: 3.1
22
22
  - name: Install bundler
23
23
  run: gem install bundler
24
24
  - name: Setup
25
25
  run: bin/setup
26
26
  - name: Rubocop
27
27
  run: bundle exec rake rubocop
28
- steep:
29
- runs-on: ubuntu-latest
30
- steps:
31
- - uses: actions/checkout@v2
32
- - name: Set up Ruby
33
- uses: ruby/setup-ruby@v1
34
- with:
35
- ruby-version: 3.0.0
36
- - name: Install bundler
37
- run: gem install bundler
38
- - name: Setup
39
- run: bin/setup
40
- - name: Steep
41
- run: bundle exec steep check
28
+ # steep:
29
+ # runs-on: ubuntu-latest
30
+ # steps:
31
+ # - uses: actions/checkout@v2
32
+ # - name: Set up Ruby
33
+ # uses: ruby/setup-ruby@v1
34
+ # with:
35
+ # ruby-version: 3.1
36
+ # - name: Install bundler
37
+ # run: gem install bundler
38
+ # - name: Setup
39
+ # run: bin/setup
40
+ # - name: Steep
41
+ # run: bundle exec steep check
42
42
  rspec:
43
43
  runs-on: ubuntu-latest
44
44
  strategy:
@@ -46,6 +46,7 @@ jobs:
46
46
  ruby:
47
47
  - 2.7
48
48
  - 3.0
49
+ - 3.1
49
50
  steps:
50
51
  - uses: actions/checkout@v2
51
52
  - name: Set up Ruby
data/.rubocop.yml CHANGED
@@ -27,6 +27,9 @@ Style/StringLiteralsInInterpolation:
27
27
  Enabled: true
28
28
  EnforcedStyle: double_quotes
29
29
 
30
+ Style/TrailingCommaInArrayLiteral:
31
+ Enabled: false
32
+
30
33
  Style/TrailingCommaInHashLiteral:
31
34
  Enabled: false
32
35
 
data/Gemfile CHANGED
@@ -12,4 +12,4 @@ gem "rubocop", "~> 1.0", require: false
12
12
  gem "rubocop-rake", require: false
13
13
  gem "rubocop-rspec", require: false
14
14
  gem "rubocop-rubycw", require: false
15
- gem "steep"
15
+ gem "steep", ">= 0.47.0"
data/README.md CHANGED
@@ -46,12 +46,12 @@ Write the configuration in a yaml file.
46
46
  # config/activeadmin-menu_tree.yml or anywhere you like
47
47
  activeadmin:
48
48
  menu_tree:
49
- - name: Dashboard
49
+ - id: Dashboard
50
50
  - label: Admin
51
51
  children:
52
- - name: AdminUser
52
+ - id: AdminUser
53
53
  label: Admin Users
54
- - name: Comment
54
+ - id: Comment
55
55
  label: Admin Comments
56
56
  ```
57
57
 
@@ -86,19 +86,22 @@ end
86
86
 
87
87
  ### Other ways to load configuration
88
88
 
89
- It is also possible to simply use hash instead of yaml.
89
+ It is also possible to simply use hash instead of yaml.
90
+ If you want to use dynamic specification using `proc` in menu_tree (instead of in ActiveAdmin Resource), you may want to use this one.
91
+
90
92
  ```ruby
91
93
  ActiveAdmin::MenuTree.setup do |config|
92
- config.menu_tree = [{
93
- name: "Dashboard"
94
- }, {
95
- label: "Foo",
96
- children: [{
97
- name: "Bar"
98
- }, {
99
- name: "Baz"
100
- }]
101
- }]
94
+ config.menu_tree = [
95
+ { id: "Dashboard", label: proc { I18n.t("active_admin.dashboard") } },
96
+ {
97
+ label: "Foo",
98
+ if: proc { "Something dynamic" },
99
+ children: [
100
+ { id: "Bar" },
101
+ { id: "Baz" }
102
+ ]
103
+ }
104
+ ]
102
105
  end
103
106
  ```
104
107
 
@@ -118,36 +121,53 @@ activeadmin:
118
121
  Or you can use other configuration gems like [global gem](https://github.com/railsware/global) by converting them to hash as well.
119
122
 
120
123
  ### Full configuration example
124
+
121
125
  ```yaml
122
126
  activeadmin:
123
127
  menu_tree:
124
- # Specify the resource name with `name`.
125
- - name: Dashboard
126
- - name: Product
128
+ # Specify the resource with `id`.
129
+ - id: Dashboard
130
+ - id: Product
127
131
  # Specify a menu label with `label`.
128
132
  - label: User Info
129
133
  # Specify child elements with `children`.
130
134
  children:
131
- - name: User
132
- - name: Profile
135
+ - id: User
136
+ - id: Profile
133
137
  - label: Admin
134
138
  children:
135
- - name: AdminUser
139
+ - id: AdminUser
136
140
  label: Admin Users
137
141
  # Comment resource will be handled specially.
138
- - name: Comment
142
+ - id: Comment
139
143
  label: Admin Comments
140
144
  - label: Others
141
145
  children:
142
- - name: Foo
143
- - name: Bar
146
+ - id: Foo
147
+ - id: Bar
144
148
  - label: Example Site
145
149
  # You can pass the other options available for `menu` DSL, like `url`, `html_options`.
146
150
  url: 'https://example.com'
147
151
  html_options:
148
152
  target: blank
153
+ # Nesting of children is also available.
154
+ - label: Lorem
155
+ children:
156
+ - label: ipsum
157
+ children:
158
+ - label: dolor
159
+ children:
160
+ - label: sit
161
+ children:
162
+ - label: amet
163
+ url: 'https://wikipedia.org/wiki/Lorem_ipsum'
164
+ html_options:
165
+ target: blank
149
166
  ```
150
167
 
168
+ <img width="1573" alt="screenshot" src="https://user-images.githubusercontent.com/7542105/153759374-fd516cb8-8022-4e44-aad9-c97f77afc4e7.png">
169
+
170
+
151
171
  ## Development
152
172
 
153
173
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -14,25 +14,22 @@ module ActiveAdmin::MenuTree
14
14
  raise ActiveAdmin::MenuTree::Error, "Invalid config" unless new_value.is_a? Array
15
15
 
16
16
  @menu_tree = new_value.map(&:deep_symbolize_keys)
17
- @menu_options = flatten_menu_tree
17
+ @menu_options = flatten_options(@menu_tree)
18
18
  end
19
19
 
20
- def find_menu_option(name:)
21
- menu_options.find { |item| item[:name] == name }
20
+ def find_menu_option(id:)
21
+ menu_options.find { |item| item[:id] == id }
22
22
  end
23
23
 
24
24
  private
25
25
 
26
- def flatten_menu_tree
27
- menu_tree.map.with_index(1) do |item, index|
28
- options = format_options(item, index: index)
26
+ def flatten_options(items, parent: nil)
27
+ items.map.with_index(1) do |item, index|
28
+ options = format_options(item, index: index, parent: parent)
29
29
  next options unless item[:children].is_a? Array
30
30
 
31
- children =
32
- item[:children].map.with_index(1) do |child, child_index|
33
- format_options(child, index: child_index, parent: item[:label])
34
- end
35
-
31
+ next_parent = parent ? [parent, item[:label]].flatten.compact : item[:label]
32
+ children = flatten_options(item[:children], parent: next_parent)
36
33
  [options] + children
37
34
  end.flatten.compact
38
35
  end
@@ -40,8 +37,11 @@ module ActiveAdmin::MenuTree
40
37
  def format_options(item, index:, parent: nil)
41
38
  # TODO: validate option
42
39
  options = item.except(:children)
40
+ if item.key?(:name)
41
+ ActiveAdmin::MenuTree.warn_deprecated("Use `id` key, instead of `name`.")
42
+ options[:id] ||= item[:name]
43
+ end
43
44
  options[:priority] = index * 10
44
- options[:id] ||= item[:name]
45
45
  options[:parent] = parent if parent.present?
46
46
  options
47
47
  end
@@ -4,8 +4,7 @@ module ActiveAdmin::MenuTree
4
4
  # ActiveAdmin::MenuTree::DSL class
5
5
  module DSL
6
6
  def menu_tree(**args)
7
- options = menu_tree_config.find_menu_option(name: config.resource_name.name) || {}
8
- options = options.except(:name)
7
+ options = menu_tree_config.find_menu_option(id: config.resource_name.name) || {}
9
8
  options = options.merge(args)
10
9
  menu(**options)
11
10
  end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveAdmin
4
+ module MenuTree
5
+ # ActiveAdmin::MenuTree::Logging module
6
+ module Logging
7
+ def debug?
8
+ ENV["MENU_TREE_DEBUG"]
9
+ end
10
+
11
+ def log_debug(message)
12
+ warn("[ActiveAdmin::MenuTree] #{message}") if debug?
13
+ end
14
+
15
+ def warn_deprecated(message)
16
+ warn("[ActiveAdmin::MenuTree] [DEPRECATION] #{message}")
17
+ end
18
+ end
19
+ end
20
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveAdmin
4
4
  module MenuTree
5
- VERSION = "0.1.2"
5
+ VERSION = "0.2.2"
6
6
  end
7
7
  end
@@ -4,6 +4,7 @@ require "active_support"
4
4
  require "active_support/core_ext"
5
5
 
6
6
  require_relative "menu_tree/version"
7
+ require_relative "menu_tree/logging"
7
8
  require_relative "menu_tree/config"
8
9
  require_relative "menu_tree/dsl"
9
10
 
@@ -13,6 +14,8 @@ module ActiveAdmin
13
14
  class Error < StandardError; end
14
15
 
15
16
  class << self
17
+ include ActiveAdmin::MenuTree::Logging
18
+
16
19
  def setup
17
20
  raise ActiveAdmin::MenuTree::Error, "No block given, require a block" unless block_given?
18
21
 
@@ -32,13 +35,13 @@ module ActiveAdmin
32
35
  private
33
36
 
34
37
  def setup_menu_options(aa_config)
35
- comments_menu = config.find_menu_option(name: "Comment")
38
+ comments_menu = config.find_menu_option(id: "Comment")
36
39
  aa_config.comments_menu = comments_menu if comments_menu.present?
37
40
 
38
41
  menu_options = config.menu_options
39
- .reject{ |item| item[:name] == "Comment" }
40
- .map{ |item| item.except(:name) }
42
+ .reject{ |item| item[:id] == "Comment" }
41
43
 
44
+ ActiveAdmin::MenuTree.log_debug("menu_options: #{menu_options.inspect}")
42
45
  aa_config.namespace :admin do |admin|
43
46
  admin.build_menu do |menu|
44
47
  menu_options.each do |options|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activeadmin-menu_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - shuuuuuny
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-11-21 00:00:00.000000000 Z
11
+ date: 2022-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activeadmin
@@ -61,6 +61,7 @@ files:
61
61
  - lib/activeadmin/menu_tree.rb
62
62
  - lib/activeadmin/menu_tree/config.rb
63
63
  - lib/activeadmin/menu_tree/dsl.rb
64
+ - lib/activeadmin/menu_tree/logging.rb
64
65
  - lib/activeadmin/menu_tree/version.rb
65
66
  - rbs_collection.lock.yaml
66
67
  - rbs_collection.yaml
@@ -93,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
94
  - !ruby/object:Gem::Version
94
95
  version: '0'
95
96
  requirements: []
96
- rubygems_version: 3.2.31
97
+ rubygems_version: 3.3.3
97
98
  signing_key:
98
99
  specification_version: 4
99
100
  summary: Allows ActiveAdmin menus to be managed in tree format.