rails-routes-js-utils 1.0.2 → 1.0.4
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.
@@ -2,7 +2,14 @@
|
|
2
2
|
window.Routes = {};
|
3
3
|
window.AllRoutes = [];
|
4
4
|
function addRouteToEnv(obj) {
|
5
|
-
window.Routes[obj.name+'_path'] =
|
5
|
+
window.Routes[obj.name+'_path'] = function(idOrObject){
|
6
|
+
if (typeof idOrObject == 'object')
|
7
|
+
return obj.replace(idOrObject);
|
8
|
+
return obj.replace({id: idOrObject});
|
9
|
+
};
|
6
10
|
window.AllRoutes.push({path: obj.path,reqs: obj.reqs});
|
7
11
|
}
|
12
|
+
|
13
|
+
addRouteToEnv({name: 'brand_root', path: /^\/$/ , reqs: {"controller":"brand/stores","action":"index","parts":[]}, replace: function(opts) { return '/'; }})
|
14
|
+
|
8
15
|
<%= Rails::Routes::Js::Utils.generate %>
|
@@ -5,13 +5,41 @@ module Rails
|
|
5
5
|
module Routes
|
6
6
|
module Js
|
7
7
|
module Utils
|
8
|
+
|
9
|
+
#DFS Traversal
|
10
|
+
def self.dig(current,js)
|
11
|
+
|
12
|
+
if (!current.respond_to? :left) || (current.class == Journey::Nodes::Symbol) || (current.class == Journey::Nodes::Group)
|
13
|
+
#puts("#{current.class} #{current}")
|
14
|
+
if (current.class == Journey::Nodes::Symbol)
|
15
|
+
#Required
|
16
|
+
js << "opts.#{current.to_s[1..-1]}"
|
17
|
+
else
|
18
|
+
if (current.class == Journey::Nodes::Group)
|
19
|
+
#js << current.to_s
|
20
|
+
else
|
21
|
+
#Literal
|
22
|
+
js << "'#{current.to_s}'"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
else
|
26
|
+
dig(current.left,js)
|
27
|
+
if (current.respond_to? :right)
|
28
|
+
dig(current.right,js)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
8
33
|
def self.generate
|
9
34
|
all_routes = ENV['CONTROLLER'] ? Rails.Application.routes.select { |route| route.defaults[:controller] == ENV['CONTROLLER'] } : IMuaSam::Application.routes
|
10
35
|
all_routes.routes.collect do |route|
|
11
36
|
compiled_regex = route.path.to_regexp.to_javascript
|
12
37
|
reqs = route.defaults.merge(parts: route.parts)
|
38
|
+
|
39
|
+
js = [];
|
40
|
+
dig(route.path.spec,js)
|
13
41
|
if route.name
|
14
|
-
"addRouteToEnv({name: '#{route.name}', path: #{compiled_regex} , reqs: #{reqs.to_json}});"
|
42
|
+
"addRouteToEnv({name: '#{route.name}', path: #{compiled_regex} , reqs: #{reqs.to_json}, replace: function(opts) { return #{js.join('+')}; } });"
|
15
43
|
end
|
16
44
|
end.join("\n");
|
17
45
|
end
|
data/lib/regex.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
class Regexp
|
2
2
|
def to_javascript
|
3
|
-
Regexp.new(inspect.sub('\\A','^').sub('\\Z','$').sub('\\z','$').sub(/^\//,'').sub(/\/[a-z]*$/,'').gsub(/\(\?#.+\)/, '').gsub(/\(\?-\w+:/,'('), self.options).inspect
|
3
|
+
Regexp.new(inspect.sub('\\A','^').sub('\\Z','$').sub('\\z','$').sub(/^\//,'').sub(/\/[a-z]*$/,'').gsub(/\(\?#.+\)/, '').gsub(/\(\?-\w+:/,'(').gsub(/\(\(\(([^()]*)\)\)\)/,"(?:(?:(\\1)))").gsub(/\(\(([^()]*)\)\)/,"(?:(\\1))"), self.options).inspect
|
4
4
|
end
|
5
5
|
end
|