rails-routes-js-utils 0.1.4 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Rails::Routes::Js::Utils
2
2
 
3
- TODO: Write a gem description
3
+ Make rails route available via window.Routes.*_path() and Array window.AllRoutes
4
4
 
5
5
  ## Installation
6
6
 
@@ -16,9 +16,28 @@ Or install it yourself as:
16
16
 
17
17
  $ gem install rails-routes-js-utils
18
18
 
19
+
20
+ Add this to application.js
21
+
22
+ ```
23
+ //= require rails-routes-js-utils
24
+ ```
25
+
19
26
  ## Usage
20
27
 
21
- TODO: Write usage instructions here
28
+ ```javascript
29
+ Routes.blog_post_path(id)
30
+ Routes.blog_posts_path()
31
+
32
+ for (var i=0; i<AllRoutes.length; i++){
33
+ if (AllRoutes[i].path.test(pathname)) {
34
+ console.log(AllRoutes[i]);
35
+ var controller = AllRoutes[i].reqs.controller;
36
+ var action = AllRoutes[i].reqs.action;
37
+ }
38
+ ```
39
+
40
+
22
41
 
23
42
  ## Contributing
24
43
 
@@ -1,40 +1,17 @@
1
1
  require "rails-routes-js-utils/version"
2
2
  require "rails-routes-js-utils/engine"
3
-
3
+ require "regex"
4
4
  module Rails
5
5
  module Routes
6
6
  module Js
7
7
  module Utils
8
8
  def self.generate
9
9
  all_routes = ENV['CONTROLLER'] ? Rails.Application.routes.select { |route| route.defaults[:controller] == ENV['CONTROLLER'] } : IMuaSam::Application.routes
10
- param_split = /\(?\.?:[^\/\(\)]*\)?/
11
- param_replace = '([^\/\(\)]*)'
12
-
13
10
  all_routes.routes.collect do |route|
14
- reqs = route.requirements.empty? ? "{}" : route.requirements.to_json
15
-
16
- this_spec = route.path.spec().to_s
17
- static_part = this_spec.split(param_split)
18
- dynamic_part = this_spec.scan(param_split)
19
- dynamic_part.slice!(-1,1)
20
- compiled_match = ['^']
21
- compiled_replace = ["function() { return ''"]
22
- for i in 0..[static_part.length,dynamic_part.length].max
23
- if (static_part[i] && static_part[i].length)
24
- compiled_match << static_part[i]
25
- compiled_replace << "+'#{static_part[i]}'"
26
- end
27
- if (dynamic_part[i] && dynamic_part[i].length)
28
- compiled_replace << "+arguments[#{i}]"
29
- compiled_match << param_replace
30
- end
31
- end
32
- compiled_match << '\/?$'
33
- compiled_regex = Regexp.new(compiled_match.join());
34
- compiled_replace << ";}";
35
-
11
+ compiled_regex = route.path.to_regexp.to_javascript
12
+ reqs = route.defaults.merge(parts: route.parts)
36
13
  if route.name
37
- "addRouteToEnv({name: '#{route.name}', path: #{compiled_regex.inspect} , reqs: #{reqs}, replace: #{compiled_replace.join()}});"
14
+ "addRouteToEnv({name: '#{route.name}', path: #{compiled_regex} , reqs: #{reqs}});"
38
15
  end
39
16
  end.join("\n");
40
17
  end
@@ -2,7 +2,7 @@ module Rails
2
2
  module Routes
3
3
  module Js
4
4
  module Utils
5
- VERSION = "0.1.4"
5
+ VERSION = "1.0.0"
6
6
  end
7
7
  end
8
8
  end
data/lib/regex.rb ADDED
@@ -0,0 +1,5 @@
1
+ class Regexp
2
+ def to_javascript
3
+ Regexp.new(inspect.sub('\\A','^').sub('\\Z','$').sub('\\z','$').sub(/^\//,'').sub(/\/[a-z]*$/,'').gsub(/\(\?#.+\)/, '').gsub(/\(\?-\w+:/,'('), self.options).inspect
4
+ end
5
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-routes-js-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-27 00:00:00.000000000 Z
12
+ date: 2012-12-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -43,6 +43,7 @@ files:
43
43
  - lib/rails-routes-js-utils.rb
44
44
  - lib/rails-routes-js-utils/engine.rb
45
45
  - lib/rails-routes-js-utils/version.rb
46
+ - lib/regex.rb
46
47
  - rails-routes-js-utils.gemspec
47
48
  homepage: ''
48
49
  licenses: []