js-routes 0.6.2 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,74 +1,82 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- abstract (1.0.0)
5
- actionmailer (3.0.9)
6
- actionpack (= 3.0.9)
7
- mail (~> 2.2.19)
8
- actionpack (3.0.9)
9
- activemodel (= 3.0.9)
10
- activesupport (= 3.0.9)
11
- builder (~> 2.1.2)
12
- erubis (~> 2.6.6)
13
- i18n (~> 0.5.0)
14
- rack (~> 1.2.1)
15
- rack-mount (~> 0.6.14)
16
- rack-test (~> 0.5.7)
17
- tzinfo (~> 0.3.23)
18
- activemodel (3.0.9)
19
- activesupport (= 3.0.9)
20
- builder (~> 2.1.2)
21
- i18n (~> 0.5.0)
22
- activerecord (3.0.9)
23
- activemodel (= 3.0.9)
24
- activesupport (= 3.0.9)
25
- arel (~> 2.0.10)
26
- tzinfo (~> 0.3.23)
27
- activeresource (3.0.9)
28
- activemodel (= 3.0.9)
29
- activesupport (= 3.0.9)
30
- activesupport (3.0.9)
31
- arel (2.0.10)
32
- builder (2.1.2)
4
+ actionmailer (3.1.0)
5
+ actionpack (= 3.1.0)
6
+ mail (~> 2.3.0)
7
+ actionpack (3.1.0)
8
+ activemodel (= 3.1.0)
9
+ activesupport (= 3.1.0)
10
+ builder (~> 3.0.0)
11
+ erubis (~> 2.7.0)
12
+ i18n (~> 0.6)
13
+ rack (~> 1.3.2)
14
+ rack-cache (~> 1.0.3)
15
+ rack-mount (~> 0.8.2)
16
+ rack-test (~> 0.6.1)
17
+ sprockets (~> 2.0.0)
18
+ activemodel (3.1.0)
19
+ activesupport (= 3.1.0)
20
+ bcrypt-ruby (~> 3.0.0)
21
+ builder (~> 3.0.0)
22
+ i18n (~> 0.6)
23
+ activerecord (3.1.0)
24
+ activemodel (= 3.1.0)
25
+ activesupport (= 3.1.0)
26
+ arel (~> 2.2.1)
27
+ tzinfo (~> 0.3.29)
28
+ activeresource (3.1.0)
29
+ activemodel (= 3.1.0)
30
+ activesupport (= 3.1.0)
31
+ activesupport (3.1.0)
32
+ multi_json (~> 1.0)
33
+ arel (2.2.1)
34
+ bcrypt-ruby (3.0.1)
35
+ builder (3.0.0)
33
36
  diff-lcs (1.1.2)
34
- erubis (2.6.6)
35
- abstract (>= 1.0.0)
37
+ erubis (2.7.0)
36
38
  git (1.2.5)
37
- i18n (0.5.0)
39
+ hike (1.2.1)
40
+ i18n (0.6.0)
38
41
  jeweler (1.6.4)
39
42
  bundler (~> 1.0)
40
43
  git (>= 1.2.5)
41
44
  rake
42
45
  libv8 (3.3.10.2)
43
- mail (2.2.19)
44
- activesupport (>= 2.3.6)
46
+ mail (2.3.0)
45
47
  i18n (>= 0.4.0)
46
48
  mime-types (~> 1.16)
47
49
  treetop (~> 1.4.8)
48
50
  mime-types (1.16)
51
+ multi_json (1.0.3)
49
52
  polyglot (0.3.2)
50
- rack (1.2.3)
51
- rack-mount (0.6.14)
53
+ rack (1.3.3)
54
+ rack-cache (1.0.3)
55
+ rack (>= 0.4)
56
+ rack-mount (0.8.3)
52
57
  rack (>= 1.0.0)
53
- rack-test (0.5.7)
58
+ rack-ssl (1.3.2)
59
+ rack
60
+ rack-test (0.6.1)
54
61
  rack (>= 1.0)
55
- rails (3.0.9)
56
- actionmailer (= 3.0.9)
57
- actionpack (= 3.0.9)
58
- activerecord (= 3.0.9)
59
- activeresource (= 3.0.9)
60
- activesupport (= 3.0.9)
62
+ rails (3.1.0)
63
+ actionmailer (= 3.1.0)
64
+ actionpack (= 3.1.0)
65
+ activerecord (= 3.1.0)
66
+ activeresource (= 3.1.0)
67
+ activesupport (= 3.1.0)
61
68
  bundler (~> 1.0)
62
- railties (= 3.0.9)
63
- railties (3.0.9)
64
- actionpack (= 3.0.9)
65
- activesupport (= 3.0.9)
69
+ railties (= 3.1.0)
70
+ railties (3.1.0)
71
+ actionpack (= 3.1.0)
72
+ activesupport (= 3.1.0)
73
+ rack-ssl (~> 1.3.2)
66
74
  rake (>= 0.8.7)
67
75
  rdoc (~> 3.4)
68
- thor (~> 0.14.4)
76
+ thor (~> 0.14.6)
69
77
  rake (0.9.2)
70
78
  rcov (0.9.9)
71
- rdoc (3.9.1)
79
+ rdoc (3.9.4)
72
80
  rspec (2.3.0)
73
81
  rspec-core (~> 2.3.0)
74
82
  rspec-expectations (~> 2.3.0)
@@ -77,9 +85,14 @@ GEM
77
85
  rspec-expectations (2.3.0)
78
86
  diff-lcs (~> 1.1.2)
79
87
  rspec-mocks (2.3.0)
88
+ sprockets (2.0.0)
89
+ hike (~> 1.2)
90
+ rack (~> 1.0)
91
+ tilt (~> 1.1, != 1.3.0)
80
92
  therubyracer (0.9.2)
81
93
  libv8 (~> 3.3.10)
82
94
  thor (0.14.6)
95
+ tilt (1.3.3)
83
96
  treetop (1.4.10)
84
97
  polyglot
85
98
  polyglot (>= 0.3.1)
data/Readme.md CHANGED
@@ -7,7 +7,7 @@ Generates javascript file that defines all Rails named routes as javascript help
7
7
  Your Rails Gemfile:
8
8
 
9
9
  ``` ruby
10
- gem "js-routes", :require => 'js_routes'
10
+ gem "js-routes"
11
11
  ```
12
12
 
13
13
  Your application initializer, like `config/initializers/jsroutes.rb`:
@@ -91,4 +91,6 @@ Advantages of this one are:
91
91
  * Support Rails `#to_param` convention for seo optimized paths
92
92
  * Well tested
93
93
 
94
+ #### Thanks to [Contributors](https://github.com/railsware/js-routes/contributors)
95
+
94
96
  #### Have fun
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.2
1
+ 0.7.1
@@ -0,0 +1 @@
1
+ <%= JsRoutes.generate %>
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.6.2"
8
+ s.version = "0.7.1"
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-09-21}
12
+ s.date = %q{2011-10-06}
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 = [
@@ -24,12 +24,15 @@ Gem::Specification.new do |s|
24
24
  "Rakefile",
25
25
  "Readme.md",
26
26
  "VERSION",
27
+ "app/assets/javascripts/js-routes.js.erb",
27
28
  "js-routes.gemspec",
29
+ "lib/js-routes.rb",
28
30
  "lib/js_routes.rb",
29
- "lib/jsroutes.rb",
31
+ "lib/js_routes/engine.rb",
30
32
  "lib/routes.js",
31
33
  "lib/tasks/js_routes.rake",
32
34
  "spec/js_routes_spec.rb",
35
+ "spec/post_rails_init_spec.rb",
33
36
  "spec/spec_helper.rb"
34
37
  ]
35
38
  s.homepage = %q{http://github.com/railsware/js-routes}
data/lib/js-routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'js_routes'
2
+ require 'js_routes/engine'
@@ -0,0 +1,15 @@
1
+ if defined?(Rails) && Rails.version >= "3.1"
2
+ class JsRoutes
3
+ class Engine < Rails::Engine
4
+ JS_ROUTES_ASSET = 'js-routes'
5
+
6
+ initializer 'js-routes.dependent_on_routes', :after => "sprockets.environment" do
7
+ routes = Rails.root.join('config','routes.rb')
8
+ Rails.application.assets.register_preprocessor 'application/javascript', :'js-routes_dependent_on_routes' do |ctx,data|
9
+ ctx.depend_on(routes) if ctx.logical_path == JS_ROUTES_ASSET
10
+ data
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
data/lib/js_routes.rb CHANGED
@@ -1,15 +1,52 @@
1
1
  class JsRoutes
2
2
 
3
3
  #
4
- # API
4
+ # OPTIONS
5
5
  #
6
6
 
7
- def self.generate(options = {})
8
- self.new(options).generate
7
+ DEFAULT_PATH = if Rails.version >= "3.1"
8
+ File.join('app','assets','javascripts','routes.js')
9
+ else
10
+ File.join('public','javascripts','routes.js')
11
+ end
12
+
13
+ DEFAULTS = {
14
+ :namespace => "Routes",
15
+ :default_format => "",
16
+ :exclude => [],
17
+ :include => //,
18
+ :file => DEFAULT_PATH,
19
+ :prefix => ""
20
+ }
21
+
22
+ class Options < Struct.new(*DEFAULTS.keys)
23
+ def to_hash
24
+ Hash[*members.zip(values).flatten(1)].symbolize_keys
25
+ end
9
26
  end
10
27
 
11
- def self.generate!(options = {})
12
- self.new(options).generate!
28
+ #
29
+ # API
30
+ #
31
+
32
+ class << self
33
+ def setup(&block)
34
+ options.tap(&block) if block
35
+ end
36
+
37
+ def options
38
+ @options ||= Options.new.tap do |opts|
39
+ DEFAULTS.each_pair {|k,v| opts[k] = v}
40
+ end
41
+ end
42
+
43
+ def generate(opts = {})
44
+ new(opts).generate
45
+ end
46
+
47
+ def generate!(opts = {})
48
+ new(opts).generate!
49
+ end
13
50
  end
14
51
 
15
52
  #
@@ -17,7 +54,7 @@ class JsRoutes
17
54
  #
18
55
 
19
56
  def initialize(options = {})
20
- @options = default_options.merge(options)
57
+ @options = self.class.options.to_hash.merge(options)
21
58
  end
22
59
 
23
60
  def generate
@@ -29,11 +66,11 @@ class JsRoutes
29
66
  end
30
67
 
31
68
  def generate!
32
- # Some libraries like devise do yet load their routes so we will wait
69
+ # Some libraries like Devise do not yet loaded their routes so we will wait
33
70
  # until initialization process finish
34
71
  # https://github.com/railsware/js-routes/issues/7
35
72
  Rails.configuration.after_initialize do
36
- File.open(@options[:file], 'w') do |f|
73
+ File.open(Rails.root.join(@options[:file]), 'w') do |f|
37
74
  f.write generate
38
75
  end
39
76
  end
@@ -63,20 +100,39 @@ class JsRoutes
63
100
  params = build_params route
64
101
  _ = <<-JS.strip!
65
102
  // #{route.name} => #{route.path}
66
- #{route.name}_path: function(#{params.<<("options").join(", ")}) {
67
- return Utils.build_path(#{params.size}, #{path_parts(route).inspect}, arguments)
103
+ #{route.name}_path: function(#{(params + ["options"]).join(", ")}) {
104
+ return Utils.build_path(#{params.size}, #{path_parts(route).inspect}, #{optional_params(route).inspect}, arguments)
68
105
  }
69
106
  JS
70
107
  end
71
108
 
109
+ # TODO: might be possible to simplify this to use route.path
110
+ # instead of all this path_info.source madness
111
+ def optional_params(route)
112
+ if RUBY_VERSION >= '1.9.2'
113
+ optional_named_captures_regexp = /\?\:.+?\(\?\<(.+?)\>/
114
+ path_info = route.conditions[:path_info]
115
+ path_info.source.scan(optional_named_captures_regexp).flatten
116
+ else
117
+ re = Regexp.escape("([^/.?]+)")
118
+ optional_named_captures_regexp = /#{re}|\(\?\:.+?\)\?/
119
+ path_info = route.conditions[:path_info]
120
+ captures = path_info.source.scan(optional_named_captures_regexp).flatten
121
+ named_captures = path_info.named_captures.to_a.sort {|a,b|a.last.first<=>b.last.first}
122
+ captures.zip(named_captures).map do |type, (name, pos)|
123
+ name unless type == '([^/.?]+)'
124
+ end.compact
125
+ end
126
+ end
72
127
 
73
128
  def build_params route
129
+ optional_named_captures = optional_params(route)
74
130
  route.conditions[:path_info].named_captures.to_a.sort do |cap1, cap2|
75
131
  # Hash is not ordered in Ruby 1.8.7
76
132
  cap1.last.first <=> cap2.last.first
77
133
  end.map do |cap|
78
134
  name = cap.first
79
- if !(name.to_s == "format")
135
+ if !(optional_named_captures.include?(name.to_s))
80
136
  # prepending each parameter name with underscore
81
137
  # to prevent conflict with JS reserved words
82
138
  "_" + name.to_s.gsub(/^:/, '')
@@ -88,24 +144,4 @@ class JsRoutes
88
144
  def path_parts route
89
145
  route.path.gsub(/\(\.:format\)$/, "").split(/:[a-z\-_]+/)
90
146
  end
91
-
92
- def default_options
93
- {
94
- :namespace => "Routes",
95
- :default_format => "",
96
- :exclude => [],
97
- :include => //,
98
- :file => default_file,
99
- :prefix => ""
100
- }
101
- end
102
-
103
- def default_file
104
- if Rails.version >= "3.1"
105
- "#{Rails.root}/app/assets/javascripts/routes.js"
106
- else
107
- "#{Rails.root}/public/javascripts/routes.js"
108
- end
109
- end
110
-
111
147
  end
data/lib/routes.js CHANGED
@@ -1,12 +1,17 @@
1
1
  (function(){
2
2
 
3
+ var defaults = {
4
+ prefix: 'PREFIX',
5
+ format: 'DEFAULT_FORMAT'
6
+ };
7
+
3
8
  var Utils = {
4
9
 
5
10
  serialize: function(obj){
6
11
  if (obj === null) {return '';}
7
12
  var s = [];
8
13
  for (prop in obj){
9
- s.push(prop + "=" + obj[prop]);
14
+ s.push(prop + "=" + encodeURIComponent(obj[prop].toString()));
10
15
  }
11
16
  if (s.length === 0) {
12
17
  return '';
@@ -18,14 +23,25 @@
18
23
  return path.replace(/\/+/g, "/").replace(/[\)\(]/g, "").replace(/\.$/m, '').replace(/\/$/m, '');
19
24
  },
20
25
 
26
+ extract: function(name, options) {
27
+ var o = undefined;
28
+ if (options.hasOwnProperty(name)) {
29
+ o = options[name];
30
+ delete options[name];
31
+ } else if (defaults.hasOwnProperty(name)) {
32
+ o = defaults[name];
33
+ }
34
+ return o;
35
+ },
36
+
21
37
  extract_format: function(options) {
22
- var format = options.hasOwnProperty("format") ? options.format : window.NAMESPACE.options.default_format;
38
+ var format = options.hasOwnProperty("format") ? options.format : defaults.format;
23
39
  delete options.format;
24
40
  return format ? "." + format : "";
25
41
  },
26
42
 
27
43
  extract_options: function(number_of_params, args) {
28
- if (args.length >= number_of_params) {
44
+ if (args.length > number_of_params) {
29
45
  return typeof(args[args.length-1]) == "object" ? args.pop() : {};
30
46
  } else {
31
47
  return {};
@@ -37,26 +53,42 @@
37
53
  return "";
38
54
  }
39
55
  if (typeof(object) == "object") {
40
- return (object.to_param || object.id).toString();
56
+ return (object.to_param || object.id || object).toString();
41
57
  } else {
42
58
  return object.toString();
43
59
  }
44
60
  },
45
61
 
46
- build_path: function(number_of_params, parts, args) {
62
+ build_path: function(number_of_params, parts, optional_params, args) {
47
63
  args = Array.prototype.slice.call(args);
48
- result = Utils.get_prefix();
64
+ var result = Utils.get_prefix();
49
65
  var opts = Utils.extract_options(number_of_params, args);
66
+ if (args.length > number_of_params) {
67
+ throw new Error("Too many parameters provided for path")
68
+ }
69
+ var params_count = 0, optional_params_count = 0;
50
70
  for (var i=0; i < parts.length; i++) {
51
- value = args.shift();
52
- part = parts[i];
53
- if (Utils.specified(value)) {
54
- result += part;
55
- result += Utils.path_identifier(value);
56
- } else if (!Utils.optional_part(part)) {
57
- //TODO: make it strict
58
- //throw new Error("Can not build path: required parameter is null or undefined.");
71
+ var part = parts[i];
72
+ if (Utils.optional_part(part)) {
73
+ var name = optional_params[optional_params_count];
74
+ optional_params_count++;
75
+ // try and find the option in opts
76
+ var optional = Utils.extract(name, opts);
77
+ if (Utils.specified(optional)) {
78
+ result += part;
79
+ result += Utils.path_identifier(optional);
80
+ }
81
+ } else {
59
82
  result += part;
83
+ if (params_count < number_of_params) {
84
+ params_count++;
85
+ var value = args.shift();
86
+ if (Utils.specified(value)) {
87
+ result += Utils.path_identifier(value);
88
+ } else {
89
+ throw new Error("Insufficient parameters to build path")
90
+ }
91
+ }
60
92
  }
61
93
  }
62
94
  var format = Utils.extract_format(opts);
@@ -72,7 +104,7 @@
72
104
  },
73
105
 
74
106
  get_prefix: function(){
75
- var prefix = window.NAMESPACE.options.prefix;
107
+ var prefix = defaults.prefix;
76
108
 
77
109
  if( prefix !== "" ){
78
110
  prefix = prefix.match('\/$') ? prefix : ( prefix + '/');
@@ -84,11 +116,5 @@
84
116
  };
85
117
 
86
118
  window.NAMESPACE = ROUTES;
87
-
88
- window.NAMESPACE.options = {
89
- prefix: 'PREFIX',
90
- default_format: 'DEFAULT_FORMAT',
91
- };
92
-
93
-
119
+ window.NAMESPACE.options = defaults;
94
120
  })();
@@ -5,7 +5,6 @@ require "fileutils"
5
5
  describe JsRoutes do
6
6
  before(:each) do
7
7
  Rails.application.stub!(:reload_routes!).and_return(true)
8
- evaljs("var window = this;")
9
8
  evaljs(_presetup)
10
9
  evaljs(JsRoutes.generate(_options))
11
10
  end
@@ -13,32 +12,61 @@ describe JsRoutes do
13
12
  let(:_presetup) { "this;" }
14
13
  let(:_options) { {} }
15
14
 
16
- it "should generate collection routing" do
17
- evaljs("Routes.inboxes_path()").should == "/inboxes"
18
- end
15
+ let(:routes) { App.routes.url_helpers }
19
16
 
20
- it "should generate member routing" do
21
- evaljs("Routes.inbox_path(1)").should == "/inboxes/1"
22
- end
17
+ describe "compatibility with Rails routes" do
23
18
 
24
- it "should generate nested routing with one parameter" do
25
- evaljs("Routes.inbox_message_path(1)").should == "/inboxes/1/messages"
26
- end
19
+ it "should generate collection routing" do
20
+ evaljs("Routes.inboxes_path()").should == routes.inboxes_path()
21
+ end
27
22
 
28
- it "should generate nested routing" do
29
- evaljs("Routes.inbox_message_path(1,2)").should == "/inboxes/1/messages/2"
30
- end
23
+ it "should generate member routing" do
24
+ evaljs("Routes.inbox_path(1)").should == routes.inbox_path(1)
25
+ end
31
26
 
32
- it "should generate routing with format" do
33
- evaljs("Routes.inbox_path(1, {format: 'json'})").should == "/inboxes/1.json"
34
- end
27
+ it "should generate nested routing with one parameter" do
28
+ evaljs("Routes.inbox_messages_path(1)").should == routes.inbox_messages_path(1)
29
+ end
30
+
31
+ it "should generate nested routing" do
32
+ evaljs("Routes.inbox_message_path(1,2)").should == routes.inbox_message_path(1, 2)
33
+ end
34
+
35
+ it "should generate routing with format" do
36
+ evaljs("Routes.inbox_path(1, {format: 'json'})").should == routes.inbox_path(1, :format => "json")
37
+ end
38
+
39
+ it "should support get parameters" do
40
+ evaljs("Routes.inbox_path(1, {format: 'json', lang: 'ua', q: 'hello'})").should == routes.inbox_path(1, :lang => "ua", :q => "hello", :format => "json")
41
+ end
42
+
43
+ it "should escape get parameters" do
44
+ evaljs("Routes.inboxes_path({uri: 'http://example.com'})").should == routes.inboxes_path(:uri => 'http://example.com')
45
+ end
35
46
 
36
- it "should support get parameters" do
37
- evaljs("Routes.inbox_path(1, {format: 'json', q: 'hello', lang: 'ua'})").should == "/inboxes/1.json?q=hello&lang=ua"
47
+ it "should support routes with reserved javascript words as parameters" do
48
+ evaljs("Routes.object_path(1, 2)").should == routes.object_path(1,2)
49
+ end
38
50
  end
39
51
 
40
- it "should support routes with reserved javascript words as parameters" do
41
- evaljs("Routes.object_path(1, 2)").should == "/returns/1/objects/2"
52
+ context "when wrong parameters given" do
53
+
54
+ it "should throw Exception if not enough parameters" do
55
+ lambda {
56
+ evaljs("Routes.inbox_path()")
57
+ }.should raise_error(V8::JSError)
58
+ end
59
+ it "should throw Exception if required parameter is not defined" do
60
+ lambda {
61
+ evaljs("Routes.inbox_path(null)")
62
+ }.should raise_error(V8::JSError)
63
+ end
64
+
65
+ it "should throw Exceptions if when there is too many parameters" do
66
+ lambda {
67
+ evaljs("Routes.inbox_path(1,2)")
68
+ }.should raise_error(V8::JSError)
69
+ end
42
70
  end
43
71
 
44
72
  context "when exclude is specified" do
@@ -71,12 +99,12 @@ describe JsRoutes do
71
99
  let(:_options) { {:prefix => "/myprefix/" } }
72
100
 
73
101
  it "should render routing with prefix" do
74
- evaljs("Routes.inbox_path(1)").should == "/myprefix/inboxes/1"
102
+ evaljs("Routes.inbox_path(1)").should == "/myprefix#{routes.inbox_path(1)}"
75
103
  end
76
104
 
77
105
  it "should render routing with prefix set in JavaScript" do
78
106
  evaljs("Routes.options.prefix = '/newprefix/'")
79
- evaljs("Routes.inbox_path(1)").should == "/newprefix/inboxes/1"
107
+ evaljs("Routes.inbox_path(1)").should == "/newprefix#{routes.inbox_path(1)}"
80
108
  end
81
109
 
82
110
  end
@@ -86,12 +114,12 @@ describe JsRoutes do
86
114
  let(:_options) { {:prefix => "/myprefix" } }
87
115
 
88
116
  it "should render routing with prefix" do
89
- evaljs("Routes.inbox_path(1)").should == "/myprefix/inboxes/1"
117
+ evaljs("Routes.inbox_path(1)").should == "/myprefix#{routes.inbox_path(1)}"
90
118
  end
91
119
 
92
120
  it "should render routing with prefix set in JavaScript" do
93
121
  evaljs("Routes.options.prefix = '/newprefix'")
94
- evaljs("Routes.inbox_path(1)").should == "/newprefix/inboxes/1"
122
+ evaljs("Routes.inbox_path(1)").should == "/newprefix#{routes.inbox_path(1)}"
95
123
  end
96
124
 
97
125
  end
@@ -100,15 +128,15 @@ describe JsRoutes do
100
128
  let(:_options) { {:default_format => "json"} }
101
129
 
102
130
  it "should render routing with default_format" do
103
- evaljs("Routes.inbox_path(1)").should == "/inboxes/1.json"
131
+ evaljs("Routes.inbox_path(1)").should == routes.inbox_path(1, :format => "json")
104
132
  end
105
133
 
106
134
  it "should override default_format when spefified implicitly" do
107
- evaljs("Routes.inbox_path(1, {format: 'xml'})").should == "/inboxes/1.xml"
135
+ evaljs("Routes.inbox_path(1, {format: 'xml'})").should == routes.inbox_path(1, :format => "xml")
108
136
  end
109
137
 
110
138
  it "should override nullify implicitly when specified implicitly" do
111
- evaljs("Routes.inbox_path(1, {format: null})").should == "/inboxes/1"
139
+ evaljs("Routes.inbox_path(1, {format: null})").should == routes.inbox_path(1)
112
140
  end
113
141
 
114
142
  end
@@ -130,37 +158,46 @@ describe JsRoutes do
130
158
  end
131
159
 
132
160
  context "when arguments are objects" do
161
+
162
+ let(:inbox) {Struct.new(:id, :to_param).new(1,"my")}
163
+
133
164
  it "should use id property of the object in path" do
134
- evaljs("Routes.inbox_path({id: 1})").should == "/inboxes/1"
165
+ evaljs("Routes.inbox_path({id: 1})").should == routes.inbox_path(1)
135
166
  end
136
167
 
137
168
  it "should prefer to_param property over id property" do
138
- evaljs("Routes.inbox_path({id: 1, to_param: 'my'})").should == "/inboxes/my"
169
+ evaljs("Routes.inbox_path({id: 1, to_param: 'my'})").should == routes.inbox_path(inbox)
139
170
  end
140
171
 
141
172
  it "should support options argument" do
142
- evaljs("Routes.inbox_message_path({id:1, to_param: 'my'}, {id:2}, {custom: true, format: 'json'})").should == "/inboxes/my/messages/2.json?custom=true"
173
+ evaljs(
174
+ "Routes.inbox_message_path({id:1, to_param: 'my'}, {id:2}, {custom: true, format: 'json'})"
175
+ ).should == routes.inbox_message_path(inbox, 2, :custom => true, :format => "json")
143
176
  end
144
177
  end
145
178
 
146
179
  context "using optional path fragments" do
147
180
  context "but not including them" do
148
181
  it "should not include the optional parts" do
149
- evaljs("Routes.things_path()").should == "/things"
182
+ evaljs("Routes.things_path()").should == routes.things_path
183
+ end
184
+
185
+ it "should not require the optional parts as arguments" do
186
+ evaljs("Routes.thing_path(5)").should == routes.thing_path(nil, 5)
150
187
  end
151
188
 
152
189
  it "should treat undefined as non-given optional part" do
153
- evaljs("Routes.thing_path(undefined, 5)").should == "/things/5"
190
+ evaljs("Routes.thing_path(5, {optional_id: undefined})").should == routes.thing_path(5, :optional_id => nil)
154
191
  end
155
192
 
156
193
  it "should treat null as non-given optional part" do
157
- evaljs("Routes.thing_path(null, 5)").should == "/things/5"
194
+ evaljs("Routes.thing_path(5, {optional_id: null})").should == routes.thing_path(5, :optional_id => nil)
158
195
  end
159
196
  end
160
197
 
161
198
  context "and including them" do
162
199
  it "should include the optional parts" do
163
- evaljs("Routes.things_path(5)").should == "/optional/5/things"
200
+ evaljs("Routes.things_path({optional_id: 5})").should == routes.things_path(:optional_id => 5)
164
201
  end
165
202
  end
166
203
  end
@@ -183,29 +220,16 @@ describe JsRoutes do
183
220
  let(:name) { "#{File.dirname(__FILE__)}/../routes.js" }
184
221
 
185
222
  before(:each) do
186
- # prevent warning
187
- Rails.configuration.active_support.deprecation = :log
188
-
189
223
  FileUtils.rm_f(name)
190
224
  JsRoutes.generate!({:file => name})
191
225
  end
192
226
 
193
- it "should not generate file at once" do
227
+ it "should not generate file before initialization" do
194
228
  File.exists?(name).should be_false
195
229
  end
196
230
 
197
- context "after Rails initialization" do
198
- before(:each) do
199
- Rails.application.initialize!
200
- end
201
- it "should generate routes file only after rails initialization" do
202
- File.exists?(name).should be_true
203
- end
204
- end
205
-
206
231
  after(:all) do
207
232
  FileUtils.rm_f(name)
208
233
  end
209
234
  end
210
-
211
235
  end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+ require "fileutils"
3
+
4
+ describe "after Rails initialization" do
5
+ let(:name) { "#{File.dirname(__FILE__)}/../routes.js" }
6
+
7
+ before(:all) do
8
+
9
+ FileUtils.rm_f(name)
10
+ JsRoutes.generate!({:file => name})
11
+
12
+ Rails.application.initialize!
13
+ end
14
+
15
+ after(:all) do
16
+ FileUtils.rm_f(name)
17
+ end
18
+
19
+ context '.generate!' do
20
+ it "should generate routes file" do
21
+ File.exists?(name).should be_true
22
+ end
23
+ end
24
+
25
+ if Rails.version >= "3.1"
26
+ context "JsRoutes::Engine" do
27
+
28
+ let(:test_asset_path) {
29
+ Rails.root.join('app','assets','javascripts','test.js')
30
+ }
31
+
32
+ before(:all) do
33
+ File.open(test_asset_path,'w') do |f|
34
+ f.puts "function() {}"
35
+ end
36
+ end
37
+ after(:all) do
38
+ FileUtils.rm_f(test_asset_path)
39
+ end
40
+
41
+ it "should have registered a preprocessor" do
42
+ pps = Rails.application.assets.preprocessors
43
+ js_pps = pps['application/javascript']
44
+ js_pps.map(&:name).should include('Sprockets::Processor (js-routes_dependent_on_routes)')
45
+ end
46
+
47
+ context "the preprocessor" do
48
+ context "when dealing with js-routes.js" do
49
+ it "should depend on routes.rb" do
50
+ ctx = Sprockets::Context.new(Rails.application.assets,
51
+ 'js-routes.js',
52
+ Pathname.new('js-routes.js'))
53
+ ctx.should_receive(:depend_on).with(Rails.root.join('config','routes.rb'))
54
+ ctx.evaluate('js-routes.js')
55
+ end
56
+ end
57
+
58
+ context "when not dealing with js-routes.js" do
59
+ it "should not depend on routes.rb" do
60
+ ctx = Sprockets::Context.new(Rails.application.assets,
61
+ 'test.js',
62
+ test_asset_path)
63
+ ctx.should_not_receive(:depend_on)
64
+ ctx.evaluate('test.js')
65
+ end
66
+ end
67
+
68
+ end
69
+ end
70
+ end
71
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,18 +1,30 @@
1
1
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
2
  $LOAD_PATH.unshift(File.dirname(__FILE__))
3
3
  require 'rspec'
4
- require 'jsroutes'
5
- require 'rails'
4
+ require 'rails/all'
5
+ require 'js-routes'
6
6
  require "v8"
7
+ require "cgi"
7
8
  require "active_support/core_ext/hash/slice"
8
9
 
9
- def evaljs(string)
10
+ def jscontext
10
11
  @context ||= V8::Context.new
11
- @context.eval(string)
12
12
  end
13
13
 
14
+ def evaljs(string)
15
+ jscontext.eval(string)
16
+ end
17
+
18
+
19
+
20
+
14
21
 
15
22
  class App < Rails::Application
23
+ if Rails.version >= '3.1'
24
+ # Enable the asset pipeline
25
+ config.assets.enabled = true
26
+ end
27
+
16
28
  self.routes.draw do
17
29
  resources :inboxes do
18
30
  resources :messages do
@@ -33,8 +45,11 @@ class App < Rails::Application
33
45
  resources :things
34
46
  end
35
47
  end
48
+
36
49
  end
37
50
 
51
+ # prevent warning
52
+ Rails.configuration.active_support.deprecation = :log
38
53
 
39
54
  # Requires supporting files with custom matchers and macros, etc,
40
55
  # in ./support/ and its subdirectories.
@@ -42,4 +57,9 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
42
57
 
43
58
  RSpec.configure do |config|
44
59
 
60
+ config.before(:each) do
61
+ evaljs("var window = this;")
62
+ jscontext[:cgi] = CGI
63
+ evaljs("function encodeURIComponent(string) {return cgi.escape(string);}")
64
+ end
45
65
  end
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: 3
4
+ hash: 1
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 6
9
- - 2
10
- version: 0.6.2
8
+ - 7
9
+ - 1
10
+ version: 0.7.1
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-09-21 00:00:00 +03:00
18
+ date: 2011-10-06 00:00:00 +03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -126,12 +126,15 @@ files:
126
126
  - Rakefile
127
127
  - Readme.md
128
128
  - VERSION
129
+ - app/assets/javascripts/js-routes.js.erb
129
130
  - js-routes.gemspec
131
+ - lib/js-routes.rb
130
132
  - lib/js_routes.rb
131
- - lib/jsroutes.rb
133
+ - lib/js_routes/engine.rb
132
134
  - lib/routes.js
133
135
  - lib/tasks/js_routes.rake
134
136
  - spec/js_routes_spec.rb
137
+ - spec/post_rails_init_spec.rb
135
138
  - spec/spec_helper.rb
136
139
  has_rdoc: true
137
140
  homepage: http://github.com/railsware/js-routes
data/lib/jsroutes.rb DELETED
@@ -1 +0,0 @@
1
- require "js_routes"