cat_router 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +24 -13
- data/app/models/cat_route.rb +11 -6
- data/lib/cat_router/instance_methods.rb +2 -2
- data/lib/cat_router/version.rb +1 -1
- data/lib/generators/templates/migration.rb +9 -6
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -8,6 +8,8 @@ Copyright (c) 2011 [Kristijan Sedlak], released under the MIT license
|
|
8
8
|
|
9
9
|
== Installation (Rails 3 ready)
|
10
10
|
|
11
|
+
NOTE: If you are upgrading please recreate the database table!
|
12
|
+
|
11
13
|
Add this line to your Gemfile (and run bundle install):
|
12
14
|
|
13
15
|
gem 'cat_router'
|
@@ -45,35 +47,38 @@ app/models/cat_route.rb file with content
|
|
45
47
|
|
46
48
|
-------
|
47
49
|
|
48
|
-
|
50
|
+
First you create a root button. By default :locale is set to value of I18n.locale and :domain is not set
|
51
|
+
(which means the route is defined for all domains).
|
49
52
|
|
50
|
-
|
53
|
+
rootRoute = CatRoute.create(:slug => ':root', :title => 'Home', :html => 'This is my home content.')
|
54
|
+
rootRoute = CatRoute.create(:slug => ':root', :title => 'Home', :html => 'This is my home content.', :locale => 'en')
|
55
|
+
rootRoute = CatRoute.create(:slug => ':root', :title => 'Home', :html => 'This is my home content.', :locale => 'en', :domain => 'mydomain.com')
|
51
56
|
|
52
|
-
|
57
|
+
You can then create a route for the main menu.
|
53
58
|
|
54
|
-
myRoute = CatRoute.create(:title => '
|
59
|
+
myRoute = CatRoute.create(:title => 'About us', :html => '<b>This is the about page.</b>')
|
55
60
|
|
56
|
-
The plugin will
|
61
|
+
The plugin will automatically generate the :slug value but you can set it manually.
|
57
62
|
|
58
|
-
myRoute = CatRoute.create(:title => 'Welcome', :html => '<b>welcome</b>', :slug => 'home/first_page'
|
63
|
+
myRoute = CatRoute.create(:title => 'Welcome', :html => '<b>welcome</b>', :slug => 'home/first_page')
|
59
64
|
|
60
|
-
|
65
|
+
You can also create a subroute (a sub button of myRoute).
|
61
66
|
|
62
67
|
mySubRoute = myRoute.cat_routes.create(:title => 'About', :html => 'about us html')
|
63
68
|
|
64
|
-
|
69
|
+
You can also create a button that redirects to some external website.
|
65
70
|
|
66
|
-
|
71
|
+
myRoute = CatRoute.create(:slug => ':redirect_to', :html => 'http://lotterade.com', :title => 'Home')
|
67
72
|
|
68
73
|
-------
|
69
74
|
|
70
|
-
You can get a list of subbuttons
|
75
|
+
You can get a list of subbuttons
|
71
76
|
|
72
77
|
mySubRoutes = CatRoute.find(13).cat_routes
|
73
78
|
|
74
79
|
To get the list of main buttons for your main menu, you will use
|
75
80
|
|
76
|
-
mainButtons = CatRoute.base_routes.with_locale('en').
|
81
|
+
mainButtons = CatRoute.base_routes.with_locale('en').with_or_no_domain('mydomain.com')
|
77
82
|
|
78
83
|
You get a root button like this
|
79
84
|
|
@@ -85,17 +90,23 @@ You get a button by slug like this
|
|
85
90
|
|
86
91
|
-------
|
87
92
|
|
93
|
+
You can reorder the default order of buttons by setting the order column
|
94
|
+
|
95
|
+
button = CatRoute.with_slug('about/our_business').first
|
96
|
+
|
97
|
+
-------
|
98
|
+
|
88
99
|
The @cat_route variable is also available (defined inside CatRoutesController) which holds the current route record.
|
89
100
|
|
90
101
|
Using HAML you will define a main-menu like this
|
91
102
|
|
92
103
|
.mainmenu
|
93
|
-
- CatRoute.base_routes.with_locale(I18n.locale).
|
104
|
+
- CatRoute.base_routes.with_locale(I18n.locale).with_or_no_domain(request.host).ordered.each do |b|
|
94
105
|
= link_to b.title, b.url, :class => b.slug == @cat_route.slug || b.id == @cat_route.parent_id ? 'selected' : ''
|
95
106
|
|
96
107
|
and a sub-menu like this
|
97
108
|
|
98
109
|
.submenu
|
99
|
-
- @cat_route.cat_routes_or_neighbours.each do |b|
|
110
|
+
- @cat_route.cat_routes_or_neighbours.ordered.each do |b|
|
100
111
|
= link_to b.title, b.url, :class => b.slug == @cat_route.slug || b.id == @cat_route.parent_id ? 'selected' : ''
|
101
112
|
|
data/app/models/cat_route.rb
CHANGED
@@ -4,9 +4,11 @@ class CatRoute < ActiveRecord::Base
|
|
4
4
|
|
5
5
|
scope :with_locale, lambda{|locale| where('locale=?', locale) }
|
6
6
|
scope :with_domain, lambda{|domain| where('domain=?', domain) }
|
7
|
-
scope :
|
8
|
-
scope :
|
7
|
+
scope :with_or_no_domain, lambda{|domain| where('domain=? OR domain=""', domain) }
|
8
|
+
scope :base_routes, where('parent_id=0')
|
9
|
+
scope :root_routes, where('slug=":root"')
|
9
10
|
scope :with_slug, lambda{|slug| where('slug=?', slug) }
|
11
|
+
scope :ordered, order('`order`')
|
10
12
|
|
11
13
|
before_validation :set_locale
|
12
14
|
before_validation :set_slug
|
@@ -16,7 +18,10 @@ class CatRoute < ActiveRecord::Base
|
|
16
18
|
validates :locale,
|
17
19
|
:presence => true
|
18
20
|
validates :slug,
|
19
|
-
:uniqueness => { :scope => [:
|
21
|
+
:uniqueness => { :scope => [:locale] }
|
22
|
+
validates :parent_id,
|
23
|
+
:presence => true,
|
24
|
+
:numericality => true
|
20
25
|
|
21
26
|
def url
|
22
27
|
if self.slug == ':redirect_to'
|
@@ -43,9 +48,9 @@ class CatRoute < ActiveRecord::Base
|
|
43
48
|
end
|
44
49
|
|
45
50
|
def set_slug
|
46
|
-
if self.slug.
|
51
|
+
if self.slug.blank?
|
47
52
|
self.slug = ''
|
48
|
-
if self.parent_id
|
53
|
+
if self.parent_id > 0
|
49
54
|
parent_route = CatRoute.find(self.parent_id)
|
50
55
|
self.slug = CatRoute.slug_string(parent_route.title)+'/' if parent_route.present?
|
51
56
|
end
|
@@ -54,7 +59,7 @@ class CatRoute < ActiveRecord::Base
|
|
54
59
|
end
|
55
60
|
|
56
61
|
def self.slug_string(string)
|
57
|
-
string.downcase.gsub(' ', '_')
|
62
|
+
string.downcase.gsub(' ', '_').gsub(',', '_').gsub('.', '_').gsub('-', '_')
|
58
63
|
end
|
59
64
|
|
60
65
|
end
|
@@ -1,12 +1,12 @@
|
|
1
1
|
module CatRouter::InstanceMethods
|
2
2
|
|
3
3
|
def set_locale
|
4
|
-
I18n.locale = :
|
4
|
+
I18n.locale = :en
|
5
5
|
I18n.locale = params[:locale] if params[:locale]
|
6
6
|
end
|
7
7
|
|
8
8
|
def load_cat_route
|
9
|
-
@cat_route = CatRoute.with_slug(params[:slug] ? params[:slug] : '').
|
9
|
+
@cat_route = CatRoute.with_slug(params[:slug] ? params[:slug] : ':root').with_or_no_domain(request.host).first
|
10
10
|
end
|
11
11
|
|
12
12
|
def handle_cat_routes
|
data/lib/cat_router/version.rb
CHANGED
@@ -1,20 +1,23 @@
|
|
1
1
|
class CatRouterMigration < ActiveRecord::Migration
|
2
2
|
def self.up
|
3
3
|
create_table :cat_routes do |t|
|
4
|
-
t.string :locale
|
5
|
-
t.string :domain
|
6
|
-
t.string :title
|
7
|
-
t.string :slug
|
8
|
-
t.text :html
|
9
|
-
t.integer :parent_id
|
4
|
+
t.string :locale, :null => false, :default => 'en'
|
5
|
+
t.string :domain, :null => false, :default => ''
|
6
|
+
t.string :title, :null => false, :default => ''
|
7
|
+
t.string :slug, :null => false, :default => ''
|
8
|
+
t.text :html, :null => false, :default => ''
|
9
|
+
t.integer :parent_id, :null => false, :default => 0
|
10
|
+
t.integer :order, :null => false, :default => 1
|
10
11
|
t.timestamps
|
11
12
|
end
|
12
13
|
|
13
14
|
add_index :cat_routes, :domain
|
14
15
|
add_index :cat_routes, :locale
|
15
16
|
add_index :cat_routes, :slug
|
17
|
+
add_index :cat_routes, :order
|
16
18
|
add_index :cat_routes, [:domain, :locale]
|
17
19
|
add_index :cat_routes, [:domain, :locale, :slug]
|
20
|
+
add_index :cat_routes, [:domain, :locale, :slug, :order]
|
18
21
|
end
|
19
22
|
|
20
23
|
def self.down
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cat_router
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 11
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 8
|
10
|
+
version: 0.1.8
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kristijan Sedlak
|