js-routes 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.md CHANGED
@@ -48,11 +48,11 @@ Available options:
48
48
  * Example: {:format => "json"}
49
49
  * Default: {}
50
50
  * `exclude` - Array of regexps to exclude from js routes.
51
- * Note that regexp applied to **named route** not to *URL*
52
51
  * Default: []
52
+ * The regexp applies only to the name before the `_path` suffix, eg: you want to match exactly `settings_path`, the regexp should be `/^settings$/`
53
53
  * `include` - Array of regexps to include in js routes.
54
- * Note that regexp applied to **named route** not to *URL*
55
54
  * Default: []
55
+ * The regexp applies only to the name before the `_path` suffix, eg: you want to match exactly `settings_path`, the regexp should be `/^settings$/`
56
56
  * `namespace` - global object used to access routes.
57
57
  * Supports nested namespace like `MyProject.routes`
58
58
  * Default: `Routes`
data/js-routes.gemspec CHANGED
@@ -29,5 +29,8 @@ Gem::Specification.new do |s|
29
29
  s.add_development_dependency(%q<guard>, [">= 0"])
30
30
  s.add_development_dependency(%q<rb-fsevent>, [">= 0"])
31
31
  s.add_development_dependency(%q<guard-coffeescript>, [">= 0"])
32
+ if RUBY_VERSION >= "1.9"
33
+ s.add_development_dependency(%q<debugger>, [">= 0"])
34
+ end
32
35
  end
33
36
 
@@ -1,3 +1,3 @@
1
1
  class JsRoutes
2
- VERSION = "0.9.0"
2
+ VERSION = "0.9.1"
3
3
  end
data/lib/js_routes.rb CHANGED
@@ -160,7 +160,6 @@ class JsRoutes
160
160
  _ = <<-JS.strip!
161
161
  // #{name.join('.')} => #{parent_spec}#{route.path.spec}
162
162
  #{route_name}: function(#{build_params(required_parts)}) {
163
- if (!#{LAST_OPTIONS_KEY}){ #{LAST_OPTIONS_KEY} = {}; }
164
163
  return Utils.build_path(#{json(required_parts)}, #{json(optional_parts)}, #{json(serialize(route.path.spec, parent_spec))}, arguments);
165
164
  }#{",\n" + url_link if url_link.length > 0}
166
165
  JS
data/lib/routes.js CHANGED
@@ -16,30 +16,47 @@
16
16
  NodeTypes = NODE_TYPES;
17
17
 
18
18
  Utils = {
19
- serialize: function(obj) {
20
- var i, key, prop, result, s, val, _i, _len;
19
+ serialize: function(object, prefix) {
20
+ var element, i, key, prop, result, s, _i, _len;
21
21
 
22
- if (!obj) {
22
+ if (prefix == null) {
23
+ prefix = null;
24
+ }
25
+ if (!object) {
23
26
  return "";
24
27
  }
28
+ if (!prefix && !(this.get_object_type(object) === "object")) {
29
+ throw new Error("Url parameters should be a javascript hash");
30
+ }
25
31
  if (window.jQuery) {
26
- result = window.jQuery.param(obj);
32
+ result = window.jQuery.param(object);
27
33
  return (!result ? "" : result);
28
34
  }
29
35
  s = [];
30
- for (key in obj) {
31
- if (!__hasProp.call(obj, key)) continue;
32
- prop = obj[key];
33
- if (prop != null) {
34
- if (this.getObjectType(prop) === "array") {
35
- for (i = _i = 0, _len = prop.length; _i < _len; i = ++_i) {
36
- val = prop[i];
37
- s.push("" + key + (encodeURIComponent("[]")) + "=" + (encodeURIComponent(val.toString())));
36
+ switch (this.get_object_type(object)) {
37
+ case "array":
38
+ for (i = _i = 0, _len = object.length; _i < _len; i = ++_i) {
39
+ element = object[i];
40
+ s.push(this.serialize(element, prefix + "[]"));
41
+ }
42
+ break;
43
+ case "object":
44
+ for (key in object) {
45
+ if (!__hasProp.call(object, key)) continue;
46
+ prop = object[key];
47
+ if (!(prop != null)) {
48
+ continue;
38
49
  }
39
- } else {
40
- s.push("" + key + "=" + (encodeURIComponent(prop.toString())));
50
+ if (prefix != null) {
51
+ key = "" + prefix + "[" + key + "]";
52
+ }
53
+ s.push(this.serialize(prop, key));
54
+ }
55
+ break;
56
+ default:
57
+ if (object) {
58
+ s.push("" + (encodeURIComponent(prefix.toString())) + "=" + (encodeURIComponent(object.toString())));
41
59
  }
42
- }
43
60
  }
44
61
  if (!s.length) {
45
62
  return "";
@@ -51,7 +68,7 @@
51
68
 
52
69
  path = path.split("://");
53
70
  last_index = path.length - 1;
54
- path[last_index] = path[last_index].replace(/\/+/g, "/").replace(/\/$/m, "");
71
+ path[last_index] = path[last_index].replace(/\/+/g, "/").replace(/.\/$/m, "");
55
72
  return path.join("://");
56
73
  },
57
74
  set_default_url_options: function(optional_parts, options) {
@@ -82,7 +99,7 @@
82
99
  var ret_value;
83
100
 
84
101
  ret_value = {};
85
- if (args.length > number_of_params && this.getObjectType(args[args.length - 1]) === "object") {
102
+ if (args.length > number_of_params) {
86
103
  ret_value = args.pop();
87
104
  }
88
105
  return ret_value;
@@ -97,9 +114,9 @@
97
114
  return "";
98
115
  }
99
116
  property = object;
100
- if (this.getObjectType(object) === "object") {
117
+ if (this.get_object_type(object) === "object") {
101
118
  property = object.to_param || object.id || object;
102
- if (this.getObjectType(property) === "function") {
119
+ if (this.get_object_type(property) === "function") {
103
120
  property = property.call(object);
104
121
  }
105
122
  }
@@ -108,7 +125,7 @@
108
125
  clone: function(obj) {
109
126
  var attr, copy, key;
110
127
 
111
- if ((obj == null) || "object" !== this.getObjectType(obj)) {
128
+ if ((obj == null) || "object" !== this.get_object_type(obj)) {
112
129
  return obj;
113
130
  }
114
131
  copy = obj.constructor();
@@ -194,7 +211,7 @@
194
211
  return this.visit(route, parameters, optional);
195
212
  }
196
213
  parameters[left] = (function() {
197
- switch (this.getObjectType(value)) {
214
+ switch (this.get_object_type(value)) {
198
215
  case "array":
199
216
  return value.join("/");
200
217
  default:
@@ -227,7 +244,7 @@
227
244
  }
228
245
  return this._classToTypeCache;
229
246
  },
230
- getObjectType: function(obj) {
247
+ get_object_type: function(obj) {
231
248
  var strType;
232
249
 
233
250
  if (window.jQuery && (window.jQuery.type != null)) {
data/lib/routes.js.coffee CHANGED
@@ -5,27 +5,38 @@ defaults =
5
5
  default_url_options: DEFAULT_URL_OPTIONS
6
6
 
7
7
  NodeTypes = NODE_TYPES
8
+
8
9
  Utils =
9
10
 
10
- serialize: (obj) ->
11
- return "" unless obj
11
+ serialize: (object, prefix = null) ->
12
+ return "" unless object
13
+ if !prefix and !(@get_object_type(object) is "object")
14
+ throw new Error("Url parameters should be a javascript hash")
15
+
12
16
  if window.jQuery
13
- result = window.jQuery.param(obj)
17
+ result = window.jQuery.param(object)
14
18
  return (if not result then "" else result)
19
+
15
20
  s = []
16
- for own key, prop of obj when prop?
17
- if @getObjectType(prop) is "array"
18
- for val, i in prop
19
- s.push "#{key}#{encodeURIComponent("[]")}=#{encodeURIComponent(val.toString())}"
21
+ switch @get_object_type(object)
22
+ when "array"
23
+ for element, i in object
24
+ s.push @serialize(element, prefix + "[]")
25
+ when "object"
26
+ for own key, prop of object when prop?
27
+ key = "#{prefix}[#{key}]" if prefix?
28
+ s.push @serialize(prop, key)
20
29
  else
21
- s.push "#{key}=#{encodeURIComponent(prop.toString())}"
30
+ if object
31
+ s.push "#{encodeURIComponent(prefix.toString())}=#{encodeURIComponent(object.toString())}"
32
+
22
33
  return "" unless s.length
23
34
  s.join("&")
24
35
 
25
36
  clean_path: (path) ->
26
37
  path = path.split("://")
27
38
  last_index = path.length - 1
28
- path[last_index] = path[last_index].replace(/\/+/g, "/").replace(/\/$/m, "")
39
+ path[last_index] = path[last_index].replace(/\/+/g, "/").replace(/.\/$/m, "")
29
40
  path.join "://"
30
41
 
31
42
  set_default_url_options: (optional_parts, options) ->
@@ -42,7 +53,7 @@ Utils =
42
53
 
43
54
  extract_options: (number_of_params, args) ->
44
55
  ret_value = {}
45
- if args.length > number_of_params and @getObjectType(args[args.length - 1]) is "object"
56
+ if args.length > number_of_params
46
57
  ret_value = args.pop()
47
58
  ret_value
48
59
 
@@ -51,13 +62,13 @@ Utils =
51
62
  # null, undefined, false or ''
52
63
  return "" unless object
53
64
  property = object
54
- if @getObjectType(object) is "object"
65
+ if @get_object_type(object) is "object"
55
66
  property = object.to_param or object.id or object
56
- property = property.call(object) if @getObjectType(property) is "function"
67
+ property = property.call(object) if @get_object_type(property) is "function"
57
68
  property.toString()
58
69
 
59
70
  clone: (obj) ->
60
- return obj if !obj? or "object" isnt @getObjectType(obj)
71
+ return obj if !obj? or "object" isnt @get_object_type(obj)
61
72
  copy = obj.constructor()
62
73
  copy[key] = attr for own key, attr of obj
63
74
  copy
@@ -70,7 +81,9 @@ Utils =
70
81
  build_path: (required_parameters, optional_parts, route, args) ->
71
82
  args = Array::slice.call(args)
72
83
  opts = @extract_options(required_parameters.length, args)
73
- throw new Error("Too many parameters provided for path") if args.length > required_parameters.length
84
+
85
+ if args.length > required_parameters.length
86
+ throw new Error("Too many parameters provided for path")
74
87
  parameters = @prepare_parameters(required_parameters, args, opts)
75
88
  @set_default_url_options optional_parts, parameters
76
89
  result = "#{@get_prefix()}#{@visit(route, parameters)}"
@@ -126,7 +139,7 @@ Utils =
126
139
  [type, left, right] = route
127
140
  value = parameters[left]
128
141
  return @visit(route, parameters, optional) unless value?
129
- parameters[left] = switch @getObjectType(value)
142
+ parameters[left] = switch @get_object_type(value)
130
143
  when "array"
131
144
  value.join("/")
132
145
  else
@@ -173,7 +186,7 @@ Utils =
173
186
  for name in "Boolean Number String Function Array Date RegExp Undefined Null".split(" ")
174
187
  @_classToTypeCache["[object " + name + "]"] = name.toLowerCase()
175
188
  @_classToTypeCache
176
- getObjectType: (obj) ->
189
+ get_object_type: (obj) ->
177
190
  return window.jQuery.type(obj) if window.jQuery and window.jQuery.type?
178
191
  strType = Object::toString.call(obj)
179
192
  @_classToType()[strType] or "object"
@@ -39,6 +39,11 @@ describe JsRoutes, "compatibility with Rails" do
39
39
  evaljs("Routes.inbox_path(1, {hello: ['world', 'mars']})").should == routes.inbox_path(1, :hello => [:world, :mars])
40
40
  end
41
41
 
42
+ it "should support nested get parameters" do
43
+ evaljs("Routes.inbox_path(1, {format: 'json', env: 'test', search: { category_ids: [2,5], q: 'hello'}})").should ==
44
+ routes.inbox_path(1, :env => 'test', :search => {:category_ids => [2,5], :q => "hello"}, :format => "json")
45
+ end
46
+
42
47
  it "should support null and undefined parameters" do
43
48
  evaljs("Routes.inboxes_path({uri: null, key: undefined})").should == routes.inboxes_path(:uri => nil, :key => nil)
44
49
  end
@@ -71,6 +76,10 @@ describe JsRoutes, "compatibility with Rails" do
71
76
  evaljs("Routes.hello_path()").should == routes.hello_path
72
77
  end
73
78
 
79
+ it "should support root_path" do
80
+ evaljs("Routes.root_path()").should == routes.root_path
81
+ end
82
+
74
83
  context "routes globbing" do
75
84
  it "should be supported as parameters" do
76
85
  evaljs("Routes.book_path('thrillers', 1)").should == routes.book_path('thrillers', 1)
data/spec/spec_helper.rb CHANGED
@@ -52,6 +52,8 @@ def draw_routes
52
52
  end
53
53
  end
54
54
 
55
+ root :to => "inboxes#index"
56
+
55
57
  namespace :admin do
56
58
  resources :users
57
59
  end
@@ -95,7 +97,7 @@ RSpec.configure do |config|
95
97
  # No need to replace native V8 functions for now
96
98
  #jscontext[:cgi] = CGI
97
99
  #evaljs("function encodeURIComponent(string) {return cgi.escape(string);}")
98
- jscontext[:log] = lambda {|arg| puts arg.inspect}
100
+ jscontext[:log] = lambda {|context, value| puts value.inspect}
99
101
  end
100
102
  config.before(:all) do
101
103
  # compile all js files begin
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: js-routes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -123,6 +123,22 @@ dependencies:
123
123
  - - ! '>='
124
124
  - !ruby/object:Gem::Version
125
125
  version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: debugger
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
126
142
  description: Generates javascript file that defines all Rails named routes as javascript
127
143
  helpers
128
144
  email: agresso@gmail.com
@@ -169,7 +185,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
169
185
  version: '0'
170
186
  segments:
171
187
  - 0
172
- hash: 1949067975122087130
188
+ hash: 3670636352927995574
173
189
  required_rubygems_version: !ruby/object:Gem::Requirement
174
190
  none: false
175
191
  requirements:
@@ -178,7 +194,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
178
194
  version: '0'
179
195
  requirements: []
180
196
  rubyforge_project:
181
- rubygems_version: 1.8.24
197
+ rubygems_version: 1.8.25
182
198
  signing_key:
183
199
  specification_version: 3
184
200
  summary: Brings Rails named routes to javascript