golden-menu 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +128 -0
  6. data/Rakefile +1 -0
  7. data/app/controllers/golden/menu/hierarchical_menus_controller.rb +48 -0
  8. data/app/helpers/golden/menu/hierarchical_menus_helper.rb +56 -0
  9. data/app/models/concerns/golden/menu/golden_menu_resource.rb +11 -0
  10. data/app/models/golden/menu/hierarchical_menu.rb +35 -0
  11. data/app/models/golden/menu/menu_resource.rb +6 -0
  12. data/app/views/golden/menu/hierarchical_menus/_form.html.erb +14 -0
  13. data/app/views/golden/menu/hierarchical_menus/_menu.html.erb +22 -0
  14. data/app/views/golden/menu/hierarchical_menus/_table.html.erb +36 -0
  15. data/app/views/golden/menu/hierarchical_menus/_tree.html.erb +3 -0
  16. data/app/views/golden/menu/hierarchical_menus/edit.html.erb +27 -0
  17. data/app/views/golden/menu/hierarchical_menus/index.html.erb +19 -0
  18. data/app/views/golden/menu/hierarchical_menus/list.html.erb +20 -0
  19. data/app/views/golden/menu/hierarchical_menus/new.html.erb +24 -0
  20. data/app/views/golden/menu/hierarchical_menus/show.html.erb +21 -0
  21. data/config/locales/menus.en.yml +50 -0
  22. data/config/locales/menus.zh-TW.yml +50 -0
  23. data/config/routes.rb +8 -0
  24. data/golden-menu.gemspec +30 -0
  25. data/lib/generators/golden/menu/USAGE +16 -0
  26. data/lib/generators/golden/menu/install_generator.rb +64 -0
  27. data/lib/generators/golden/menu/templates/create_golden_menu_resources.rb +12 -0
  28. data/lib/generators/golden/menu/templates/create_golden_menus.rb +19 -0
  29. data/lib/generators/golden/menu/templates/golden_menu.rb +11 -0
  30. data/lib/generators/golden/menu/templates/menu_resources_helper.rb +8 -0
  31. data/lib/generators/golden/menu/templates/menus.en.yml +6 -0
  32. data/lib/generators/golden/menu/templates/menus.zh-TW.yml +6 -0
  33. data/lib/golden/menu.rb +27 -0
  34. data/lib/golden/menu/engine.rb +12 -0
  35. data/lib/golden/menu/option_helper.rb +41 -0
  36. data/lib/golden/menu/version.rb +5 -0
  37. metadata +217 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2a9e1ca8781107e28df6d6418e7a2a1d5443089c
4
+ data.tar.gz: b8dd7c33402a20495f4545b2c94baf6f0236193b
5
+ SHA512:
6
+ metadata.gz: ed26d208087a54659e146a00c895074329f2fd691b3f0a0a39f249dc4c2e2f3eabea95ae5fd565c964fac5e9246752a09de6b7687c972a9eedd639a9f79c63ff
7
+ data.tar.gz: 62748ad8a8d3537e824a9d100a9ec6225325a6388e1b4c050360a1a89e2ef834d883eec27680784de5711817ee06812da7b259ece5cab89abd0c1f0015cef284
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in golden-menu.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Tse-Ching Ho
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,128 @@
1
+ # Golden::Menu
2
+
3
+ The `golden-menu` creates menus by building a hierarchiacal tree first and linking related resources later.
4
+
5
+ ## Requirement
6
+
7
+ For Rails 4 project, `Gemfile` should have
8
+
9
+ ```ruby
10
+ gem 'protected_attributes'
11
+ gem 'devise', '~> 3.0.0.rc'
12
+ gem 'cancan'
13
+ gem 'simple_form', '~> 3.0.0.rc'
14
+ gem 'anjlab-bootstrap-rails', require: 'bootstrap-rails'
15
+ gem 'will_paginate'
16
+ gem 'rails-theme-helper'
17
+ ```
18
+
19
+ For Rails 3 project, `Gemfile` should have
20
+
21
+ ```ruby
22
+ gem 'devise', '>= 2.0', '< 3.0'
23
+ gem 'cancan'
24
+ gem 'simple_form', '>= 2.0', '< 3.0'
25
+ gem 'anjlab-bootstrap-rails', require: 'bootstrap-rails'
26
+ gem 'will_paginate'
27
+ gem 'rails-theme-helper'
28
+ ```
29
+
30
+ Please make sure these gems are installed and configured properly.
31
+
32
+ ## Installation
33
+
34
+ Add `golden-menu` to your application's Gemfile.
35
+
36
+ ```ruby
37
+ gem 'golden-menu'
38
+ ```
39
+
40
+ Then bundle gems, generate files and migrate database.
41
+
42
+ ```ruby
43
+ $ bundle
44
+ $ rails generate golden:menu:install
45
+ $ rake db:migrate
46
+ ```
47
+
48
+ Remember add menu abilities for `cancan`.
49
+
50
+ ```ruby
51
+ can :manage, Golden::Menu::HierarchicalMenu
52
+ ```
53
+
54
+ ## Configuration
55
+
56
+ You will need to add these basic configures of `golden-menu` for your needs.
57
+
58
+ * `groups` for grouping menus with a name
59
+ * `translate_group_block` for group name translation
60
+
61
+ Edit `config/initializers/golden_menu.rb` for more detail.
62
+
63
+ ## Usage
64
+
65
+ Please go to `/hierarchical_menus` and create the needed menu tree first.
66
+
67
+ ### Menu Navigation
68
+
69
+ You can simply add a `golden_dropdown_menu` to your view of main app
70
+
71
+ ```ruby
72
+ <%= golden_dropdown_menu Golden::Menu::HierarchicalMenu.grouped('header').roots %>
73
+ ```
74
+
75
+ Or with more options and list items
76
+
77
+ ```ruby
78
+ <%= golden_dropdown_menu Golden::Menu::HierarchicalMenu.grouped('sidebar').roots, {
79
+ menu_type: %w{pull-left},
80
+ class: 'nav nav-list',
81
+ role: 'side-navigation'
82
+ } do |li|
83
+ li << [t('.title'), class: 'nav-header']
84
+ li << link_to(t('.guides'), guides_path)
85
+ pages.each do |page|
86
+ li << link_to(page.title, page_path(page))
87
+ end
88
+ end %>
89
+ ```
90
+
91
+ ### Menu resources
92
+
93
+ You can add the menu association field to form parital of any classes.
94
+
95
+ ```ruby
96
+ <%= f.association :menus,
97
+ collection: nested_set_options(Golden::Menu::HierarchicalMenu) { |menu|
98
+ "#{'-' * menu.level} #{menu.name_and_group} (#{menu.menu_resources.count})"
99
+ },
100
+ input_html: {
101
+ multiple: true,
102
+ class: 'chzn-select'
103
+ } %>
104
+ ```
105
+
106
+ Edit `menu_resources_helper.rb` and define what resource the menu item represented.
107
+
108
+ ```ruby
109
+ def golden_menu_resource_link resource
110
+ case resource
111
+ when Page
112
+ link_to resource.title, page_path(resource)
113
+ end
114
+ end
115
+ ```
116
+
117
+ ## Contributing
118
+
119
+ 1. Fork it
120
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
121
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
122
+ 4. Push to the branch (`git push origin my-new-feature`)
123
+ 5. Create new Pull Request
124
+
125
+ ## License
126
+
127
+ MIT
128
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,48 @@
1
+ class Golden::Menu::HierarchicalMenusController < ApplicationController
2
+ include ::TheSortableTreeController::Rebuild
3
+
4
+ before_filter :authenticate_session!
5
+ load_and_authorize_resource :menu, class: 'Golden::Menu::HierarchicalMenu', parent: false #, find_by: :url
6
+
7
+ def index
8
+ @menu = @menus.nested_set.select('id, url, name, description, parent_id')
9
+ respond_with @menus
10
+ end
11
+
12
+ def show
13
+ respond_with @menu
14
+ end
15
+
16
+ def new
17
+ respond_with @menu
18
+ end
19
+
20
+ def edit
21
+ end
22
+
23
+ def create
24
+ @menu.save
25
+ respond_with @menu
26
+ end
27
+
28
+ def update
29
+ @menu.update_attributes params[:menu]
30
+ respond_with @menu
31
+ end
32
+
33
+ def destroy
34
+ @menu.destroy
35
+ respond_with @menu
36
+ end
37
+
38
+ def list
39
+ @menu = @menus.nested_set
40
+ @menus = @menus.paginate page: params[:page]
41
+ end
42
+
43
+ protected
44
+
45
+ def the_define_common_variables
46
+ ['@menu', 'menus', Golden::Menu::HierarchicalMenu]
47
+ end
48
+ end
@@ -0,0 +1,56 @@
1
+ module Golden::Menu::HierarchicalMenusHelper
2
+ # Submenu should not be shown but move to root instead if root menu has no
3
+ # menu_resources at all.
4
+ def golden_dropdown_menu menus, options = {}
5
+ option_helper = Golden::Menu::OptionHelper.new options
6
+ render_list option_helper.menu_ul_class do |li|
7
+ yield li if block_given?
8
+ menus.each do |menu|
9
+ next unless menu.menu_resources.exists?
10
+ uri = golden_menu_link menu, option_helper.linked?
11
+ li << [
12
+ dropdown_toggle(menu.name, uri, title: menu.description),
13
+ dropdown_menu(role: 'menu') do |li2|
14
+ golden_dropdown_content menu.menu_resources, li2
15
+ golden_dropdown_submenu menu.children, li2, option_helper
16
+ end,
17
+ option_helper.menu_li_class
18
+ ]
19
+ end
20
+ end
21
+ end
22
+
23
+ def golden_dropdown_submenu menus, li, option_helper
24
+ menus.each_with_index do |submenu, index|
25
+ next unless submenu.menu_resources.exists?
26
+ uri = golden_menu_link submenu, option_helper.linked?
27
+ li << [nil, { class: 'divider' }] if index == 0
28
+ li << [
29
+ link_to(submenu.name, uri, title: submenu.description),
30
+ dropdown_menu do |li2|
31
+ golden_dropdown_content submenu.menu_resources, li2
32
+ golden_dropdown_submenu submenu.children, li2, option_helper
33
+ end,
34
+ option_helper.submenu_li_class
35
+ ]
36
+ end
37
+ end
38
+
39
+ def golden_dropdown_content menu_resources, li
40
+ menu_resources.each do |menu_resource|
41
+ link = golden_menu_resource_link menu_resource.resource
42
+ li << link if link
43
+ end
44
+ end
45
+
46
+ def golden_menu_link menu, linked = false
47
+ return menu.resource_url if menu.resource_url.present?
48
+ linked ? golden_menu.hierarchical_menu_path(menu) : '#'
49
+ end
50
+
51
+ def golden_menu_groups
52
+ Golden::Menu.groups.map do |value|
53
+ [Golden::Menu.translate_group(value), value]
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,11 @@
1
+ module Golden::Menu::GoldenMenuResource
2
+ extend ActiveSupport::Concern
3
+ included do
4
+ attr_accessible :menu_ids
5
+ has_many :menu_resources, class_name: 'Golden::Menu::MenuResource', as: :resource
6
+ has_many :menus, class_name: 'Golden::Menu::HierarchicalMenu', through: :menu_resources
7
+ end
8
+
9
+ module ClassMethods
10
+ end
11
+ end
@@ -0,0 +1,35 @@
1
+ class Golden::Menu::HierarchicalMenu < ActiveRecord::Base
2
+ include ::TheSortableTree::Scopes
3
+
4
+ self.table_name = 'golden_menus'
5
+
6
+ attr_accessible :group, :name, :description, :parent_id, :resource_url
7
+
8
+ validates :name, presence: true
9
+ validates :url, format: { with: /\A([\w\-]{3,})\z/ }, allow_blank: true
10
+
11
+ belongs_to :parent, class_name: 'Golden::Menu::HierarchicalMenu'
12
+ has_many :menu_resources, foreign_key: 'menu_id'
13
+
14
+ acts_as_url :name, sync_url: true, only_when_blank: true
15
+ acts_as_nested_set
16
+
17
+ class << self
18
+ def grouped(name)
19
+ where(group: name)
20
+ end
21
+ end
22
+
23
+ def to_param
24
+ id
25
+ end
26
+
27
+ def name_and_group
28
+ return name unless translated_group
29
+ "#{name} @#{translated_group}"
30
+ end
31
+
32
+ def translated_group
33
+ ::Golden::Menu.translate_group group
34
+ end
35
+ end
@@ -0,0 +1,6 @@
1
+ class Golden::Menu::MenuResource < ActiveRecord::Base
2
+ self.table_name = 'golden_menu_resources'
3
+
4
+ belongs_to :menu, class_name: 'Golden::Menu::HierarchicalMenu', foreign_key: 'menu_id'
5
+ belongs_to :resource, polymorphic: true
6
+ end
@@ -0,0 +1,14 @@
1
+ <div class="form-inputs">
2
+ <%= f.input :group,
3
+ as: :select,
4
+ collection: golden_menu_groups,
5
+ input_html: { class: 'chzn-select' } %>
6
+ <%= f.input :name %>
7
+ <%= f.input :description %>
8
+ <%= f.association :parent,
9
+ collection: nested_set_options(Golden::Menu::HierarchicalMenu) { |menu|
10
+ "#{'-' * menu.level} #{menu.name}"
11
+ },
12
+ input_html: { class: 'chzn-select' } %>
13
+ <%= f.input :resource_url %>
14
+ </div>
@@ -0,0 +1,22 @@
1
+ <section>
2
+ <p>
3
+ <b><%= Golden::Menu::HierarchicalMenu.human_attribute_name :group %>:</b>
4
+ <%= menu.translated_group %>
5
+ </p>
6
+ <p>
7
+ <b><%= Golden::Menu::HierarchicalMenu.human_attribute_name :name %>:</b>
8
+ <%= menu.name %>
9
+ </p>
10
+ <p>
11
+ <b><%= Golden::Menu::HierarchicalMenu.human_attribute_name :description %>:</b>
12
+ <%= menu.description %>
13
+ </p>
14
+ <p>
15
+ <b><%= Golden::Menu::HierarchicalMenu.human_attribute_name :parent %>:</b>
16
+ <%= menu.parent.try :name %>
17
+ </p>
18
+ <p>
19
+ <b><%= Golden::Menu::HierarchicalMenu.human_attribute_name :menu_resource_counts %>:</b>
20
+ <%= menu.menu_resources.count %>
21
+ </p>
22
+ </section>
@@ -0,0 +1,36 @@
1
+ <table class="table table-striped table-bordered">
2
+ <thead>
3
+ <tr>
4
+ <th><%= Golden::Menu::HierarchicalMenu.human_attribute_name :group %></th>
5
+ <th><%= Golden::Menu::HierarchicalMenu.human_attribute_name :name %></th>
6
+ <th><%= Golden::Menu::HierarchicalMenu.human_attribute_name :description %></th>
7
+ <th><%= Golden::Menu::HierarchicalMenu.human_attribute_name :parent %></th>
8
+ <th><%= Golden::Menu::HierarchicalMenu.human_attribute_name :menu_resource_counts %></th>
9
+ <th><%= t('helpers.actions') %></th>
10
+ </tr>
11
+ </thead>
12
+ <tbody>
13
+ <% menus.each do |menu| %>
14
+ <tr>
15
+ <td><%= menu.translated_group %></td>
16
+ <td><%= menu.name %></td>
17
+ <td><%= menu.description %></td>
18
+ <td><%= menu.parent.try :name %></td>
19
+ <td><%= menu.menu_resources.count %></td>
20
+ <td>
21
+ <%= render_btn_group do |btn|
22
+ if can? :show, menu
23
+ btn << link_to_show(menu)
24
+ end
25
+ if can? :edit, menu
26
+ btn << link_to_edit([:edit, menu])
27
+ end
28
+ if can? :destroy, menu
29
+ btn << link_to_destroy(menu)
30
+ end
31
+ end %>
32
+ </td>
33
+ </tr>
34
+ <% end %>
35
+ </tbody>
36
+ </table>
@@ -0,0 +1,3 @@
1
+ <%= content_tag :ol, class: 'sortable_tree', 'data-max_levels' => 5, 'data-rebuild_url' => rebuild_hierarchical_menus_url do
2
+ sortable_tree menus, title: :name_and_group
3
+ end %>
@@ -0,0 +1,27 @@
1
+ <article>
2
+ <header>
3
+ <h1><%= t('.title') %></h1>
4
+ </header>
5
+
6
+ <%= render_list class: 'nav nav-tabs' do |li|
7
+ if can? :index, Golden::Menu::HierarchicalMenu
8
+ li << [link_to(t('helpers.index'), hierarchical_menus_path), { class: 'pull-right' }]
9
+ end
10
+ if can? :show, @menu
11
+ li << [link_to(t('helpers.show'), @menu), { class: 'pull-right' }]
12
+ end
13
+ end %>
14
+
15
+ <div>
16
+ <%= simple_form_for @menu, as: :menu do |f| %>
17
+ <%= f.error_notification %>
18
+ <%= render 'form', f: f %>
19
+ <div class="form-actions">
20
+ <%= submit_button f %>
21
+ </div>
22
+ <% end %>
23
+ </div>
24
+
25
+ <footer>
26
+ </footer>
27
+ </article>
@@ -0,0 +1,19 @@
1
+ <article>
2
+ <header>
3
+ <h1><%= t('.title') %></h1>
4
+ </header>
5
+
6
+ <%= render_list class: 'nav nav-tabs' do |li|
7
+ if can? :list, Golden::Menu::HierarchicalMenu
8
+ li << [link_to(t('.list'), list_hierarchical_menus_path), { class: 'pull-right' }]
9
+ end
10
+ if can? :new, Golden::Menu::HierarchicalMenu
11
+ li << [link_to(t('helpers.new'), new_hierarchical_menu_path), { class: 'pull-right' }]
12
+ end
13
+ end %>
14
+
15
+ <%= render 'tree', menus: @menus %>
16
+
17
+ <footer>
18
+ </footer>
19
+ </article>
@@ -0,0 +1,20 @@
1
+ <article>
2
+ <header>
3
+ <h1><%= t('.title') %></h1>
4
+ </header>
5
+
6
+ <%= render_list class: 'nav nav-tabs' do |li|
7
+ if can? :index, Golden::Menu::HierarchicalMenu
8
+ li << [link_to(t('.index'), hierarchical_menus_path), { class: 'pull-right' }]
9
+ end
10
+ if can? :new, Golden::Menu::HierarchicalMenu
11
+ li << [link_to(t('helpers.new'), new_hierarchical_menu_path), { class: 'pull-right' }]
12
+ end
13
+ end %>
14
+
15
+ <%= render 'table', menus: @menus %>
16
+ <%= render 'application/pagination', collection: @menus %>
17
+
18
+ <footer>
19
+ </footer>
20
+ </article>
@@ -0,0 +1,24 @@
1
+ <article>
2
+ <header>
3
+ <h1><%= t('.title') %></h1>
4
+ </header>
5
+
6
+ <%= render_list class: 'nav nav-tabs' do |li|
7
+ if can? :index, Golden::Menu::HierarchicalMenu
8
+ li << [link_to(t('helpers.index'), hierarchical_menus_path), { class: 'pull-right' }]
9
+ end
10
+ end %>
11
+
12
+ <div>
13
+ <%= simple_form_for @menu, as: :menu do |f| %>
14
+ <%= f.error_notification %>
15
+ <%= render 'form', f: f %>
16
+ <div class="form-actions">
17
+ <%= submit_button f %>
18
+ </div>
19
+ <% end %>
20
+ </div>
21
+
22
+ <footer>
23
+ </footer>
24
+ </article>
@@ -0,0 +1,21 @@
1
+ <article>
2
+ <header>
3
+ <h1><%= t('.title', name: @menu.name) %></h1>
4
+ </header>
5
+
6
+ <%= render_list class: 'nav nav-tabs' do |li|
7
+ if can? :index, Golden::Menu::HierarchicalMenu
8
+ li << [link_to(t('helpers.index'), hierarchical_menus_path), { class: 'pull-right' }]
9
+ end
10
+ if can? :edit, @menu
11
+ li << [link_to(t('helpers.edit'), [:edit, @menu]), { class: 'pull-right' }]
12
+ end
13
+ end %>
14
+
15
+ <div>
16
+ <%= render 'menu', menu: @menu %>
17
+ </div>
18
+
19
+ <footer>
20
+ </footer>
21
+ </article>
@@ -0,0 +1,50 @@
1
+ en:
2
+ golden:
3
+ menu:
4
+ hierarchical_menus:
5
+ edit:
6
+ title: 'Editing menu'
7
+ index:
8
+ title: 'Menus'
9
+ list: 'Listing menus'
10
+ new:
11
+ title: 'New menu'
12
+ show:
13
+ title: 'Menu - %{name}'
14
+ list:
15
+ title: 'Listing menus'
16
+ index: 'Menus'
17
+
18
+ activerecord:
19
+ models:
20
+ golden/menu/hierarchical_menu: 'Menu'
21
+ attributes:
22
+ golden/menu/hierarchical_menu:
23
+ url: 'Url'
24
+ group: 'Group'
25
+ name: 'Name'
26
+ description: 'Description'
27
+ resource_url: 'Resource URL'
28
+ parent: 'Parent'
29
+ lft: 'Lft'
30
+ rgt: 'Rgt'
31
+ depth: 'Depth'
32
+ menu_resources: 'Menu items'
33
+ menu_resource_counts: 'Menu items count'
34
+
35
+ simple_form:
36
+ labels:
37
+ golden_menu/hierarchical_menu:
38
+ hints:
39
+ golden_menu/hierarchical_menu:
40
+ menu:
41
+ group: 'Only root menus (blank parent) need to choose a group'
42
+ resource_url: 'fill redirect URL while click menu'
43
+ placeholders:
44
+ golden_menu/hierarchical_menu:
45
+
46
+ helpers:
47
+ submit:
48
+ golden_menu/hierarchical_menu:
49
+ create: 'Create %{model}'
50
+ update: 'Update %{model}'
@@ -0,0 +1,50 @@
1
+ zh-TW:
2
+ golden:
3
+ menu:
4
+ hierarchical_menus:
5
+ edit:
6
+ title: '編輯選單'
7
+ index:
8
+ title: '選單管理'
9
+ list: '選單列表'
10
+ new:
11
+ title: '新增選單'
12
+ show:
13
+ title: '選單 - %{name}'
14
+ list:
15
+ title: '選單列表'
16
+ index: '選單管理'
17
+
18
+ activerecord:
19
+ models:
20
+ golden/menu/hierarchical_menu: '選單'
21
+ attributes:
22
+ golden/menu/hierarchical_menu:
23
+ url: '網址'
24
+ group: '群組'
25
+ name: '名稱'
26
+ description: '描述'
27
+ resource_url: '連結網址'
28
+ parent: '父層'
29
+ lft: '左'
30
+ rgt: '右'
31
+ depth: '深度'
32
+ menu_resources: '選項'
33
+ menu_resource_counts: '選項數量'
34
+
35
+ simple_form:
36
+ labels:
37
+ golden_menu_hierarchical_menu:
38
+ hints:
39
+ golden_menu_hierarchical_menu:
40
+ menu:
41
+ group: '只有最頂層的選單(父層為空者)需要選擇群組'
42
+ resource_url: '可選填點擊時連結的網址'
43
+ placeholders:
44
+ golden_menu_hierarchical_menu:
45
+
46
+ helpers:
47
+ submit:
48
+ golden_menu_hierarchical_menu:
49
+ create: '建立%{model}'
50
+ update: '確認'
data/config/routes.rb ADDED
@@ -0,0 +1,8 @@
1
+ Golden::Menu::Engine.routes.draw do
2
+ resources :hierarchical_menus do
3
+ collection do
4
+ get :list
5
+ post :rebuild
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'golden/menu/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'golden-menu'
8
+ spec.version = Golden::Menu::VERSION
9
+ spec.authors = ['Tse-Ching Ho']
10
+ spec.email = ['tsechingho@gmail.com']
11
+ spec.description = %q{Golden Menu creates menus by building a hierarchiacal tree first and linking related resources later}
12
+ spec.summary = %q{creates menus by building a hierarchiacal tree first and linking related resources later}
13
+ spec.homepage = 'https://github.com/goldenio/golden-menu'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'railties', '>= 3.2.13', '< 5'
22
+ spec.add_dependency 'activesupport', '>= 3.2.13', '< 5'
23
+ spec.add_dependency 'actionpack', '>= 3.2.13', '< 5'
24
+ spec.add_dependency 'activerecord', '>= 3.2.13', '< 5'
25
+ spec.add_dependency 'awesome_nested_set', '>= 2.1'
26
+ spec.add_dependency 'the_sortable_tree', '>= 2.3'
27
+
28
+ spec.add_development_dependency 'bundler', '~> 1.3'
29
+ spec.add_development_dependency 'rake'
30
+ end
@@ -0,0 +1,16 @@
1
+ Description:
2
+ It will generate migration, configuration and helper for use.
3
+
4
+ Example:
5
+ rails generate golden:menu:install
6
+
7
+ This will create/modify:
8
+ db/migrate/20130613082608_create_golden_menus.rb
9
+ db/migrate/20130613082609_create_golden_menu_resources.rb
10
+ config/initializers/golden_menu.rb
11
+ config/application.rb
12
+ config/locales/golden/menus.en.yml
13
+ config/locales/golden/menus.zh-TW.yml
14
+ config/routes.rb
15
+ app/controllers/application_controller.rb
16
+ app/helpers/golden/menu/menu_resources_helper.rb
@@ -0,0 +1,64 @@
1
+ require 'active_record/migration'
2
+
3
+ class Golden::Menu::InstallGenerator < Rails::Generators::NamedBase
4
+ include Rails::Generators::Migration
5
+
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ argument :name, type: :string, default: 'menu'
9
+
10
+ def self.next_migration_number dirname
11
+ next_migration_number = current_migration_number(dirname) + 1
12
+ ActiveRecord::Migration.next_migration_number(next_migration_number)
13
+ end
14
+
15
+ def generate_migration
16
+ %W(
17
+ create_golden_menus.rb
18
+ create_golden_menu_resources.rb
19
+ ).each do |file|
20
+ migration_template file, File.join('db/migrate', class_path, file)
21
+ end
22
+ end
23
+
24
+
25
+ def generate_initializer
26
+ file = 'golden_menu.rb'
27
+ log :initializer, file
28
+ template file, File.join('config/initializers', class_path, file), verbose: false
29
+ end
30
+
31
+ def add_locales_path
32
+ file = 'config/application.rb'
33
+ sentinel = "# config.i18n.default_locale = :de\n"
34
+ insert_into_file file, after: sentinel do
35
+ " config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s]\n"
36
+ end
37
+ end
38
+
39
+ def generate_locales
40
+ files = %w{menus.en.yml menus.zh-TW.yml}
41
+ files.each do |file|
42
+ log :locales, "golden/#{file}"
43
+ template file, File.join('config/locales/golden', class_path, file), verbose: false
44
+ end
45
+ end
46
+
47
+ def inject_routes
48
+ route "mount Golden::Menu::Engine, at: '/'\n"
49
+ end
50
+
51
+ def inject_controller
52
+ file = 'app/controllers/application_controller.rb'
53
+ sentinel = "protect_from_forgery with: :exception\n"
54
+ insert_into_file file, after: sentinel do
55
+ " helper Golden::Menu::HierarchicalMenusHelper\n"
56
+ end
57
+ end
58
+
59
+ def generate_helper
60
+ file = 'menu_resources_helper.rb'
61
+ log :helper, file
62
+ template file, File.join('app/helpers/golden/menu', class_path, file), verbose: false
63
+ end
64
+ end
@@ -0,0 +1,12 @@
1
+ class CreateGoldenMenuResources < ActiveRecord::Migration
2
+ def change
3
+ create_table :golden_menu_resources do |t|
4
+ t.references :menu
5
+ t.references :resource, polymorphic: true
6
+
7
+ t.timestamps
8
+ end
9
+ add_index :golden_menu_resources, :menu_id
10
+ add_index :golden_menu_resources, [:resource_type, :resource_id]
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ class CreateGoldenMenus < ActiveRecord::Migration
2
+ def change
3
+ create_table :golden_menus do |t|
4
+ t.string :url
5
+ t.string :group
6
+ t.string :name, null: false
7
+ t.string :description
8
+ t.string :resource_url
9
+ t.references :parent
10
+ t.integer :lft
11
+ t.integer :rgt
12
+ t.integer :depth
13
+
14
+ t.timestamps
15
+ end
16
+ add_index :golden_menus, :url
17
+ add_index :golden_menus, :parent_id
18
+ end
19
+ end
@@ -0,0 +1,11 @@
1
+ Golden::Menu.configure do |config|
2
+ # Add avaiable groups with array of name and value.
3
+ # Name will be used as translation key.
4
+ # Default is a blank array. You SHOULD give one at least.
5
+ config.groups = %w{header sidebar}
6
+
7
+ # You can overwrite translated group names by changing the block definition.
8
+ #config.translate_group_block = lambda do |group|
9
+ # I18n.t(group, scope: 'golden.menu.groups')
10
+ #end
11
+ end
@@ -0,0 +1,8 @@
1
+ module Golden::Menu::MenuResourcesHelper
2
+ def golden_menu_resource_link resource
3
+ # case resource
4
+ # when Page
5
+ # link_to resource.title, page_path(resource)
6
+ # end
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ en:
2
+ golden:
3
+ menu:
4
+ groups:
5
+ header: 'Header'
6
+ sidebar: 'Sidebar'
@@ -0,0 +1,6 @@
1
+ zh-TW:
2
+ golden:
3
+ menu:
4
+ groups:
5
+ header: '頁首'
6
+ sidebar: '側欄'
@@ -0,0 +1,27 @@
1
+ require 'awesome_nested_set'
2
+ require 'the_sortable_tree'
3
+
4
+ module Golden
5
+ module Menu
6
+ mattr_accessor :groups
7
+ @@groups = []
8
+
9
+ mattr_accessor :translate_group_block
10
+ @@translate_group_block = lambda { |group|
11
+ I18n.t(group, scope: 'golden.menu.groups')
12
+ }
13
+
14
+ def self.configure
15
+ yield self
16
+ end
17
+
18
+ def self.translate_group group
19
+ return if group.blank?
20
+ @@translate_group_block.call group
21
+ end
22
+ end
23
+ end
24
+
25
+ require 'golden/menu/version'
26
+ require 'golden/menu/option_helper'
27
+ require 'golden/menu/engine'
@@ -0,0 +1,12 @@
1
+ module Golden
2
+ module Menu
3
+ class Engine < ::Rails::Engine
4
+ isolate_namespace Golden::Menu
5
+ engine_name :golden_menu
6
+
7
+ config.autoload_paths += [
8
+ root.join('app', 'models', 'concerns').to_s
9
+ ]
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,41 @@
1
+ module Golden
2
+ module Menu
3
+ class OptionHelper
4
+ attr_accessor :menu_type
5
+ attr_accessor :linked
6
+ attr_accessor :options
7
+
8
+ def initialize args
9
+ @menu_type = args.delete(:menu_type) || []
10
+ @linked = args.delete(:menu_linked)
11
+ @linked = false if @linked.nil?
12
+ @options = args
13
+ end
14
+
15
+ def menu_ul_class
16
+ { class: 'nav' }.deep_merge options
17
+ end
18
+
19
+ def menu_li_class
20
+ if menu_type.include? 'dropup'
21
+ { class: 'dropup' }
22
+ else
23
+ { class: 'dropdown' }
24
+ end
25
+ end
26
+
27
+ def submenu_li_class
28
+ if menu_type.include? 'pull-left'
29
+ { class: 'dropdown-submenu pull-left' }
30
+ else
31
+ { class: 'dropdown-submenu' }
32
+ end
33
+ end
34
+
35
+ def linked?
36
+ linked
37
+ end
38
+ end
39
+ end
40
+ end
41
+
@@ -0,0 +1,5 @@
1
+ module Golden
2
+ module Menu
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,217 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: golden-menu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tse-Ching Ho
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.13
20
+ - - <
21
+ - !ruby/object:Gem::Version
22
+ version: '5'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.13
30
+ - - <
31
+ - !ruby/object:Gem::Version
32
+ version: '5'
33
+ - !ruby/object:Gem::Dependency
34
+ name: activesupport
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: 3.2.13
40
+ - - <
41
+ - !ruby/object:Gem::Version
42
+ version: '5'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - '>='
48
+ - !ruby/object:Gem::Version
49
+ version: 3.2.13
50
+ - - <
51
+ - !ruby/object:Gem::Version
52
+ version: '5'
53
+ - !ruby/object:Gem::Dependency
54
+ name: actionpack
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - '>='
58
+ - !ruby/object:Gem::Version
59
+ version: 3.2.13
60
+ - - <
61
+ - !ruby/object:Gem::Version
62
+ version: '5'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 3.2.13
70
+ - - <
71
+ - !ruby/object:Gem::Version
72
+ version: '5'
73
+ - !ruby/object:Gem::Dependency
74
+ name: activerecord
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - '>='
78
+ - !ruby/object:Gem::Version
79
+ version: 3.2.13
80
+ - - <
81
+ - !ruby/object:Gem::Version
82
+ version: '5'
83
+ type: :runtime
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: 3.2.13
90
+ - - <
91
+ - !ruby/object:Gem::Version
92
+ version: '5'
93
+ - !ruby/object:Gem::Dependency
94
+ name: awesome_nested_set
95
+ requirement: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '2.1'
100
+ type: :runtime
101
+ prerelease: false
102
+ version_requirements: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '2.1'
107
+ - !ruby/object:Gem::Dependency
108
+ name: the_sortable_tree
109
+ requirement: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '2.3'
114
+ type: :runtime
115
+ prerelease: false
116
+ version_requirements: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '2.3'
121
+ - !ruby/object:Gem::Dependency
122
+ name: bundler
123
+ requirement: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ~>
126
+ - !ruby/object:Gem::Version
127
+ version: '1.3'
128
+ type: :development
129
+ prerelease: false
130
+ version_requirements: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ~>
133
+ - !ruby/object:Gem::Version
134
+ version: '1.3'
135
+ - !ruby/object:Gem::Dependency
136
+ name: rake
137
+ requirement: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ type: :development
143
+ prerelease: false
144
+ version_requirements: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - '>='
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ description: Golden Menu creates menus by building a hierarchiacal tree first and
150
+ linking related resources later
151
+ email:
152
+ - tsechingho@gmail.com
153
+ executables: []
154
+ extensions: []
155
+ extra_rdoc_files: []
156
+ files:
157
+ - .gitignore
158
+ - Gemfile
159
+ - LICENSE.txt
160
+ - README.md
161
+ - Rakefile
162
+ - app/controllers/golden/menu/hierarchical_menus_controller.rb
163
+ - app/helpers/golden/menu/hierarchical_menus_helper.rb
164
+ - app/models/concerns/golden/menu/golden_menu_resource.rb
165
+ - app/models/golden/menu/hierarchical_menu.rb
166
+ - app/models/golden/menu/menu_resource.rb
167
+ - app/views/golden/menu/hierarchical_menus/_form.html.erb
168
+ - app/views/golden/menu/hierarchical_menus/_menu.html.erb
169
+ - app/views/golden/menu/hierarchical_menus/_table.html.erb
170
+ - app/views/golden/menu/hierarchical_menus/_tree.html.erb
171
+ - app/views/golden/menu/hierarchical_menus/edit.html.erb
172
+ - app/views/golden/menu/hierarchical_menus/index.html.erb
173
+ - app/views/golden/menu/hierarchical_menus/list.html.erb
174
+ - app/views/golden/menu/hierarchical_menus/new.html.erb
175
+ - app/views/golden/menu/hierarchical_menus/show.html.erb
176
+ - config/locales/menus.en.yml
177
+ - config/locales/menus.zh-TW.yml
178
+ - config/routes.rb
179
+ - golden-menu.gemspec
180
+ - lib/generators/golden/menu/USAGE
181
+ - lib/generators/golden/menu/install_generator.rb
182
+ - lib/generators/golden/menu/templates/create_golden_menu_resources.rb
183
+ - lib/generators/golden/menu/templates/create_golden_menus.rb
184
+ - lib/generators/golden/menu/templates/golden_menu.rb
185
+ - lib/generators/golden/menu/templates/menu_resources_helper.rb
186
+ - lib/generators/golden/menu/templates/menus.en.yml
187
+ - lib/generators/golden/menu/templates/menus.zh-TW.yml
188
+ - lib/golden/menu.rb
189
+ - lib/golden/menu/engine.rb
190
+ - lib/golden/menu/option_helper.rb
191
+ - lib/golden/menu/version.rb
192
+ homepage: https://github.com/goldenio/golden-menu
193
+ licenses:
194
+ - MIT
195
+ metadata: {}
196
+ post_install_message:
197
+ rdoc_options: []
198
+ require_paths:
199
+ - lib
200
+ required_ruby_version: !ruby/object:Gem::Requirement
201
+ requirements:
202
+ - - '>='
203
+ - !ruby/object:Gem::Version
204
+ version: '0'
205
+ required_rubygems_version: !ruby/object:Gem::Requirement
206
+ requirements:
207
+ - - '>='
208
+ - !ruby/object:Gem::Version
209
+ version: '0'
210
+ requirements: []
211
+ rubyforge_project:
212
+ rubygems_version: 2.0.3
213
+ signing_key:
214
+ specification_version: 4
215
+ summary: creates menus by building a hierarchiacal tree first and linking related
216
+ resources later
217
+ test_files: []