routing_data 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -43,7 +43,7 @@ RoutingData.actions_for(:users)
43
43
  # => [:index, :create, :new, :edit, :show, :update, :destroy, :show]
44
44
  ```
45
45
 
46
- You can also generate json-data about routes and use it in your Javascript (in progress):
46
+ You can also generate json-data about routes and use it in your Javascript:
47
47
 
48
48
  ```ruby
49
49
  RoutingData.routes_data
@@ -53,6 +53,37 @@ RoutingData.routes_json_data
53
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
54
  ```
55
55
 
56
+ In your Javascript you can use RoutingData object and url helpers through it. Just add
57
+
58
+ ``` //= require routing_data
59
+
60
+ to app/assets/javascripts/application.js and
61
+
62
+ ```ruby
63
+ <%= include_routing_data %>
64
+ ```
65
+
66
+ in your views before requiring javascripts.
67
+
68
+ After this you can work with RoutingData object:
69
+
70
+ ```javascript
71
+ RoutingData // { routes:{...}, helpers: {...} }
72
+ ```
73
+
74
+ **RoutingData.routes** is made from data from RoutingData.routes_json_data method in Ruby.
75
+
76
+ **RoutingData.helpers** contains url helpers with names like in you Ruby on Rails app. For example:
77
+
78
+ ```javascript
79
+ RoutingData.helpers.new_post_path(); // "/posts/new"
80
+
81
+ RoutingData.helpers.post_path({id: 1}); // "/posts/1"
82
+
83
+ RoutingData.helpers.post_path({id: 1, format: 'xml'}); // "/posts/1.xml"
84
+ ```
85
+
86
+
56
87
  ## Contributing
57
88
 
58
89
  1. Fork it
@@ -0,0 +1,7 @@
1
+ module RoutingData
2
+ class Engine < ::Rails::Engine
3
+ initializer "routing_data.view_helpers" do
4
+ ActionView::Base.send :include, ViewHelpers
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,34 @@
1
+ (function(){
2
+ RoutingData = typeof(RoutingData) == 'undefined' ? {} : RoutingData;
3
+ RoutingData.helpers = {};
4
+
5
+ for(var route in RoutingData.routes) {
6
+ var routeData = RoutingData.routes[route];
7
+ RoutingData.helpers[route + '_path'] = createURLHelper(routeData);
8
+ };
9
+
10
+ function createURLHelper(routeData) {
11
+ return function() {
12
+ var args = typeof(arguments[0]) === 'object' ? arguments[0] : {}
13
+ var path = routeData.path;
14
+
15
+ for(var i in routeData.parts) {
16
+ var part = routeData.parts[i];
17
+ var replacement = '';
18
+
19
+ if(path.indexOf('/:' + part) !== -1) {
20
+ path = path.replace('/:' + part, '/' + args[part]);
21
+ } else if(path.indexOf('(:' + part + ')') !== -1) {
22
+ replacement = typeof(args[part]) !== 'undefined' ? args[part] : replacement;
23
+ path = path.replace('(:' + part + ')', replacement);
24
+ } else if(path.indexOf('(.:' + part + ')') !== -1) {
25
+ replacement = typeof(args[part]) !== 'undefined' ? '.' + args[part] : replacement;
26
+ path = path.replace('(.:' + part + ')', replacement);
27
+ };
28
+ };
29
+
30
+ console.log(path);
31
+ return path;
32
+ };
33
+ };
34
+ })();
@@ -1,3 +1,3 @@
1
1
  module RoutingData
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -0,0 +1,13 @@
1
+ module RoutingData
2
+ module ViewHelpers
3
+ def include_routing_data
4
+ raw(<<-JAVASCRIPT
5
+ <script>
6
+ RoutingData = {};
7
+ RoutingData.routes = #{RoutingData.routes_json_data};
8
+ </script>
9
+ JAVASCRIPT
10
+ )
11
+ end
12
+ end
13
+ end
data/lib/routing_data.rb CHANGED
@@ -1,4 +1,6 @@
1
- require "routing_data/version"
1
+ require 'routing_data/version'
2
+ require 'routing_data/engine'
3
+ require 'routing_data/view_helpers'
2
4
 
3
5
  module RoutingData
4
6
  # Returns Routes objects
@@ -61,17 +63,21 @@ module RoutingData
61
63
  end
62
64
 
63
65
  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
- }
66
+ unless @routes_data
67
+ @routes_data = {}
68
+
69
+ all_routes.to_a.each do |route|
70
+ if route.name
71
+ @routes_data[route.name] = {
72
+ name: route.name,
73
+ path: route.path.spec.to_s,
74
+ controller: route.defaults[:controller],
75
+ action: route.defaults[:action],
76
+ parts: route.parts
77
+ }
78
+ end
73
79
  end
74
- end.compact!
80
+ end
75
81
 
76
82
  if opts[:for_name]
77
83
  routes_data = @routes_data.select { |route| route[:name] == opts[:for_name] }
data/routing_data.gemspec CHANGED
@@ -16,4 +16,6 @@ Gem::Specification.new do |gem|
16
16
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "railties", "~> 3.1"
19
21
  end
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.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,23 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
  date: 2012-12-09 00:00:00.000000000 Z
13
- dependencies: []
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: railties
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.1'
14
30
  description: Simple solution that provides information about routes in your rails
15
31
  app.
16
32
  email:
@@ -25,7 +41,10 @@ files:
25
41
  - README.md
26
42
  - Rakefile
27
43
  - lib/routing_data.rb
44
+ - lib/routing_data/engine.rb
45
+ - lib/routing_data/lib/assets/javascripts/routing_data.js
28
46
  - lib/routing_data/version.rb
47
+ - lib/routing_data/view_helpers.rb
29
48
  - routing_data.gemspec
30
49
  homepage: ''
31
50
  licenses: []