js-routes 0.4.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.md CHANGED
@@ -13,11 +13,9 @@ gem "js-routes"
13
13
  Your application initializer, like `config/initializers/jsroutes.rb`:
14
14
 
15
15
  ``` ruby
16
- JsRoutes.generate!(
17
- :file => "#{Rails.root}app/assets/javascripts/routes.js",
18
- :default_format => "json",
19
- :exclude => [/^admin_/, /paypal/, /^api_/]
20
- )
16
+ JsRoutes.generate!({
17
+ #options
18
+ })
21
19
  ```
22
20
 
23
21
  Available options:
@@ -31,7 +29,7 @@ Available options:
31
29
  * `:include` - Array of regexps to include in js routes. Default: []
32
30
  * Note that regexp applied to **named route** not to *URL*
33
31
  * `:namespace` - global object used to access routes. Default: `Routes`
34
- * Possible variants: `MyProject.routes`, `MyProjectRoutes`
32
+ * Supports nested namespace like `MyProject.routes`
35
33
 
36
34
  Example options usage:
37
35
 
@@ -40,16 +38,16 @@ JsRoutes.generate!(:file => "#{path}/app_routes.js", :namespace => "AppRoutes",
40
38
  JsRoutes.generate!(:file => "#{path}/adm_routes.js", :namespace => "AdmRoutes", :include => /^admin_/, :default_format => "json")
41
39
  ```
42
40
 
43
- In order to generate routes to string and manipulate them yourself use `JsRoutes.generate`:
41
+ In order to generate routes to string and manipulate them yourself use:
44
42
  Like:
45
43
 
46
44
  ``` ruby
47
- JsRoutes.generate(options)
45
+ routes_js = JsRoutes.generate(options)
48
46
  ```
49
47
 
50
48
  ### Usage
51
49
 
52
- This will create a nice javascript file with `Routes` object that has all the rails routes available:
50
+ Configuration above will create a nice javascript file with `Routes` object that has all the rails routes available:
53
51
 
54
52
  ``` js
55
53
  Routes.users_path() // => "/users"
@@ -57,13 +55,30 @@ Routes.user_path(1) // => "/users/1"
57
55
  Routes.user_path(1, {format: 'json'}) // => "/users/1.json"
58
56
  Routes.new_user_project_path(1, {format: 'json'}) // => "/users/1/projects/new.json"
59
57
  Routes.user_project_path(1,2, {q: 'hello', custom: true}) // => "/users/1/projects/2?q=hello&custom=true"
58
+ ```
59
+
60
+ Using serialized object as route function arguments:
60
61
 
62
+ ``` js
63
+ var google = {id: 1, name: "Google"};
64
+ Routes.company_path(google) // => "/companies/1"
65
+ var google = {id: 1, name: "Google", to_param: "google"};
66
+ Routes.company_path(google) // => "/companies/google"
61
67
  ```
62
68
 
63
69
  In order to make routes helpers available globally:
64
70
 
65
71
  ``` js
66
- $.extend(window, Routes)
72
+ jQuery.extend(window, Routes)
67
73
  ```
68
74
 
75
+ ### Alternative
76
+
77
+ There are some alternatives available. Advantages of this one are:
78
+
79
+ * Rails3 support
80
+ * Rich options set
81
+ * Support Rails `#to_param` convention for seo optimized paths
82
+ * Well tested
83
+
69
84
  #### Have fun
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.6.0
data/js-routes.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{js-routes}
8
- s.version = "0.4.0"
8
+ s.version = "0.6.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Bogdan Gusiev"]
12
- s.date = %q{2011-08-18}
12
+ s.date = %q{2011-08-22}
13
13
  s.description = %q{Generates javascript file that defines all Rails named routes as javascript helpers}
14
14
  s.email = %q{agresso@gmail.com}
15
15
  s.extra_rdoc_files = [
data/lib/js_routes.rb CHANGED
@@ -46,24 +46,28 @@ module JsRoutes
46
46
  end
47
47
 
48
48
  def build_js(route, options)
49
+ params = build_params route
49
50
  _ = <<-JS.strip!
50
51
  // #{route.name} => #{route.path}
51
- #{route.name}_path: function(#{build_params route}) {
52
- return Utils.build_path(#{path_parts(route).inspect}, arguments)
52
+ #{route.name}_path: function(#{params.<<("options").join(", ")}) {
53
+ return Utils.build_path(#{params.size}, #{path_parts(route).inspect}, arguments)
53
54
  }
54
55
  JS
55
56
  end
56
57
 
57
58
 
58
59
  def build_params route
59
- route.conditions[:path_info].named_captures.map do |cap|
60
+ route.conditions[:path_info].named_captures.to_a.sort do |cap1, cap2|
61
+ # Hash is not ordered in Ruby 1.8.7
62
+ cap1.last.first <=> cap2.last.first
63
+ end.map do |cap|
60
64
  name = cap.first
61
65
  if !(name.to_s == "format")
62
66
  # prepending each parameter name with underscore
63
67
  # to prevent conflict with JS reserved words
64
68
  "_" + name.to_s.gsub(/^:/, '')
65
69
  end
66
- end.compact.<<("options").join(', ')
70
+ end.compact
67
71
  end
68
72
 
69
73
 
data/lib/routes.js CHANGED
@@ -26,18 +26,33 @@
26
26
  return format ? "." + format : "";
27
27
  },
28
28
 
29
- extract_options: function(args) {
30
- return typeof(args[args.length-1]) == "object" ? args.pop() : {};
29
+ extract_options: function(number_of_params, args) {
30
+ if (args.length >= number_of_params) {
31
+ return typeof(args[args.length-1]) == "object" ? args.pop() : {};
32
+ } else {
33
+ return {};
34
+ }
35
+ },
36
+
37
+ path_identifier: function(object) {
38
+ if (!object) {
39
+ return "";
40
+ }
41
+ if (typeof(object) == "object") {
42
+ return (object.to_param || object.id).toString();
43
+ } else {
44
+ return object.toString();
45
+ }
31
46
  },
32
47
 
33
- build_path: function(parts, args) {
48
+ build_path: function(number_of_params, parts, args) {
34
49
  args = Array.prototype.slice.call(args);
35
50
  result = "";
36
- var opts = Utils.extract_options(args);
51
+ var opts = Utils.extract_options(number_of_params, args);
37
52
  for (var i=0; i < parts.length; i++) {
38
53
  part = parts[i];
39
54
  result += part;
40
- result += args.shift() || "";
55
+ result += Utils.path_identifier(args.shift());
41
56
  }
42
57
  var format = Utils.extract_format(opts);
43
58
  return Utils.clean_path(result + format) + Utils.serialize(opts);
@@ -103,11 +103,31 @@ describe JsRoutes do
103
103
 
104
104
  end
105
105
 
106
+ context "when arguments are objects" do
107
+ it "should use id property of the object" do
108
+ evaljs("Routes.inbox_path({id: 1})").should == "/inboxes/1"
109
+ end
110
+
111
+ it "should use prefer to_param property over id property" do
112
+ evaljs("Routes.inbox_path({id: 1, to_param: 'my'})").should == "/inboxes/my"
113
+ end
114
+
115
+ it "should support still support options argument" do
116
+ evaljs("Routes.inbox_message_path({id:1, to_param: 'my'}, {id:2}, {custom: true, format: 'json'})").should == "/inboxes/my/messages/2.json?custom=true"
117
+ end
118
+ end
119
+
106
120
  describe "generated js" do
107
121
  subject { JsRoutes.generate }
108
- it "should have correct function signature" do
122
+ it "should have correct function without arguments signature" do
123
+ subject.should include("inboxes_path: function(options)")
124
+ end
125
+ it "should have correct function with arguments signature" do
109
126
  subject.should include("inbox_message_path: function(_inbox_id, _id, options)")
110
127
  end
128
+ it "should have correct function signature with Ruby 1.8.7 and unordered hash" do
129
+ subject.should include("inbox_message_attachment_path: function(_inbox_id, _message_id, _id, options)")
130
+ end
111
131
  end
112
132
 
113
133
  describe ".generate!" do
data/spec/spec_helper.rb CHANGED
@@ -15,7 +15,9 @@ end
15
15
  class App < Rails::Application
16
16
  self.routes.draw do
17
17
  resources :inboxes do
18
- resources :messages
18
+ resources :messages do
19
+ resources :attachments
20
+ end
19
21
  end
20
22
 
21
23
  namespace :admin do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: js-routes
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 4
8
+ - 6
9
9
  - 0
10
- version: 0.4.0
10
+ version: 0.6.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Bogdan Gusiev
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-08-18 00:00:00 +03:00
18
+ date: 2011-08-22 00:00:00 +03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency