js-routes 0.8.4 → 0.8.5

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/Rakefile CHANGED
@@ -28,12 +28,11 @@ Jeweler::RubygemsDotOrgTasks.new
28
28
  require 'rspec/core'
29
29
  require 'rspec/core/rake_task'
30
30
  RSpec::Core::RakeTask.new(:spec) do |spec|
31
- spec.pattern = FileList['spec/**/*_spec.rb']
32
- end
33
-
34
- RSpec::Core::RakeTask.new(:rcov) do |spec|
35
- spec.pattern = 'spec/**/*_spec.rb'
36
- spec.rcov = true
31
+ spec.pattern = FileList['spec/**/*_spec.rb'].sort_by do|n|
32
+ # we need to run post_rails_init_spec as the latest
33
+ # because it cause unrevertable changes to runtime
34
+ n.include?("post_rails_init_spec") ? 1 : 0
35
+ end
37
36
  end
38
37
 
39
38
  task :default => :spec
data/Readme.md CHANGED
@@ -38,6 +38,7 @@ Available options:
38
38
  * `namespace` - global object used to access routes. Default: `Routes`
39
39
  * Supports nested namespace like `MyProject.routes`
40
40
  * `prefix` - String representing a url path to prepend to all paths. Default: blank
41
+ * Example: `http://yourdomain.com`. This will cause route helpers to generate full path only.
41
42
 
42
43
 
43
44
  ### Advanced Setup
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.8.4
1
+ 0.8.5
@@ -1 +1,2 @@
1
+ <%# encoding: UTF-8 %>
1
2
  <%= JsRoutes.assert_usable_configuration! && JsRoutes.generate %>
data/js-routes.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "js-routes"
8
- s.version = "0.8.4"
8
+ s.version = "0.8.5"
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 = "2012-07-10"
12
+ s.date = "2013-01-04"
13
13
  s.description = "Generates javascript file that defines all Rails named routes as javascript helpers"
14
14
  s.email = "agresso@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -32,8 +32,10 @@ Gem::Specification.new do |s|
32
32
  "lib/js_routes/engine.rb",
33
33
  "lib/routes.js",
34
34
  "lib/tasks/js_routes.rake",
35
- "spec/js_routes_spec.rb",
36
- "spec/post_rails_init_spec.rb",
35
+ "spec/js_routes/generated_javascript_spec.rb",
36
+ "spec/js_routes/options_spec.rb",
37
+ "spec/js_routes/post_rails_init_spec.rb",
38
+ "spec/js_routes/rails_routes_compatibility_spec.rb",
37
39
  "spec/spec_helper.rb"
38
40
  ]
39
41
  s.homepage = "http://github.com/railsware/js-routes"
data/lib/js_routes.rb CHANGED
@@ -123,10 +123,10 @@ class JsRoutes
123
123
 
124
124
  def build_route_if_match(route, parent_route=nil)
125
125
  if any_match?(route, parent_route, @options[:exclude]) || !any_match?(route, parent_route, @options[:include])
126
- nil
127
- else
128
- build_js(route, parent_route)
129
- end
126
+ nil
127
+ else
128
+ build_js(route, parent_route)
129
+ end
130
130
  end
131
131
 
132
132
  def any_match?(route, parent_route, matchers)
@@ -187,3 +187,4 @@ class JsRoutes
187
187
  ]
188
188
  end
189
189
  end
190
+
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+ require "fileutils"
3
+
4
+
5
+ describe JsRoutes do
6
+ before(:each) do
7
+ evaljs(JsRoutes.generate)
8
+ end
9
+
10
+ describe "generated js" do
11
+ subject { JsRoutes.generate }
12
+ it "should have correct function without arguments signature" do
13
+ should include("inboxes_path: function(options)")
14
+ end
15
+ it "should have correct function with arguments signature" do
16
+ should include("inbox_message_path: function(_inbox_id, _id, options)")
17
+ end
18
+ it "should have correct function signature with Ruby 1.8.7 and unordered hash" do
19
+ should include("inbox_message_attachment_path: function(_inbox_id, _message_id, _id, options)")
20
+ end
21
+
22
+ it "routes should be sorted in alphabetical order" do
23
+ subject.index("book_path").should <= subject.index("inboxes_path")
24
+ end
25
+ end
26
+
27
+ describe ".generate!" do
28
+
29
+ let(:name) { "#{File.dirname(__FILE__)}/../routes.js" }
30
+
31
+ before(:each) do
32
+ FileUtils.rm_f(name)
33
+ JsRoutes.generate!({:file => name})
34
+ end
35
+
36
+ it "should not generate file before initialization" do
37
+ # This method is alread fixed in Rails master
38
+ # But in 3.2 stable we need to hack it like this
39
+ if Rails.application.instance_variable_get("@initialized")
40
+ pending
41
+ end
42
+ File.exists?(name).should be_false
43
+ end
44
+
45
+ after(:all) do
46
+ FileUtils.rm_f(name)
47
+ end
48
+ end
49
+
50
+ describe "compiled javascript asset" do
51
+ subject { ERB.new(File.read("app/assets/javascripts/js-routes.js.erb")).result(binding) }
52
+ it "should have js routes code" do
53
+ should include("inbox_message_path: function(_inbox_id, _id, options)")
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,134 @@
1
+ require 'spec_helper'
2
+
3
+ describe JsRoutes, "options" do
4
+
5
+ before(:each) do
6
+ evaljs(_presetup)
7
+ evaljs(JsRoutes.generate(_options))
8
+ end
9
+
10
+ let(:_presetup) { "this;" }
11
+ let(:_options) { {} }
12
+
13
+ context "when exclude is specified" do
14
+
15
+ let(:_options) { {:exclude => /^admin_/} }
16
+
17
+ it "should exclude specified routes from file" do
18
+ evaljs("Routes.admin_users_path").should be_nil
19
+ end
20
+
21
+ it "should not exclude routes not under specified pattern" do
22
+ evaljs("Routes.inboxes_path()").should_not be_nil
23
+ end
24
+ end
25
+ context "when include is specified" do
26
+
27
+ let(:_options) { {:include => /^admin_/} }
28
+
29
+ it "should exclude specified routes from file" do
30
+ evaljs("Routes.admin_users_path()").should_not be_nil
31
+ end
32
+
33
+ it "should not exclude routes not under specified pattern" do
34
+ evaljs("Routes.inboxes_path").should be_nil
35
+ end
36
+ end
37
+
38
+ context "when prefix with trailing slash is specified" do
39
+
40
+ let(:_options) { {:prefix => "/myprefix/" } }
41
+
42
+ it "should render routing with prefix" do
43
+ evaljs("Routes.inbox_path(1)").should == "/myprefix#{routes.inbox_path(1)}"
44
+ end
45
+
46
+ it "should render routing with prefix set in JavaScript" do
47
+ evaljs("Routes.options.prefix = '/newprefix/'")
48
+ evaljs("Routes.inbox_path(1)").should == "/newprefix#{routes.inbox_path(1)}"
49
+ end
50
+
51
+ end
52
+
53
+ context "when prefix with http:// is specified" do
54
+
55
+ let(:_options) { {:prefix => "http://localhost:3000" } }
56
+
57
+ it "should render routing with prefix" do
58
+ evaljs("Routes.inbox_path(1)").should == _options[:prefix] + routes.inbox_path(1)
59
+ end
60
+ end
61
+
62
+ context "when prefix without trailing slash is specified" do
63
+
64
+ let(:_options) { {:prefix => "/myprefix" } }
65
+
66
+ it "should render routing with prefix" do
67
+ evaljs("Routes.inbox_path(1)").should == "/myprefix#{routes.inbox_path(1)}"
68
+ end
69
+
70
+ it "should render routing with prefix set in JavaScript" do
71
+ evaljs("Routes.options.prefix = '/newprefix'")
72
+ evaljs("Routes.inbox_path(1)").should == "/newprefix#{routes.inbox_path(1)}"
73
+ end
74
+
75
+ end
76
+
77
+ context "when default_format is specified" do
78
+ let(:_options) { {:default_format => "json"} }
79
+
80
+ it "should render routing with default_format" do
81
+ evaljs("Routes.inbox_path(1)").should == routes.inbox_path(1, :format => "json")
82
+ end
83
+
84
+ it "should override default_format when spefified implicitly" do
85
+ evaljs("Routes.inbox_path(1, {format: 'xml'})").should == routes.inbox_path(1, :format => "xml")
86
+ end
87
+
88
+ it "should override nullify implicitly when specified implicitly" do
89
+ evaljs("Routes.inbox_path(1, {format: null})").should == routes.inbox_path(1)
90
+ end
91
+
92
+ it "shouldn't include the format when {:format => false} is specified" do
93
+ evaljs("Routes.no_format_path()").should == routes.no_format_path
94
+ end
95
+
96
+ it "shouldn't require the format" do
97
+ evaljs("Routes.json_only_path()").should == routes.json_only_path(:format => 'json')
98
+ end
99
+ end
100
+
101
+ describe "when namespace option is specified" do
102
+ let(:_options) { {:namespace => "PHM"} }
103
+ it "should use this namespace for routing" do
104
+ evaljs("window.Routes").should be_nil
105
+ evaljs("PHM.inbox_path").should_not be_nil
106
+ end
107
+ end
108
+
109
+ describe "when nested namespace option is specified" do
110
+ context "and defined on client" do
111
+ let(:_presetup) { "window.PHM = {}" }
112
+ let(:_options) { {:namespace => "PHM.Routes"} }
113
+ it "should use this namespace for routing" do
114
+ evaljs("PHM.Routes.inbox_path").should_not be_nil
115
+ end
116
+ end
117
+
118
+ context "but undefined on client" do
119
+ let(:_options) { {:namespace => "PHM.Routes"} }
120
+ it "should initialize namespace" do
121
+ evaljs("window.PHM.Routes.inbox_path").should_not be_nil
122
+ end
123
+ end
124
+
125
+ context "and some parts are defined" do
126
+ let(:_presetup) { "window.PHM = { Utils: {} };" }
127
+ let(:_options) { {:namespace => "PHM.Routes"} }
128
+ it "should not overwrite existing parts" do
129
+ evaljs("window.PHM.Utils").should_not be_nil
130
+ evaljs("window.PHM.Routes.inbox_path").should_not be_nil
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,202 @@
1
+ require "spec_helper"
2
+
3
+ describe JsRoutes, "compatibility with Rails" do
4
+
5
+ before(:each) do
6
+ evaljs(JsRoutes.generate({}))
7
+ end
8
+
9
+
10
+ it "should generate collection routing" do
11
+ evaljs("Routes.inboxes_path()").should == routes.inboxes_path()
12
+ end
13
+
14
+ it "should generate member routing" do
15
+ evaljs("Routes.inbox_path(1)").should == routes.inbox_path(1)
16
+ end
17
+
18
+ it "should generate nested routing with one parameter" do
19
+ evaljs("Routes.inbox_messages_path(1)").should == routes.inbox_messages_path(1)
20
+ end
21
+
22
+ it "should generate nested routing" do
23
+ evaljs("Routes.inbox_message_path(1,2)").should == routes.inbox_message_path(1, 2)
24
+ end
25
+
26
+ it "should generate routing with format" do
27
+ evaljs("Routes.inbox_path(1, {format: 'json'})").should == routes.inbox_path(1, :format => "json")
28
+ end
29
+
30
+ it "should support simple get parameters" do
31
+ evaljs("Routes.inbox_path(1, {format: 'json', lang: 'ua', q: 'hello'})").should == routes.inbox_path(1, :lang => "ua", :q => "hello", :format => "json")
32
+ end
33
+
34
+ it "should support array get parameters" do
35
+ evaljs("Routes.inbox_path(1, {hello: ['world', 'mars']})").should == routes.inbox_path(1, :hello => [:world, :mars])
36
+ end
37
+
38
+ it "should support null and undefined parameters" do
39
+ evaljs("Routes.inboxes_path({uri: null, key: undefined})").should == routes.inboxes_path(:uri => nil, :key => nil)
40
+ end
41
+
42
+ it "should escape get parameters" do
43
+ evaljs("Routes.inboxes_path({uri: 'http://example.com'})").should == routes.inboxes_path(:uri => 'http://example.com')
44
+ end
45
+
46
+ it "should support routes with reserved javascript words as parameters" do
47
+ evaljs("Routes.object_path(1, 2)").should == routes.object_path(1,2)
48
+ end
49
+
50
+ it "should support url anchor given as parameter" do
51
+ evaljs("Routes.inbox_path(1, {anchor: 'hello'})").should == routes.inbox_path(1, :anchor => "hello")
52
+ end
53
+
54
+ it "should support engine routes" do
55
+ evaljs("Routes.blog_app_posts_path()").should == blog_routes.posts_path()
56
+ end
57
+
58
+ it "should support engine routes with parameter" do
59
+ evaljs("Routes.blog_app_post_path(1)").should == blog_routes.post_path(1)
60
+ end
61
+
62
+ it "shouldn't require the format" do
63
+ evaljs("Routes.json_only_path({format: 'json'})").should == routes.json_only_path(:format => 'json')
64
+ end
65
+
66
+ it "should support utf-8 route" do
67
+ evaljs("Routes.hello_path()").should == routes.hello_path
68
+ end
69
+
70
+ context "routes globbing" do
71
+ it "should be supported as parameters" do
72
+ evaljs("Routes.book_path('thrillers', 1)").should == routes.book_path('thrillers', 1)
73
+ end
74
+
75
+ it "should support routes globbing as hash" do
76
+ pending
77
+ evaljs("Routes.book_path(1, {section: 'thrillers'})").should == routes.book_path(1, :section => 'thrillers')
78
+ end
79
+
80
+ it "should bee support routes globbing as hash" do
81
+ pending
82
+ evaljs("Routes.book_path(1)").should == routes.book_path(1)
83
+ end
84
+ end
85
+
86
+ context "when jQuery is present" do
87
+ before do
88
+ evaljs("window.jQuery = {};")
89
+ jscontext[:parameterize] = lambda {|object| _value.to_param}
90
+ evaljs("window.jQuery.param = parameterize")
91
+ end
92
+
93
+ shared_examples_for "serialization" do
94
+ it "should support serialization of objects" do
95
+ evaljs("window.jQuery.param(#{_value.to_json})").should == _value.to_param
96
+ evaljs("Routes.inboxes_path(#{_value.to_json})").should == routes.inboxes_path(_value)
97
+ end
98
+ end
99
+ context "when parameters is a hash" do
100
+ let(:_value) do
101
+ {:a => {:b => 'c'}, :q => [1,2]}
102
+ end
103
+ it_should_behave_like 'serialization'
104
+ end
105
+ context "when parameters is null" do
106
+ let(:_value) do
107
+ nil
108
+ end
109
+ it_should_behave_like 'serialization'
110
+ end
111
+ end
112
+
113
+ context "using optional path fragments" do
114
+ context "including not optional parts" do
115
+ it "should include everything that is not optional" do
116
+ evaljs("Routes.foo_path()").should == routes.foo_path
117
+ end
118
+ end
119
+
120
+ context "but not including them" do
121
+ it "should not include the optional parts" do
122
+ evaljs("Routes.things_path()").should == routes.things_path
123
+ end
124
+
125
+ it "should not require the optional parts as arguments" do
126
+ #TODO: fix this inconsistence
127
+ pending
128
+ evaljs("Routes.thing_path(null, 5)").should == routes.thing_path(nil, 5)
129
+ end
130
+
131
+ it "should treat undefined as non-given optional part" do
132
+ evaljs("Routes.thing_path(5, {optional_id: undefined})").should == routes.thing_path(5, :optional_id => nil)
133
+ end
134
+
135
+ it "should treat null as non-given optional part" do
136
+ evaljs("Routes.thing_path(5, {optional_id: null})").should == routes.thing_path(5, :optional_id => nil)
137
+ end
138
+ end
139
+
140
+ context "and including them" do
141
+ it "should include the optional parts" do
142
+ evaljs("Routes.things_path({optional_id: 5})").should == routes.things_path(:optional_id => 5)
143
+ end
144
+ end
145
+ end
146
+
147
+ context "when wrong parameters given" do
148
+
149
+ it "should throw Exception if not enough parameters" do
150
+ lambda {
151
+ evaljs("Routes.inbox_path()")
152
+ }.should raise_error(V8::JSError)
153
+ end
154
+ it "should throw Exception if required parameter is not defined" do
155
+ lambda {
156
+ evaljs("Routes.inbox_path(null)")
157
+ }.should raise_error(V8::JSError)
158
+ end
159
+
160
+ it "should throw Exceptions if when there is too many parameters" do
161
+ lambda {
162
+ evaljs("Routes.inbox_path(1,2)")
163
+ }.should raise_error(V8::JSError)
164
+ end
165
+ end
166
+
167
+ context "when javascript engine without Array#indexOf is used" do
168
+ before(:each) do
169
+ evaljs("Array.prototype.indexOf = null")
170
+ end
171
+ it "should still work correctly" do
172
+ evaljs("Routes.inboxes_path()").should == routes.inboxes_path()
173
+ end
174
+ end
175
+
176
+ context "when arguments are objects" do
177
+
178
+ let(:inbox) {Struct.new(:id, :to_param).new(1,"my")}
179
+
180
+ it "should use id property of the object in path" do
181
+ evaljs("Routes.inbox_path({id: 1})").should == routes.inbox_path(1)
182
+ end
183
+
184
+ it "should prefer to_param property over id property" do
185
+ evaljs("Routes.inbox_path({id: 1, to_param: 'my'})").should == routes.inbox_path(inbox)
186
+ end
187
+
188
+ it "should call to_param if it is a function" do
189
+ evaljs("Routes.inbox_path({id: 1, to_param: function(){ return 'my';}})").should == routes.inbox_path(inbox)
190
+ end
191
+
192
+ it "should call id if it is a function" do
193
+ evaljs("Routes.inbox_path({id: function() { return 1;}})").should == routes.inbox_path(1)
194
+ end
195
+
196
+ it "should support options argument" do
197
+ evaljs(
198
+ "Routes.inbox_message_path({id:1, to_param: 'my'}, {id:2}, {custom: true, format: 'json'})"
199
+ ).should == routes.inbox_message_path(inbox, 2, :custom => true, :format => "json")
200
+ end
201
+ end
202
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
4
  $LOAD_PATH.unshift(File.dirname(__FILE__))
3
5
  require 'rspec'
@@ -15,15 +17,20 @@ def evaljs(string)
15
17
  jscontext.eval(string)
16
18
  end
17
19
 
20
+ def routes
21
+ App.routes.url_helpers
22
+ end
23
+
24
+ def blog_routes
25
+ BlogEngine::Engine.routes.url_helpers
26
+ end
27
+
18
28
 
19
29
  module BlogEngine
20
30
  class Engine < Rails::Engine
21
31
  isolate_namespace BlogEngine
22
32
  end
23
33
 
24
- Engine.routes.draw do
25
- resources :posts
26
- end
27
34
  end
28
35
 
29
36
 
@@ -34,6 +41,9 @@ end
34
41
 
35
42
  def draw_routes
36
43
 
44
+ BlogEngine::Engine.routes.draw do
45
+ resources :posts
46
+ end
37
47
  App.routes.draw do
38
48
  resources :inboxes do
39
49
  resources :messages do
@@ -63,6 +73,8 @@ def draw_routes
63
73
  get '/no_format' => "foo#foo", :format => false, :as => :no_format
64
74
 
65
75
  get '/json_only' => "foo#foo", :format => true, :constraints => {:format => /json/}, :as => :json_only
76
+
77
+ get '/привет' => "foo#foo", :as => :hello
66
78
  end
67
79
 
68
80
  end
@@ -81,4 +93,7 @@ RSpec.configure do |config|
81
93
  jscontext[:cgi] = CGI
82
94
  evaljs("function encodeURIComponent(string) {return cgi.escape(string);}")
83
95
  end
96
+ config.before(:all) do
97
+ draw_routes
98
+ end
84
99
  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.8.4
4
+ version: 0.8.5
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-07-10 00:00:00.000000000 Z
12
+ date: 2013-01-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -115,8 +115,10 @@ files:
115
115
  - lib/js_routes/engine.rb
116
116
  - lib/routes.js
117
117
  - lib/tasks/js_routes.rake
118
- - spec/js_routes_spec.rb
119
- - spec/post_rails_init_spec.rb
118
+ - spec/js_routes/generated_javascript_spec.rb
119
+ - spec/js_routes/options_spec.rb
120
+ - spec/js_routes/post_rails_init_spec.rb
121
+ - spec/js_routes/rails_routes_compatibility_spec.rb
120
122
  - spec/spec_helper.rb
121
123
  homepage: http://github.com/railsware/js-routes
122
124
  licenses:
@@ -133,7 +135,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
133
135
  version: '0'
134
136
  segments:
135
137
  - 0
136
- hash: 4117024943780104763
138
+ hash: 2174231102950156016
137
139
  required_rubygems_version: !ruby/object:Gem::Requirement
138
140
  none: false
139
141
  requirements:
@@ -1,366 +0,0 @@
1
- require 'spec_helper'
2
- require "fileutils"
3
-
4
-
5
- describe JsRoutes do
6
- before(:all) do
7
- draw_routes
8
- end
9
- before(:each) do
10
- evaljs(_presetup)
11
- evaljs(JsRoutes.generate(_options))
12
- end
13
-
14
- let(:_presetup) { "this;" }
15
- let(:_options) { {} }
16
-
17
- let(:routes) { App.routes.url_helpers }
18
- let(:blog_routes) { BlogEngine::Engine.routes.url_helpers }
19
-
20
- describe "compatibility with Rails" do
21
-
22
- it "should generate collection routing" do
23
- evaljs("Routes.inboxes_path()").should == routes.inboxes_path()
24
- end
25
-
26
- it "should generate member routing" do
27
- evaljs("Routes.inbox_path(1)").should == routes.inbox_path(1)
28
- end
29
-
30
- it "should generate nested routing with one parameter" do
31
- evaljs("Routes.inbox_messages_path(1)").should == routes.inbox_messages_path(1)
32
- end
33
-
34
- it "should generate nested routing" do
35
- evaljs("Routes.inbox_message_path(1,2)").should == routes.inbox_message_path(1, 2)
36
- end
37
-
38
- it "should generate routing with format" do
39
- evaljs("Routes.inbox_path(1, {format: 'json'})").should == routes.inbox_path(1, :format => "json")
40
- end
41
-
42
- it "should support simple get parameters" do
43
- evaljs("Routes.inbox_path(1, {format: 'json', lang: 'ua', q: 'hello'})").should == routes.inbox_path(1, :lang => "ua", :q => "hello", :format => "json")
44
- end
45
-
46
- it "should support array get parameters" do
47
- evaljs("Routes.inbox_path(1, {hello: ['world', 'mars']})").should == routes.inbox_path(1, :hello => [:world, :mars])
48
- end
49
-
50
- it "should support null and undefined parameters" do
51
- evaljs("Routes.inboxes_path({uri: null, key: undefined})").should == routes.inboxes_path(:uri => nil, :key => nil)
52
- end
53
-
54
- it "should escape get parameters" do
55
- evaljs("Routes.inboxes_path({uri: 'http://example.com'})").should == routes.inboxes_path(:uri => 'http://example.com')
56
- end
57
-
58
- it "should support routes with reserved javascript words as parameters" do
59
- evaljs("Routes.object_path(1, 2)").should == routes.object_path(1,2)
60
- end
61
-
62
- it "should support url anchor given as parameter" do
63
- evaljs("Routes.inbox_path(1, {anchor: 'hello'})").should == routes.inbox_path(1, :anchor => "hello")
64
- end
65
-
66
- it "should support engine routes" do
67
- evaljs("Routes.blog_app_posts_path()").should == blog_routes.posts_path()
68
- end
69
-
70
- it "should support engine routes with parameter" do
71
- evaljs("Routes.blog_app_post_path(1)").should == blog_routes.post_path(1)
72
- end
73
-
74
- it "shouldn't require the format" do
75
- evaljs("Routes.json_only_path({format: 'json'})").should == routes.json_only_path(:format => 'json')
76
- end
77
-
78
- context "routes globbing" do
79
- it "should be supported as parameters" do
80
- evaljs("Routes.book_path('thrillers', 1)").should == routes.book_path('thrillers', 1)
81
- end
82
-
83
- xit "should support routes globbing as hash" do
84
- evaljs("Routes.book_path(1, {section: 'thrillers'})").should == routes.book_path(1, :section => 'thrillers')
85
- end
86
-
87
- xit "should bee support routes globbing as hash" do
88
- evaljs("Routes.book_path(1)").should == routes.book_path(1)
89
- end
90
- end
91
-
92
- context "when jQuery is present" do
93
- before do
94
- evaljs("window.jQuery = {};")
95
- jscontext[:parameterize] = lambda {|object| _value.to_param}
96
- evaljs("window.jQuery.param = parameterize")
97
- end
98
-
99
- shared_examples_for "serialization" do
100
- it "should support serialization of objects" do
101
- evaljs("window.jQuery.param(#{_value.to_json})").should == _value.to_param
102
- evaljs("Routes.inboxes_path(#{_value.to_json})").should == routes.inboxes_path(_value)
103
- end
104
- end
105
- context "when parameters is a hash" do
106
- let(:_value) do
107
- {a: {b: 'c'}, q: [1,2]}
108
- end
109
- it_should_behave_like 'serialization'
110
- end
111
- context "when parameters is null" do
112
- let(:_value) do
113
- nil
114
- end
115
- it_should_behave_like 'serialization'
116
- end
117
- end
118
-
119
- context "using optional path fragments" do
120
- context "including not optional parts" do
121
- it "should include everything that is not optional" do
122
- evaljs("Routes.foo_path()").should == routes.foo_path
123
- end
124
- end
125
-
126
- context "but not including them" do
127
- it "should not include the optional parts" do
128
- evaljs("Routes.things_path()").should == routes.things_path
129
- end
130
-
131
- xit "should not require the optional parts as arguments" do
132
- #TODO: fix this inconsistence
133
- evaljs("Routes.thing_path(null, 5)").should == routes.thing_path(nil, 5)
134
- end
135
-
136
- it "should treat undefined as non-given optional part" do
137
- evaljs("Routes.thing_path(5, {optional_id: undefined})").should == routes.thing_path(5, :optional_id => nil)
138
- end
139
-
140
- it "should treat null as non-given optional part" do
141
- evaljs("Routes.thing_path(5, {optional_id: null})").should == routes.thing_path(5, :optional_id => nil)
142
- end
143
- end
144
-
145
- context "and including them" do
146
- it "should include the optional parts" do
147
- evaljs("Routes.things_path({optional_id: 5})").should == routes.things_path(:optional_id => 5)
148
- end
149
- end
150
- end
151
- end
152
-
153
- context "when wrong parameters given" do
154
-
155
- it "should throw Exception if not enough parameters" do
156
- lambda {
157
- evaljs("Routes.inbox_path()")
158
- }.should raise_error(V8::JSError)
159
- end
160
- it "should throw Exception if required parameter is not defined" do
161
- lambda {
162
- evaljs("Routes.inbox_path(null)")
163
- }.should raise_error(V8::JSError)
164
- end
165
-
166
- it "should throw Exceptions if when there is too many parameters" do
167
- lambda {
168
- evaljs("Routes.inbox_path(1,2)")
169
- }.should raise_error(V8::JSError)
170
- end
171
- end
172
-
173
- context "when exclude is specified" do
174
-
175
- let(:_options) { {:exclude => /^admin_/} }
176
-
177
- it "should exclude specified routes from file" do
178
- evaljs("Routes.admin_users_path").should be_nil
179
- end
180
-
181
- it "should not exclude routes not under specified pattern" do
182
- evaljs("Routes.inboxes_path()").should_not be_nil
183
- end
184
- end
185
- context "when include is specified" do
186
-
187
- let(:_options) { {:include => /^admin_/} }
188
-
189
- it "should exclude specified routes from file" do
190
- evaljs("Routes.admin_users_path()").should_not be_nil
191
- end
192
-
193
- it "should not exclude routes not under specified pattern" do
194
- evaljs("Routes.inboxes_path").should be_nil
195
- end
196
- end
197
-
198
- context "when prefix with trailing slash is specified" do
199
-
200
- let(:_options) { {:prefix => "/myprefix/" } }
201
-
202
- it "should render routing with prefix" do
203
- evaljs("Routes.inbox_path(1)").should == "/myprefix#{routes.inbox_path(1)}"
204
- end
205
-
206
- it "should render routing with prefix set in JavaScript" do
207
- evaljs("Routes.options.prefix = '/newprefix/'")
208
- evaljs("Routes.inbox_path(1)").should == "/newprefix#{routes.inbox_path(1)}"
209
- end
210
-
211
- end
212
-
213
- context "when prefix with http:// is specified" do
214
-
215
- let(:_options) { {:prefix => "http://localhost:3000" } }
216
-
217
- it "should render routing with prefix" do
218
- evaljs("Routes.inbox_path(1)").should == _options[:prefix] + routes.inbox_path(1)
219
- end
220
- end
221
-
222
- context "when prefix without trailing slash is specified" do
223
-
224
- let(:_options) { {:prefix => "/myprefix" } }
225
-
226
- it "should render routing with prefix" do
227
- evaljs("Routes.inbox_path(1)").should == "/myprefix#{routes.inbox_path(1)}"
228
- end
229
-
230
- it "should render routing with prefix set in JavaScript" do
231
- evaljs("Routes.options.prefix = '/newprefix'")
232
- evaljs("Routes.inbox_path(1)").should == "/newprefix#{routes.inbox_path(1)}"
233
- end
234
-
235
- end
236
-
237
- context "when javascript engine without Array#indexOf is used" do
238
- before(:each) do
239
- evaljs("Array.prototype.indexOf = null")
240
- end
241
- it "should still work correctly" do
242
- evaljs("Routes.inboxes_path()").should == routes.inboxes_path()
243
- end
244
- end
245
-
246
- context "when default_format is specified" do
247
- let(:_options) { {:default_format => "json"} }
248
-
249
- it "should render routing with default_format" do
250
- evaljs("Routes.inbox_path(1)").should == routes.inbox_path(1, :format => "json")
251
- end
252
-
253
- it "should override default_format when spefified implicitly" do
254
- evaljs("Routes.inbox_path(1, {format: 'xml'})").should == routes.inbox_path(1, :format => "xml")
255
- end
256
-
257
- it "should override nullify implicitly when specified implicitly" do
258
- evaljs("Routes.inbox_path(1, {format: null})").should == routes.inbox_path(1)
259
- end
260
-
261
- it "shouldn't include the format when {:format => false} is specified" do
262
- evaljs("Routes.no_format_path()").should == routes.no_format_path
263
- end
264
-
265
- it "shouldn't require the format" do
266
- evaljs("Routes.json_only_path()").should == routes.json_only_path(:format => 'json')
267
- end
268
- end
269
-
270
- describe "when namespace option is specified" do
271
- let(:_options) { {:namespace => "PHM"} }
272
- it "should use this namespace for routing" do
273
- evaljs("window.Routes").should be_nil
274
- evaljs("PHM.inbox_path").should_not be_nil
275
- end
276
- end
277
-
278
- describe "when nested namespace option is specified" do
279
- context "and defined on client" do
280
- let(:_presetup) { "window.PHM = {}" }
281
- let(:_options) { {:namespace => "PHM.Routes"} }
282
- it "should use this namespace for routing" do
283
- evaljs("PHM.Routes.inbox_path").should_not be_nil
284
- end
285
- end
286
-
287
- context "but undefined on client" do
288
- let(:_options) { {:namespace => "PHM.Routes"} }
289
- it "should initialize namespace" do
290
- evaljs("window.PHM.Routes.inbox_path").should_not be_nil
291
- end
292
- end
293
-
294
- context "and some parts are defined" do
295
- let(:_presetup) { "window.PHM = { Utils: {} };" }
296
- let(:_options) { {:namespace => "PHM.Routes"} }
297
- it "should not overwrite existing parts" do
298
- evaljs("window.PHM.Utils").should_not be_nil
299
- evaljs("window.PHM.Routes.inbox_path").should_not be_nil
300
- end
301
- end
302
- end
303
-
304
- context "when arguments are objects" do
305
-
306
- let(:inbox) {Struct.new(:id, :to_param).new(1,"my")}
307
-
308
- it "should use id property of the object in path" do
309
- evaljs("Routes.inbox_path({id: 1})").should == routes.inbox_path(1)
310
- end
311
-
312
- it "should prefer to_param property over id property" do
313
- evaljs("Routes.inbox_path({id: 1, to_param: 'my'})").should == routes.inbox_path(inbox)
314
- end
315
-
316
- it "should call to_param if it is a function" do
317
- evaljs("Routes.inbox_path({id: 1, to_param: function(){ return 'my';}})").should == routes.inbox_path(inbox)
318
- end
319
-
320
- it "should call id if it is a function" do
321
- evaljs("Routes.inbox_path({id: function() { return 1;}})").should == routes.inbox_path(1)
322
- end
323
-
324
- it "should support options argument" do
325
- evaljs(
326
- "Routes.inbox_message_path({id:1, to_param: 'my'}, {id:2}, {custom: true, format: 'json'})"
327
- ).should == routes.inbox_message_path(inbox, 2, :custom => true, :format => "json")
328
- end
329
- end
330
-
331
-
332
- describe "generated js" do
333
- subject { JsRoutes.generate }
334
- it "should have correct function without arguments signature" do
335
- should include("inboxes_path: function(options)")
336
- end
337
- it "should have correct function with arguments signature" do
338
- should include("inbox_message_path: function(_inbox_id, _id, options)")
339
- end
340
- it "should have correct function signature with Ruby 1.8.7 and unordered hash" do
341
- should include("inbox_message_attachment_path: function(_inbox_id, _message_id, _id, options)")
342
- end
343
-
344
- it "routes should be sorted in alphabetical order" do
345
- subject.index("book_path").should <= subject.index("inboxes_path")
346
- end
347
- end
348
-
349
- describe ".generate!" do
350
-
351
- let(:name) { "#{File.dirname(__FILE__)}/../routes.js" }
352
-
353
- before(:each) do
354
- FileUtils.rm_f(name)
355
- JsRoutes.generate!({:file => name})
356
- end
357
-
358
- it "should not generate file before initialization" do
359
- File.exists?(name).should be_false
360
- end
361
-
362
- after(:all) do
363
- FileUtils.rm_f(name)
364
- end
365
- end
366
- end