menuizer 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3f46abe20a3b5c13dd4883bbe2e7983c1616dd65
4
+ data.tar.gz: eb0d8786f57091e8b204d9a9c8ce11ffc6c80a33
5
+ SHA512:
6
+ metadata.gz: cfa99ec1f9b76b6fa55d8599e0bdb2f8f257ad2778f63a368d6733a3c520cc09b2c8252067690ad0c9faeef8f94047e494a1a1e59ffe95b5a741bc89a9028d88
7
+ data.tar.gz: ba389b0d967729dc41de32879cb1d6c5ff5470b0f87211dc241f88263ac85f6d21e26ccdf98258d5f23b77a2ae05cddd758069ba7189243496bb4152b2713a09
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /release/
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.1
6
+ deploy:
7
+ api_key:
8
+ secure: X9io3ltI9EXOql2o8IMlIBshluU4B2vabN3Lnim8r7lTi8av+K9wHnyOi7rOqHrzc3iqoytVoQUML1WwQHhWzXy02sBGS82211zWYtrNOGwNbYC43xg6kM9xpL/hF+Jfiz+dN0j3jhbizAw11dWfNwcEdnInc8XoUggotLMysfWZFb7lFdwP8Py5zI1ikXwJDog8LhENtYckjcAet8EJ2kPkbsgDSMChP4sLn1JUtffXra1yU2Z3lxjYPLIkXELM4k/pCWXSWaqaJOsbJi664/ROQRRlDuhH2yEbooSl2daU0XnHrCzfAzuxp9SFfdKkDX7NlF1CF9OoOjuQBBjv8VBVlTbDqKVg+ewFwx/sp9Ka4cq9sb1QO2P4XRGysy7286GbFZWWP+1L4iWr3E1dFl+7wX3vI2tUOg74VuNAExiKWYZuxSUjKGg7xhpfj3FZnUxHvk9r3V/UcWL9sukxtXKkRnuVAfrfSQiYQamei4rqAixp6QEvAjN1ppAV87fRizoTKakmIBN0cP3M/odfPa61T9EsKkzsXMk7w1VO48C/MsRs2P14vT8XECpcXBJqOFY4THr74eWu3qE6sSZpFxDxdKpogyoG8anNlAM3Dah8mZ+5WUT6F1fRGDzvAmcCbDzeHZepAQv3rmIeOB1PJG2YYoZgcIblEngIra0Yvew=
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in menuizer.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 げっとシステム
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,190 @@
1
+ # Menuizer
2
+
3
+ [![Build Status](https://travis-ci.org/getto-systems/menuizer.svg?branch=master)](https://travis-ci.org/getto-systems/menuizer)
4
+
5
+ build menu items for admin page ( like AdminLTE )
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'menuizer'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install menuizer
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ # config/initializers/menuizer.rb
27
+ Menuizer.configure do |menu|
28
+ menu.header "MAIN NAVIGATION"
29
+ menu.item "Dashboard", icon: "fa fa-dashboard" do
30
+ menu.item "Dashboard v1", icon: "fa fa-circle-o"
31
+ menu.item "Dashboard v2", icon: "fa fa-circle-o"
32
+ end
33
+
34
+ menu.item Widget, icon: "fa fa-th"
35
+
36
+ menu.item "Settings", icon: "fa fa-cog" do
37
+ menu.item Admin, icon: "fa fa-circle-o"
38
+ menu.item User, icon: "fa fa-circle-o", notices: [
39
+ ->{ [:warning, User.unauthorized.count] },
40
+ ->{ [:danger, User.wait.count] },
41
+ ]
42
+
43
+ menu.item "nested" do
44
+ menu.item "nested item", path: :path_to_somewhere_path, icon: "fa fa-circle-o"
45
+ end
46
+ end
47
+ end
48
+ ```
49
+
50
+ ```erb
51
+ <%# app/views/admins/index.html.erb %>
52
+ <% @active_menu = Admin # first argument of menu.item %>
53
+ <% content_for :title do %><%= Menuizer.menu.active_item.title %><% end %>
54
+
55
+ ...
56
+
57
+ <ol class="breadcrumb">
58
+ <% Menuizer.menu.active_items.each do |item| %>
59
+ <li><%= link_to item.path || "#" do %><i class="<%= item.icon %>"></i> <%= item.title %><% end %></li>
60
+ <% end %>
61
+ </ol>
62
+ ```
63
+
64
+ ```erb
65
+ <%# app/views/layouts/application.html.erb %>
66
+ <% Menuizer.menu.activate @active_menu %>
67
+ <title><%= yield :title %></title>
68
+
69
+ ...
70
+
71
+ <ul class="sidebar-menu">
72
+ <% Menuizer.menu.items.each do |item| %>
73
+ <%= render "menu", item: item %>
74
+ <% end %>
75
+ </ul>
76
+ ```
77
+
78
+ ```erb
79
+ <%
80
+ item # Menuizer.menu.items's item
81
+ %>
82
+ <% case item.type %>
83
+ <% when :header %>
84
+ <li class="header"><%= item.title %></li>
85
+ <% when :item %>
86
+ <li class="<% if item.is_active %>active<% end %>">
87
+ <%= link_to item.path || "#" do %>
88
+ <i class="<%= item.icon %>"></i> <span><%= item.title %></span>
89
+ <% if item.notices.present? %>
90
+ <% item.notices.each do |notice| %>
91
+ <% type, text = notice.call %>
92
+ <span class="label label-<%= type %> pull-right"><%= text %></span>
93
+ <% end %>
94
+ <% end %>
95
+ <% end %>
96
+ </li>
97
+ <% else %>
98
+ <li class="<% if item.is_active %>active <% end %>treeview">
99
+ <a href="#">
100
+ <i class="<%= item.icon %>"></i> <span><%= item.title %></span>
101
+ <span class="pull-right-container">
102
+ <i class="fa fa-angle-left pull-right"></i>
103
+ </span>
104
+ </a>
105
+ <ul class="treeview-menu">
106
+ <% item.children.each do |item| %>
107
+ <%= render "layouts/manage/menu", item: item %>
108
+ <% end %>
109
+ </ul>
110
+ </li>
111
+ <% end %>
112
+ ```
113
+
114
+ ## API
115
+
116
+ ### menu.item
117
+
118
+ ```ruby
119
+ menu.item(
120
+ title, # required : convert to item.title
121
+ path: nil, # optional : convert to item.path
122
+
123
+ # other keys pass-through to item.*
124
+ icon: "fa fa-icon",
125
+ notices: [
126
+ ->{ [:info, count] },
127
+ ],
128
+ )
129
+ ```
130
+
131
+ #### `title` converting
132
+
133
+ * convert `title.model_name.human` if title respond to `model_name`
134
+ * or, leave `title`
135
+
136
+
137
+ #### `path` converting
138
+
139
+ * convert `:"#{title.model_name.plural}_path"` if title respond to `model_name`
140
+ * or, leave `nil`
141
+
142
+ ## Multiple namespaces
143
+
144
+ if your rails application has multiple namespaces, and required multiple menues, pass `:namespace` to Menuizer methods.
145
+
146
+ ```ruby
147
+ # config/initializers/menuizer.rb
148
+ Menuizer.configure(:namespace) do |menu|
149
+ ...
150
+ end
151
+ ```
152
+
153
+ ```erb
154
+ <%# app/views/admins/index.html.erb %>
155
+ <% @active_menu = Admin # first argument of menu.item %>
156
+ <% content_for :title do %><%= Menuizer.menu(:namespace).active_item.title %><% end %>
157
+
158
+ ...
159
+
160
+ <ol class="breadcrumb">
161
+ <% Menuizer.menu(:namespace).active_items.each do |item| %>
162
+ <li><%= link_to item.path || "#" do %><i class="<%= item.icon %>"></i> <%= item.title %><% end %></li>
163
+ <% end %>
164
+ </ol>
165
+ ```
166
+
167
+ ```erb
168
+ <%# app/views/layouts/application.html.erb %>
169
+ <% Menuizer.menu(:namespace).activate @active_menu %>
170
+ <title><%= yield :title %></title>
171
+
172
+ ...
173
+
174
+ <ul class="sidebar-menu">
175
+ <% Menuizer.menu(:namespace).items.each do |item| %>
176
+ <%= render "menu", item: item %>
177
+ <% end %>
178
+ </ul>
179
+ ```
180
+
181
+ ## Development
182
+
183
+ 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.
184
+
185
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
186
+
187
+ ## Contributing
188
+
189
+ Bug reports and pull requests are welcome on GitHub at https://github.com/getto-systems/menuizer.
190
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "menuizer"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,94 @@
1
+ class Menuizer::Menu
2
+ def initialize(namespace)
3
+ @namespace = namespace ? "#{namespace}_" : nil
4
+ end
5
+
6
+ def activate(key)
7
+ active_items.each do |item|
8
+ item.is_active = false
9
+ end
10
+
11
+ @active_item = item = map[key]
12
+ while item
13
+ item.is_active = true
14
+ item = item.parent
15
+ end
16
+ end
17
+ def active_item
18
+ @active_item
19
+ end
20
+ def active_items
21
+ result = []
22
+ item = @active_item
23
+ while item
24
+ result << item
25
+ item = item.parent
26
+ end
27
+ result
28
+ end
29
+
30
+
31
+ def header(title)
32
+ current << OpenStruct.new(
33
+ type: :header,
34
+ title: title,
35
+ )
36
+ end
37
+ def item(title, path: nil, **opts)
38
+ unless block_given?
39
+ item = OpenStruct.new(
40
+ type: :item,
41
+ title: to_title(title),
42
+ path: to_path(path: path, title: title),
43
+ parent: @parent,
44
+ **opts,
45
+ )
46
+ map[title] = item
47
+ current << item
48
+ else
49
+ owner = @parent
50
+ parents = @current
51
+ item = @parent = OpenStruct.new(
52
+ type: :tree,
53
+ title: to_title(title),
54
+ children: [],
55
+ parent: owner,
56
+ **opts,
57
+ )
58
+ @current = item.children
59
+ yield
60
+ children, @current, @parent = @current, parents, owner
61
+ current << item
62
+ end
63
+ end
64
+
65
+ def items
66
+ @items ||= []
67
+ end
68
+
69
+ private
70
+
71
+ def current
72
+ @current ||= items
73
+ end
74
+ def map
75
+ @map ||= {}
76
+ end
77
+
78
+ def to_title(title)
79
+ if title.respond_to?(:model_name)
80
+ title.model_name.human
81
+ else
82
+ title
83
+ end
84
+ end
85
+ def to_path(path:, title:)
86
+ if path
87
+ path
88
+ else
89
+ if title.respond_to?(:model_name)
90
+ :"#{@namespace}#{title.model_name.plural}_path"
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,3 @@
1
+ module Menuizer
2
+ VERSION = "0.1.1"
3
+ end
data/lib/menuizer.rb ADDED
@@ -0,0 +1,19 @@
1
+ require "menuizer/version"
2
+ require "menuizer/menu"
3
+
4
+ module Menuizer
5
+ class << self
6
+ def configure(namespace=nil)
7
+ yield (map[namespace] = Menu.new(namespace))
8
+ end
9
+ def menu(namespace=nil)
10
+ map[namespace]
11
+ end
12
+
13
+ private
14
+
15
+ def map
16
+ @map ||= {}
17
+ end
18
+ end
19
+ end
data/menuizer.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'menuizer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "menuizer"
8
+ spec.version = Menuizer::VERSION
9
+ spec.authors = ["shun"]
10
+ spec.email = ["shun@getto.systems"]
11
+
12
+ spec.summary = %q{build menu items for admin page}
13
+ spec.description = %q{build menu items for admin page ( like AdminLTE )}
14
+ spec.homepage = "https://github.com/getto-systems/menuizer"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.13"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "minitest", "~> 5.0"
35
+ spec.add_development_dependency "travis"
36
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: menuizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - shun
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: travis
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: build menu items for admin page ( like AdminLTE )
70
+ email:
71
+ - shun@getto.systems
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - lib/menuizer.rb
85
+ - lib/menuizer/menu.rb
86
+ - lib/menuizer/version.rb
87
+ - menuizer.gemspec
88
+ homepage: https://github.com/getto-systems/menuizer
89
+ licenses: []
90
+ metadata:
91
+ allowed_push_host: https://rubygems.org
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.6.7
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: build menu items for admin page
112
+ test_files: []