js-routes 0.9.4 → 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.md CHANGED
@@ -65,9 +65,14 @@ Available options:
65
65
  * Example: http[s]://example.com
66
66
  * Default: false
67
67
 
68
- You can generate routes files on the application side like this:
68
+ ### Very Advanced Setup
69
+
70
+ In case you need multiple route files for different parts of your application, you can not use asset pipeline.
71
+
72
+ You need to generate routes files on the application side like this:
69
73
 
70
74
  ``` ruby
75
+ path = "app/assets/javascripts"
71
76
  JsRoutes.generate!("#{path}/app_routes.js", :namespace => "AppRoutes", :exclude => [/^admin_/, /^api_/])
72
77
  JsRoutes.generate!("#{path}/adm_routes.js", :namespace => "AdmRoutes", :include => /^admin_/)
73
78
  JsRoutes.generate!("#{path}/api_routes.js", :namespace => "ApiRoutes", :include => /^api_/, :default_url_options => {:format => "json"})
@@ -1,3 +1,3 @@
1
1
  class JsRoutes
2
- VERSION = "0.9.4"
2
+ VERSION = "0.9.5"
3
3
  end
data/lib/routes.js CHANGED
@@ -18,6 +18,7 @@
18
18
  Utils = {
19
19
  serialize: function(object, prefix) {
20
20
  var element, i, key, prop, result, s, _i, _len;
21
+
21
22
  if (prefix == null) {
22
23
  prefix = null;
23
24
  }
@@ -64,13 +65,15 @@
64
65
  },
65
66
  clean_path: function(path) {
66
67
  var last_index;
68
+
67
69
  path = path.split("://");
68
70
  last_index = path.length - 1;
69
- path[last_index] = path[last_index].replace(/\/+/g, "/").replace(/.\/$/m, "");
71
+ path[last_index] = path[last_index].replace(/\/+/g, "/");
70
72
  return path.join("://");
71
73
  },
72
74
  set_default_url_options: function(optional_parts, options) {
73
75
  var i, part, _i, _len, _results;
76
+
74
77
  _results = [];
75
78
  for (i = _i = 0, _len = optional_parts.length; _i < _len; i = ++_i) {
76
79
  part = optional_parts[i];
@@ -84,6 +87,7 @@
84
87
  },
85
88
  extract_anchor: function(options) {
86
89
  var anchor;
90
+
87
91
  anchor = "";
88
92
  if (options.hasOwnProperty("anchor")) {
89
93
  anchor = "#" + options.anchor;
@@ -92,15 +96,22 @@
92
96
  return anchor;
93
97
  },
94
98
  extract_options: function(number_of_params, args) {
95
- var ret_value;
96
- ret_value = {};
97
- if (args.length > number_of_params) {
98
- ret_value = args.pop();
99
+ var last_argument, type;
100
+
101
+ last_argument = args[args.length - 1];
102
+ type = this.get_object_type(last_argument);
103
+ if (args.length > number_of_params || (type === "object" && !this.look_like_serialized_model(last_argument))) {
104
+ return args.pop();
105
+ } else {
106
+ return {};
99
107
  }
100
- return ret_value;
108
+ },
109
+ look_like_serialized_model: function(object) {
110
+ return "id" in object || "to_param" in object;
101
111
  },
102
112
  path_identifier: function(object) {
103
113
  var property;
114
+
104
115
  if (object === 0) {
105
116
  return "0";
106
117
  }
@@ -118,6 +129,7 @@
118
129
  },
119
130
  clone: function(obj) {
120
131
  var attr, copy, key;
132
+
121
133
  if ((obj == null) || "object" !== this.get_object_type(obj)) {
122
134
  return obj;
123
135
  }
@@ -131,15 +143,19 @@
131
143
  },
132
144
  prepare_parameters: function(required_parameters, actual_parameters, options) {
133
145
  var i, result, val, _i, _len;
146
+
134
147
  result = this.clone(options) || {};
135
148
  for (i = _i = 0, _len = required_parameters.length; _i < _len; i = ++_i) {
136
149
  val = required_parameters[i];
137
- result[val] = actual_parameters[i];
150
+ if (i < actual_parameters.length) {
151
+ result[val] = actual_parameters[i];
152
+ }
138
153
  }
139
154
  return result;
140
155
  },
141
156
  build_path: function(required_parameters, optional_parts, route, args) {
142
157
  var anchor, opts, parameters, result, url, url_params;
158
+
143
159
  args = Array.prototype.slice.call(args);
144
160
  opts = this.extract_options(required_parameters.length, args);
145
161
  if (args.length > required_parameters.length) {
@@ -158,6 +174,7 @@
158
174
  },
159
175
  visit: function(route, parameters, optional) {
160
176
  var left, left_part, right, right_part, type, value;
177
+
161
178
  if (optional == null) {
162
179
  optional = false;
163
180
  }
@@ -196,6 +213,7 @@
196
213
  },
197
214
  visit_globbing: function(route, parameters, optional) {
198
215
  var left, right, type, value;
216
+
199
217
  type = route[0], left = route[1], right = route[2];
200
218
  if (left.replace(/^\*/i, "") !== left) {
201
219
  route[1] = left = left.replace(/^\*/i, "");
@@ -216,6 +234,7 @@
216
234
  },
217
235
  get_prefix: function() {
218
236
  var prefix;
237
+
219
238
  prefix = defaults.prefix;
220
239
  if (prefix !== "") {
221
240
  prefix = (prefix.match("/$") ? prefix : "" + prefix + "/");
@@ -225,6 +244,7 @@
225
244
  _classToTypeCache: null,
226
245
  _classToType: function() {
227
246
  var name, _i, _len, _ref;
247
+
228
248
  if (this._classToTypeCache != null) {
229
249
  return this._classToTypeCache;
230
250
  }
@@ -238,6 +258,7 @@
238
258
  },
239
259
  get_object_type: function(obj) {
240
260
  var strType;
261
+
241
262
  if (window.jQuery && (window.jQuery.type != null)) {
242
263
  return window.jQuery.type(obj);
243
264
  }
@@ -246,6 +267,7 @@
246
267
  },
247
268
  namespace: function(root, namespaceString) {
248
269
  var current, parts;
270
+
249
271
  parts = (namespaceString ? namespaceString.split(".") : []);
250
272
  if (!parts.length) {
251
273
  return;
data/lib/routes.js.coffee CHANGED
@@ -36,7 +36,7 @@ Utils =
36
36
  clean_path: (path) ->
37
37
  path = path.split("://")
38
38
  last_index = path.length - 1
39
- path[last_index] = path[last_index].replace(/\/+/g, "/").replace(/.\/$/m, "")
39
+ path[last_index] = path[last_index].replace(/\/+/g, "/")
40
40
  path.join "://"
41
41
 
42
42
  set_default_url_options: (optional_parts, options) ->
@@ -52,10 +52,17 @@ Utils =
52
52
  anchor
53
53
 
54
54
  extract_options: (number_of_params, args) ->
55
- ret_value = {}
56
- if args.length > number_of_params
57
- ret_value = args.pop()
58
- ret_value
55
+ last_argument = args[args.length - 1]
56
+ type = @get_object_type(last_argument)
57
+ if args.length > number_of_params or (type == "object" and !@look_like_serialized_model(last_argument))
58
+ args.pop()
59
+ else
60
+ {}
61
+
62
+ look_like_serialized_model: (object) ->
63
+ # consider object a model if it have a path identifier properties like id and to_param
64
+ "id" of object or "to_param" of object
65
+
59
66
 
60
67
  path_identifier: (object) ->
61
68
  return "0" if object is 0
@@ -75,7 +82,9 @@ Utils =
75
82
 
76
83
  prepare_parameters: (required_parameters, actual_parameters, options) ->
77
84
  result = @clone(options) or {}
78
- result[val] = actual_parameters[i] for val, i in required_parameters
85
+ for val, i in required_parameters
86
+ if i < actual_parameters.length
87
+ result[val] = actual_parameters[i]
79
88
  result
80
89
 
81
90
  build_path: (required_parameters, optional_parts, route, args) ->
@@ -42,12 +42,17 @@ describe JsRoutes, "compatibility with Rails" do
42
42
  expect(evaljs("Routes.inbox_path(1, {expanded: true, anchor: 'hello'})")).to eq(routes.inbox_path(1, :expanded => true, :anchor => "hello"))
43
43
  end
44
44
 
45
- it "should support engine routes" do
46
- expect(evaljs("Routes.blog_app_posts_path()")).to eq(blog_routes.posts_path())
47
- end
45
+ context "with rails engines" do
46
+ it "should support simple route" do
47
+ expect(evaljs("Routes.blog_app_posts_path()")).to eq(blog_routes.posts_path())
48
+ end
48
49
 
49
- it "should support engine routes with parameter" do
50
- expect(evaljs("Routes.blog_app_post_path(1)")).to eq(blog_routes.post_path(1))
50
+ it "should support route with parameters" do
51
+ expect(evaljs("Routes.blog_app_post_path(1)")).to eq(blog_routes.post_path(1))
52
+ end
53
+ it "should support root path" do
54
+ expect(evaljs("Routes.blog_app_root_path()")).to eq(blog_routes.root_path)
55
+ end
51
56
  end
52
57
 
53
58
  it "shouldn't require the format" do
@@ -115,6 +120,10 @@ describe JsRoutes, "compatibility with Rails" do
115
120
  it "should support routes globbing in book_title route as array with optional params" do
116
121
  expect(evaljs("Routes.book_title_path('john', ['thrillers', 'comedian'], {some_key: 'some_value'})")).to eq(routes.book_title_path('john', ['thrillers', 'comedian'], {:some_key => 'some_value'}))
117
122
  end
123
+
124
+ it "should support required paramters given as options hash" do
125
+ expect(evaljs("Routes.search_path({q: 'hello'})")).to eq(routes.search_path(:q => 'hello'))
126
+ end
118
127
  end
119
128
 
120
129
  context "when jQuery is present" do
data/spec/spec_helper.rb CHANGED
@@ -58,6 +58,7 @@ end
58
58
  def draw_routes
59
59
 
60
60
  BlogEngine::Engine.routes.draw do
61
+ root to: "application#index"
61
62
  resources :posts
62
63
  end
63
64
  App.routes.draw do
@@ -94,6 +95,8 @@ def draw_routes
94
95
  get '/json_only' => "foo#foo", :format => true, :constraints => {:format => /json/}, :as => :json_only
95
96
 
96
97
  get '/привет' => "foo#foo", :as => :hello
98
+ get '(/o/:organization)/search/:q' => "foo#foo", as: :search
99
+
97
100
  end
98
101
 
99
102
  end
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.4
4
+ version: 0.9.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -188,7 +188,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
188
188
  version: '0'
189
189
  segments:
190
190
  - 0
191
- hash: -2158756967123699365
191
+ hash: 2369940964485562575
192
192
  required_rubygems_version: !ruby/object:Gem::Requirement
193
193
  none: false
194
194
  requirements: