cat_router 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/app/controllers/cat_router_controller.rb +14 -0
- data/app/models/cat_route.rb +59 -0
- data/lib/cat_router/version.rb +1 -1
- metadata +5 -3
@@ -0,0 +1,14 @@
|
|
1
|
+
class CatRouterController < ApplicationController
|
2
|
+
|
3
|
+
def router
|
4
|
+
handle
|
5
|
+
end
|
6
|
+
|
7
|
+
protected
|
8
|
+
|
9
|
+
def handle
|
10
|
+
@cat_route = CatRoute.with_slug(params[:slug] ? params[:slug] : '').first
|
11
|
+
render :layout => true, :inline => @cat_route.html ? @cat_route.html : ''
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
class CatRoute < ActiveRecord::Base
|
2
|
+
has_many :cat_routes, :class_name => "CatRoute", :foreign_key => "parent_id"
|
3
|
+
belongs_to :cat_route, :class_name => "CatRoute"
|
4
|
+
|
5
|
+
scope :with_locale, lambda{|locale| where('locale=?', locale) }
|
6
|
+
scope :base_routes, where('parent_id IS NULL')
|
7
|
+
scope :root_routes, where('slug=""')
|
8
|
+
scope :with_slug, lambda{|slug| where('slug=?', slug) }
|
9
|
+
|
10
|
+
before_validation :set_locale
|
11
|
+
before_validation :set_slug
|
12
|
+
|
13
|
+
validates :title,
|
14
|
+
:presence => true
|
15
|
+
validates :locale,
|
16
|
+
:presence => true
|
17
|
+
validates :slug,
|
18
|
+
:uniqueness => true
|
19
|
+
|
20
|
+
def url
|
21
|
+
if self.slug == ':redirect_to'
|
22
|
+
self.html
|
23
|
+
else
|
24
|
+
"/#{self.locale}/#{self.slug}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def cat_routes_or_neighbours
|
29
|
+
if self.parent_id.present?
|
30
|
+
CatRoute.find(self.parent_id).cat_routes
|
31
|
+
else
|
32
|
+
self.cat_routes
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
protected
|
37
|
+
|
38
|
+
def set_locale
|
39
|
+
if self.locale.nil?
|
40
|
+
self.locale = I18n.locale
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def set_slug
|
45
|
+
if self.slug.nil?
|
46
|
+
self.slug = ''
|
47
|
+
if self.parent_id.present?
|
48
|
+
parent_route = CatRoute.find(self.parent_id)
|
49
|
+
self.slug = CatRoute.slug_string(parent_route.title)+'/' if parent_route.present?
|
50
|
+
end
|
51
|
+
self.slug+= CatRoute.slug_string(self.title)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.slug_string(string)
|
56
|
+
string.downcase.gsub(' ', '_')
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
data/lib/cat_router/version.rb
CHANGED
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: 31
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 2
|
10
|
+
version: 0.1.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kristijan Sedlak
|
@@ -30,6 +30,8 @@ extra_rdoc_files:
|
|
30
30
|
- MIT-LICENSE
|
31
31
|
files:
|
32
32
|
- Rakefile
|
33
|
+
- app/controllers/cat_router_controller.rb
|
34
|
+
- app/models/cat_route.rb
|
33
35
|
- lib/cat_router/routes.rb
|
34
36
|
- lib/cat_router/version.rb
|
35
37
|
- lib/cat_router.rb
|