routing_data 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -14,4 +14,6 @@ rdoc
14
14
  spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
+ dummy/log
17
18
  tmp
19
+ *~
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # RoutingData
2
2
 
3
- TODO: Write a gem description
3
+ Simple solution that provides information about routes in your rails app.
4
4
 
5
5
  ## Installation
6
6
 
@@ -17,8 +17,41 @@ Or install it yourself as:
17
17
  $ gem install routing_data
18
18
 
19
19
  ## Usage
20
+ With RoutingData you have access to url helpers in your models:
20
21
 
21
- TODO: Write usage instructions here
22
+ ```ruby
23
+ RoutingData.url_helpers.new_user_path
24
+ # => "/users/new"
25
+
26
+ RoutingData.url_helpers.user_path(User.first)
27
+ # => "/users/1"
28
+
29
+ RoutingData.url_helpers.post_path(Post.last)
30
+ # => "/posts/2"
31
+ ```
32
+
33
+ You can access to lists of controllers and actions in your application (this is helpful for authorization purposes):
34
+
35
+ ```ruby
36
+ RoutingData.controllers_actions_map
37
+ # => {:home=>[:index], :blogs=>[:index, :create, :new, :edit, :show, :update, :destroy], :categories=>[:index, :create, :new, :edit, :show, :update, :destroy], :posts=>[:index, :create, :new, :edit, :show, :update, :destroy], :users=>[:index, :create, :new, :edit, :show, :update, :destroy, :show]}
38
+
39
+ RoutingData.controllers
40
+ # => [:home, :blogs, :categories, :posts, :users]
41
+
42
+ RoutingData.actions_for(:users)
43
+ # => [:index, :create, :new, :edit, :show, :update, :destroy, :show]
44
+ ```
45
+
46
+ You can also generate json-data about routes and use it in your Javascript (in progress):
47
+
48
+ ```ruby
49
+ RoutingData.routes_data
50
+ # => [{:name=>"root", :path=>"/", :controller=>"home", :action=>"index", :parts=>[]}, {:name=>"blogs", :path=>"/blogs(.:format)", :controller=>"blogs", :action=>"index", :parts=>[:format]}, {:name=>"new_blog", :path=>"/blogs/new(.:format)", :controller=>"blogs", :action=>"new", :parts=>[:format]}, {:name=>"edit_blog", :path=>"/blogs/:id/edit(.:format)", :controller=>"blogs", :action=>"edit", :parts=>[:id, :format]}, {:name=>"blog", :path=>"/blogs/:id(.:format)", :controller=>"blogs", :action=>"show", :parts=>[:id, :format]}, {:name=>"categories", :path=>"/categories(.:format)", :controller=>"categories", :action=>"index", :parts=>[:format]}, {:name=>"new_category", :path=>"/categories/new(.:format)", :controller=>"categories", :action=>"new", :parts=>[:format]}, {:name=>"edit_category", :path=>"/categories/:id/edit(.:format)", :controller=>"categories", :action=>"edit", :parts=>[:id, :format]}, {:name=>"category", :path=>"/categories/:id(.:format)", :controller=>"categories", :action=>"show", :parts=>[:id, :format]}, {:name=>"posts", :path=>"/posts(.:format)", :controller=>"posts", :action=>"index", :parts=>[:format]}, {:name=>"new_post", :path=>"/posts/new(.:format)", :controller=>"posts", :action=>"new", :parts=>[:format]}, {:name=>"edit_post", :path=>"/posts/:id/edit(.:format)", :controller=>"posts", :action=>"edit", :parts=>[:id, :format]}, {:name=>"post", :path=>"/posts/:id(.:format)", :controller=>"posts", :action=>"show", :parts=>[:id, :format]}, {:name=>"users", :path=>"/users(.:format)", :controller=>"users", :action=>"index", :parts=>[:format]}, {:name=>"new_user", :path=>"/users/new(.:format)", :controller=>"users", :action=>"new", :parts=>[:format]}, {:name=>"edit_user", :path=>"/users/:id/edit(.:format)", :controller=>"users", :action=>"edit", :parts=>[:id, :format]}, {:name=>"user", :path=>"/users/:id(.:format)", :controller=>"users", :action=>"show", :parts=>[:id, :format]}, {:name=>"show_user", :path=>"/user/ololo/:user_id(.:format)", :controller=>"users", :action=>"show", :parts=>[:user_id, :format]}]
51
+
52
+ RoutingData.routes_json_data
53
+ # => "[{\"name\":\"root\",\"path\":\"/\",\"controller\":\"home\",\"action\":\"index\",\"parts\":[]},{\"name\":\"blogs\",\"path\":\"/blogs(.:format)\",\"controller\":\"blogs\",\"action\":\"index\",\"parts\":[\"format\"]},{\"name\":\"new_blog\",\"path\":\"/blogs/new(.:format)\",\"controller\":\"blogs\",\"action\":\"new\",\"parts\":[\"format\"]},{\"name\":\"edit_blog\",\"path\":\"/blogs/:id/edit(.:format)\",\"controller\":\"blogs\",\"action\":\"edit\",\"parts\":[\"id\",\"format\"]},{\"name\":\"blog\",\"path\":\"/blogs/:id(.:format)\",\"controller\":\"blogs\",\"action\":\"show\",\"parts\":[\"id\",\"format\"]},{\"name\":\"categories\",\"path\":\"/categories(.:format)\",\"controller\":\"categories\",\"action\":\"index\",\"parts\":[\"format\"]},{\"name\":\"new_category\",\"path\":\"/categories/new(.:format)\",\"controller\":\"categories\",\"action\":\"new\",\"parts\":[\"format\"]},{\"name\":\"edit_category\",\"path\":\"/categories/:id/edit(.:format)\",\"controller\":\"categories\",\"action\":\"edit\",\"parts\":[\"id\",\"format\"]},{\"name\":\"category\",\"path\":\"/categories/:id(.:format)\",\"controller\":\"categories\",\"action\":\"show\",\"parts\":[\"id\",\"format\"]},{\"name\":\"posts\",\"path\":\"/posts(.:format)\",\"controller\":\"posts\",\"action\":\"index\",\"parts\":[\"format\"]},{\"name\":\"new_post\",\"path\":\"/posts/new(.:format)\",\"controller\":\"posts\",\"action\":\"new\",\"parts\":[\"format\"]},{\"name\":\"edit_post\",\"path\":\"/posts/:id/edit(.:format)\",\"controller\":\"posts\",\"action\":\"edit\",\"parts\":[\"id\",\"format\"]},{\"name\":\"post\",\"path\":\"/posts/:id(.:format)\",\"controller\":\"posts\",\"action\":\"show\",\"parts\":[\"id\",\"format\"]},{\"name\":\"users\",\"path\":\"/users(.:format)\",\"controller\":\"users\",\"action\":\"index\",\"parts\":[\"format\"]},{\"name\":\"new_user\",\"path\":\"/users/new(.:format)\",\"controller\":\"users\",\"action\":\"new\",\"parts\":[\"format\"]},{\"name\":\"edit_user\",\"path\":\"/users/:id/edit(.:format)\",\"controller\":\"users\",\"action\":\"edit\",\"parts\":[\"id\",\"format\"]},{\"name\":\"user\",\"path\":\"/users/:id(.:format)\",\"controller\":\"users\",\"action\":\"show\",\"parts\":[\"id\",\"format\"]},{\"name\":\"show_user\",\"path\":\"/user/ololo/:user_id(.:format)\",\"controller\":\"users\",\"action\":\"show\",\"parts\":[\"user_id\",\"format\"]}]"
54
+ ```
22
55
 
23
56
  ## Contributing
24
57
 
@@ -1,3 +1,3 @@
1
1
  module RoutingData
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/routing_data.rb CHANGED
@@ -11,6 +11,16 @@ module RoutingData
11
11
  @all_routes
12
12
  end
13
13
 
14
+ # Returns url helpers with pathes
15
+ def self.url_helpers
16
+ unless @url_helpers
17
+ Rails.application.reload_routes!
18
+ url_helpers_module = Rails.application.routes.url_helpers
19
+ end
20
+
21
+ @url_helpers ||= url_helpers_module
22
+ end
23
+
14
24
  # Returns controllers -> actions map
15
25
  # Example:
16
26
  # RoutingData.controlles_actions_map
@@ -40,9 +50,9 @@ module RoutingData
40
50
 
41
51
  # Returns array of actions for specific controller
42
52
  # Example:
43
- # RoutingData.actions_of(:pages)
53
+ # RoutingData.actions_for(:pages)
44
54
  # #=> [:index, :show, :new, :edit, :create, :update, :destroy]
45
- def self.actions_of(controller)
55
+ def self.actions_for(controller)
46
56
  controllers_actions_map[controller] || []
47
57
  end
48
58
 
@@ -50,4 +60,36 @@ module RoutingData
50
60
  @controllers ||= controllers_actions_map.keys
51
61
  end
52
62
 
63
+ def self.routes_data(opts = {})
64
+ @routes_data ||= all_routes.to_a.map do |route|
65
+ if route.name
66
+ {
67
+ name: route.name,
68
+ path: route.path.spec.to_s,
69
+ controller: route.defaults[:controller],
70
+ action: route.defaults[:action],
71
+ parts: route.parts
72
+ }
73
+ end
74
+ end.compact!
75
+
76
+ if opts[:for_name]
77
+ routes_data = @routes_data.select { |route| route[:name] == opts[:for_name] }
78
+ elsif opts[:for_name_in]
79
+ routes_data = @routes_data.select { |route| route[:for_name_in].include? route[:name] }
80
+ elsif opts[:for_controller]
81
+ routes_data = @routes_data.select { |route| route[:controller] == opts[:for_controller] }
82
+ elsif opts[:for_controllers]
83
+ routes_data = @routes_data.select { |route| opts[:for_controllers].include? route[:controller] }
84
+ else
85
+ routes_data = @routes_data
86
+ end
87
+
88
+ routes_data
89
+ end
90
+
91
+ def self.routes_json_data(opts = {})
92
+ @routes_json_data ||= routes_data(opts).to_json
93
+ end
94
+
53
95
  end
data/routing_data.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |gem|
8
8
  gem.version = RoutingData::VERSION
9
9
  gem.authors = ["Vladimir Melnick"]
10
10
  gem.email = ["egotraumatic@gmail.com"]
11
- gem.description = %q{Simple solution that provides information about routing in your rails app.}
12
- gem.summary = %q{Simple solution that provides information about routing in your rails app.}
11
+ gem.description = %q{Simple solution that provides information about routes in your rails app.}
12
+ gem.summary = %q{Simple solution that provides information about routes in your rails app.}
13
13
  gem.homepage = ""
14
14
 
15
15
  gem.files = `git ls-files`.split($/)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: routing_data
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,9 +9,9 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-20 00:00:00.000000000 Z
12
+ date: 2012-12-09 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: Simple solution that provides information about routing in your rails
14
+ description: Simple solution that provides information about routes in your rails
15
15
  app.
16
16
  email:
17
17
  - egotraumatic@gmail.com
@@ -50,6 +50,6 @@ rubyforge_project:
50
50
  rubygems_version: 1.8.24
51
51
  signing_key:
52
52
  specification_version: 3
53
- summary: Simple solution that provides information about routing in your rails app.
53
+ summary: Simple solution that provides information about routes in your rails app.
54
54
  test_files: []
55
55
  has_rdoc: