menuizer 0.1.9 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NGFkZjNiZjMxOGE4NTEzMDZkYjA4ZjNhMmU4MmZkZWJiNDM5Y2QyOA==
4
+ NDRiOGYzNzI1NjcyZTg1NmE2YjJiZjcwN2MwZWYwOGE5YWI0OWNhZA==
5
5
  data.tar.gz: !binary |-
6
- NDA4MjA2MTg3MTY2ZDZlZjhkYTlkN2Y5NDFjN2UxNjE0ZWRhM2YxMw==
6
+ MWRhY2RmODdlZDIwNmYyYzgyYTg2ODE3MTdiZWIyN2RhZTNlNGEyMA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NTU1ZjRkODFhMWMxYTE4ODljZmQ5YTAwZjZmYjA4ZDk3ODAzMTEzZTFlOWRm
10
- ZmM2NmY5YTgxMzk1NDllNDZhYjg3MzAzYTQwODc5ZTBjYTVkMThkZGU3NmUz
11
- Nzc5NDlkOTMyOGRjMDQ4YTYxMmJmYzE0MjAyNDIzNWMyNTJhMjc=
9
+ YzIyOGZhNjdiZjI4YWIwZGQ1NzZhNzBmN2YxNDQ4MjFhYWIwNGYyNDJlZjE2
10
+ ZGU0MGYyYjA4ODY1MDE1MWEzMWM5Zjk1ZjQ5NGMzYmUyOGUzMjMzYTI1YTMx
11
+ MGE0OTAyZWEwM2I0NGRhZTMyNzg2NmUwYTY5OTc4ZTg3ZDkzMWY=
12
12
  data.tar.gz: !binary |-
13
- NTU1M2MzNzIxNTNiN2MwOTE4MzZmYjIyYzRmOGE4NWZlMzYwMDhmYTc4OTQ4
14
- ZGY1MTE4MjMyYjhjMmE1ZmFjYzg5NDdjM2EzYzRkZmY2OGViMThjZDQ5NzUw
15
- M2QwNzE5NDAxN2U4MTRlMzEwMDI5YjFkMzk2NjdmZTYzODc3MDg=
13
+ OThlNTIyZDQ1YWJiNzFiYzNmMmMzZTZmODQ2MzYxOGVjMzk2MmUyOTEyYmU3
14
+ NTI2OTA2MWRhYjczMjBjMTIzMjMzYjBkZmM0NzU2ZWQ5ZTU4ZTIwMWIwZmJk
15
+ NDg1MWM0NmYyYzYyNmQzZjE5OTFkYjgxNjZiNGY3NTY1NDVkYzc=
data/README.md CHANGED
@@ -25,38 +25,68 @@ Or install it yourself as:
25
25
 
26
26
  ```ruby
27
27
  # config/initializers/menuizer.rb
28
- Menuizer.configure do |menu|
29
- menu.header "MAIN NAVIGATION"
30
- menu.item "Dashboard", icon: "fa fa-dashboard" do
31
- menu.item "Dashboard v1", icon: "fa fa-circle-o"
32
- menu.item "Dashboard v2", icon: "fa fa-circle-o"
33
- end
34
-
35
- menu.item Widget, icon: "fa fa-th"
36
-
37
- menu.item "Settings", icon: "fa fa-cog" do
38
- menu.item Admin, icon: "fa fa-circle-o"
39
- menu.item User, icon: "fa fa-circle-o", notices: [
40
- ->{ [:warning, User.unauthorized.count] },
41
- ->{ [:danger, User.wait.count] },
42
- ]
43
-
44
- menu.item "nested" do
45
- menu.item "nested item", path: :path_to_somewhere, icon: "fa fa-circle-o"
28
+ Menuizer.configure do |config|
29
+ config.file_path = Rails.root.join("config/menuizer.yml")
30
+ end
31
+ ```
32
+
33
+ ```yaml
34
+ # config/menuizer.yml
35
+ - header: MAIN NAVIGATION
36
+ - item: Dashboard
37
+ icon: fa fa-dashboard
38
+ children:
39
+ - item: Dashboard v1
40
+ icon: fa fa-circle-o
41
+ - item: Dashboard v2
42
+ icon: fa fa-circle-o
43
+
44
+ - item: :Widget
45
+ icon: fa fa-th
46
+
47
+ - item: Settings
48
+ icon: fa fa-cog
49
+ children:
50
+ - item: :Admin
51
+ icon: fa fa-circle-o
52
+ - item: :User
53
+ icon: fa fa-circle-o
54
+ notice:
55
+ class: :User
56
+ method: :main_menu_notices
57
+
58
+ - item: nested
59
+ children:
60
+ - item: nested item
61
+ path: :path_to_somewhere
62
+ icon: fa fa-circle-o
63
+ ```
64
+
65
+ ```ruby
66
+ # app/controllers/application_controller.rb
67
+ class ApplicationController < ActionController::Base
68
+ before_action :set_menuizer
69
+
70
+ private
71
+
72
+ def set_menuizer
73
+ @menuizer = Menuizer.menu
74
+ end
75
+ helper_method def menuizer
76
+ @menuizer
46
77
  end
47
- end
48
78
  end
49
79
  ```
50
80
 
51
81
  ```erb
52
82
  <%# app/views/admins/index.html.erb %>
53
- <% @active_menu = Admin # first argument of menu.item %>
54
- <% content_for :title do %><%= Menuizer.menu.active_item.title %><% end %>
83
+ <% menuizer.activate :Admin # item's value %>
84
+ <% content_for :title do %><%= menuizer.active_item.try(:title) %><% end %>
55
85
 
56
86
  ...
57
87
 
58
88
  <ol class="breadcrumb">
59
- <% Menuizer.menu.active_items.each do |item| %>
89
+ <% menuizer.active_items.each do |item| %>
60
90
  <li><%= link_to item.path || "#" do %><i class="<%= item.icon %>"></i> <%= item.title %><% end %></li>
61
91
  <% end %>
62
92
  </ol>
@@ -64,21 +94,18 @@ end
64
94
 
65
95
  ```erb
66
96
  <%# app/views/layouts/application.html.erb %>
67
- <% Menuizer.menu.activate @active_menu %>
68
- <title><%= yield :title %></title>
69
-
70
97
  ...
71
-
72
98
  <ul class="sidebar-menu">
73
- <% Menuizer.menu.items.each do |item| %>
99
+ <% menuizer.items.each do |item| %>
74
100
  <%= render "menu", item: item %>
75
101
  <% end %>
76
102
  </ul>
103
+ ...
77
104
  ```
78
105
 
79
106
  ```erb
80
107
  <%
81
- item # Menuizer.menu.items's item
108
+ item # menuizer.items's item
82
109
  %>
83
110
  <% case item.type %>
84
111
  <% when :header %>
@@ -112,32 +139,49 @@ end
112
139
  <% end %>
113
140
  ```
114
141
 
115
- ## API
142
+ ## Converters
116
143
 
117
- ### menu.item
144
+ ```ruby
145
+ # config/initializers/menuizer.rb
146
+ Menuizer.configure do |config|
147
+ config.converter = {
148
+ icon: ->(icon,opts){
149
+ case
150
+ when icon.blank? || icon.starts_with?("fa") then icon
151
+ when icon then "fa fa-#{icon}"
152
+ else
153
+ "fa fa-circle-o"
154
+ end
155
+ },
156
+ }
157
+ end
158
+ ```
118
159
 
119
160
  ```ruby
120
- menu.item(
121
- title, # required : convert to item.title
122
- path: nil, # optional : convert to item.path
123
-
124
- # other keys pass-through to item.*
125
- icon: "fa fa-icon",
126
- notices: [
127
- ->{ [:info, count] },
128
- ],
129
- )
161
+ menuizer.items.each do |item|
162
+ item.icon #=> call converter
163
+ end
130
164
  ```
131
165
 
132
- #### `title` converting
166
+ icon, opts is yaml's original value
167
+ opts: all key-value hash
168
+
133
169
 
134
- * convert `title.model_name.human` if title respond to `model_name`
170
+ ### convert `model`
171
+
172
+ * set `:model` key if title is `Symbol` and respond to `model_name` ( ActiveRecord )
173
+
174
+
175
+ ### convert `title`
176
+
177
+ * convert `model.model_name.human` if model respond to `model_name.huuman`
135
178
  * or, leave `title`
136
179
 
137
180
 
138
- #### `path` converting
181
+ ### convert `path`
139
182
 
140
- * convert `:"#{namespace}#{title.model_name.plural}"` if title respond to `model_name`
183
+ * leave `path` if not `nil`
184
+ * convert `:"#{namespace}#{model.model_name.plural}"` if title respond to `model_name.plural`
141
185
  * or, leave `nil`
142
186
 
143
187
  **what namespace is?**
@@ -146,61 +190,37 @@ menu.item(
146
190
 
147
191
  ## Multiple namespaces
148
192
 
149
- if your rails application has multiple namespaces, and required multiple menues, pass `:namespace` to Menuizer methods.
193
+ If your rails application has multiple namespaces, and required multiple menues, pass `:namespace` to Menuizer methods.
150
194
 
151
195
  ```ruby
152
196
  # config/initializers/menuizer.rb
153
- Menuizer.configure(:namespace) do |menu|
154
- ...
197
+ Menuizer.configure(:namespace) do |config|
198
+ config.file_path = Rails.root.join("config/menuizer/namespace.yml")
155
199
  end
156
200
  ```
157
201
 
158
- ```erb
159
- <%# app/views/admins/index.html.erb %>
160
- <% @active_menu = Admin # first argument of menu.item %>
161
- <% content_for :title do %><%= Menuizer.menu(:namespace).active_item.title %><% end %>
162
-
163
- ...
164
-
165
- <ol class="breadcrumb">
166
- <% Menuizer.menu(:namespace).active_items.each do |item| %>
167
- <li><%= link_to item.path || "#" do %><i class="<%= item.icon %>"></i> <%= item.title %><% end %></li>
168
- <% end %>
169
- </ol>
202
+ ```yaml
203
+ # config/menuizer/namespace.yml
204
+ - header: NAMESPACE MENU
205
+ - item: :Admin
170
206
  ```
171
207
 
172
- ```erb
173
- <%# app/views/layouts/application.html.erb %>
174
- <% Menuizer.menu(:namespace).activate @active_menu %>
175
- <title><%= yield :title %></title>
176
-
177
- ...
178
-
179
- <ul class="sidebar-menu">
180
- <% Menuizer.menu(:namespace).items.each do |item| %>
181
- <%= render "menu", item: item %>
182
- <% end %>
183
- </ul>
184
- ```
208
+ ```ruby
209
+ # app/controllers/application_controller.rb
210
+ class ApplicationController < ActionController::Base
211
+ before_action :set_menuizer
185
212
 
186
- ## set converter methods
213
+ private
187
214
 
188
- ```ruby
189
- # config/initializers/menuizer.rb
190
- Menuizer.configure(:namespace) do |menu|
191
- menu.set_converter :icon do |icon,opts|
192
- case
193
- when icon.blank? || icon.starts_with?("fa") then icon
194
- when icon then "fa fa-#{icon.to_s.gsub("_","-")}"
195
- else
196
- "fa fa-circle-o"
215
+ def set_menuizer
216
+ @menuizer = Menuizer.menu(:namespace)
217
+ end
218
+ helper_method def menuizer
219
+ @menuizer
197
220
  end
198
- end
199
221
  end
200
222
  ```
201
223
 
202
- second argument `opts` : original key-value hash
203
-
204
224
  ## Development
205
225
 
206
226
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -5,20 +5,18 @@ class Menuizer::Menu::Item < OpenStruct
5
5
  end
6
6
 
7
7
  def title
8
- title = @opts[:title]
9
- if title.respond_to?(:model_name) && title.model_name.respond_to?(:human)
10
- title.model_name.human
8
+ if model && model.model_name.respond_to?(:human)
9
+ model.model_name.human
11
10
  else
12
- title
11
+ @opts[:title]
13
12
  end
14
13
  end
15
14
  def path
16
15
  if path = @opts[:path]
17
16
  path
18
17
  else
19
- title = @opts[:title]
20
- if title.respond_to?(:model_name) && title.model_name.respond_to?(:plural)
21
- :"#{namespace}#{title.model_name.plural}"
18
+ if model && model.model_name.respond_to?(:plural)
19
+ :"#{namespace}#{model.model_name.plural}"
22
20
  end
23
21
  end
24
22
  end
data/lib/menuizer/menu.rb CHANGED
@@ -2,7 +2,12 @@ class Menuizer::Menu
2
2
  def initialize(namespace)
3
3
  if namespace
4
4
  @namespace = "#{namespace}_"
5
- @item_class = self.class.const_set(:"Item_#{namespace}", Class.new(Item))
5
+ item_class = :"Item_#{namespace}"
6
+ if self.class.const_defined?(item_class)
7
+ @item_class = self.class.const_get(item_class)
8
+ else
9
+ @item_class = self.class.const_set(item_class, Class.new(Item))
10
+ end
6
11
  else
7
12
  @namespace = nil
8
13
  @item_class = Item
@@ -41,41 +46,17 @@ class Menuizer::Menu
41
46
  end
42
47
  end
43
48
 
44
-
45
- def header(title)
46
- current << @item_class.new(
47
- type: :header,
48
- namespace: @namespace,
49
- title: title,
50
- )
51
- end
52
- def item(title, path: nil, **opts)
53
- unless block_given?
54
- item = @item_class.new(
55
- type: :item,
56
- parent: @parent,
57
- namespace: @namespace,
58
- title: title,
59
- path: path,
60
- **opts,
61
- )
62
- map[title] = item
63
- current << item
64
- else
65
- owner = @parent
66
- parents = @current
67
- item = @parent = @item_class.new(
68
- type: :tree,
69
- children: [],
70
- parent: owner,
71
- namespace: @namespace,
72
- title: title,
73
- **opts,
74
- )
75
- @current = item.children
76
- yield
77
- children, @current, @parent = @current, parents, owner
78
- current << item
49
+ def load(data)
50
+ return unless data.respond_to?(:each)
51
+ data.each do |item|
52
+ if item.respond_to?(:map) && item.respond_to?(:delete)
53
+ item = item.map{|k,v| [k.to_sym,v]}.to_h
54
+ if title = item.delete(:header)
55
+ add_header title
56
+ else
57
+ add_item item.delete(:item), item
58
+ end
59
+ end
79
60
  end
80
61
  end
81
62
 
@@ -85,6 +66,57 @@ class Menuizer::Menu
85
66
 
86
67
  private
87
68
 
69
+ def add_header(title)
70
+ current << @item_class.new(
71
+ type: :header,
72
+ namespace: @namespace,
73
+ title: title,
74
+ )
75
+ end
76
+ def add_item(title, opts)
77
+ model = to_model title
78
+
79
+ unless children = opts.delete(:children)
80
+ item = @item_class.new(
81
+ type: :item,
82
+ parent: @parent,
83
+ namespace: @namespace,
84
+ title: title,
85
+ model: model,
86
+ **opts,
87
+ )
88
+ map[title] = item
89
+ current << item
90
+ else
91
+ owner = @parent
92
+ parents = @current
93
+ item = @parent = @item_class.new(
94
+ type: :tree,
95
+ children: [],
96
+ parent: owner,
97
+ namespace: @namespace,
98
+ title: title,
99
+ model: model,
100
+ **opts,
101
+ )
102
+ @current = item.children
103
+ self.load children
104
+ @current, @parent = parents, owner
105
+ current << item
106
+ end
107
+ end
108
+ def to_model(title)
109
+ return unless title.is_a?(Symbol)
110
+ parent = Object
111
+ title.to_s.split("::").each do |tip|
112
+ return unless parent.const_defined?(tip)
113
+ parent = parent.const_get(tip)
114
+ end
115
+ if parent.respond_to?(:model_name)
116
+ parent
117
+ end
118
+ end
119
+
88
120
  def current
89
121
  @current ||= items
90
122
  end
data/lib/menuizer.rb CHANGED
@@ -5,16 +5,27 @@ require "menuizer/menu/item"
5
5
  module Menuizer
6
6
  class << self
7
7
  def configure(namespace=nil)
8
- yield (map[namespace] = Menu.new(namespace))
8
+ yield (config[namespace] ||= OpenStruct.new)
9
9
  end
10
10
  def menu(namespace=nil)
11
- map[namespace]
11
+ Menu.new(namespace).tap{|menu|
12
+ c = config[namespace]
13
+ if c.respond_to?(:converter)
14
+ c.converter.each do |key,block|
15
+ menu.set_converter key, &block
16
+ end
17
+ end
18
+ if c.respond_to?(:file_path) && path = c.file_path
19
+ require "yaml"
20
+ menu.load YAML.load_file(path)
21
+ end
22
+ }
12
23
  end
13
24
 
14
25
  private
15
26
 
16
- def map
17
- @map ||= {}
27
+ def config
28
+ @config ||= {}
18
29
  end
19
30
  end
20
31
  end
data/version-dump.sh CHANGED
@@ -10,7 +10,7 @@ fi
10
10
  version_file=version.txt
11
11
  current_version=$(cat $version_file)
12
12
 
13
- git pull --tags
13
+ git fetch --tags
14
14
  current_tag=$(git tag | tail -1)
15
15
 
16
16
  if [ "v$current_version" != "$current_tag" ]; then
data/version.txt CHANGED
@@ -1 +1 @@
1
- 0.1.9
1
+ 0.2.0
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: menuizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - shun