js-routes 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.3.1
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.3.0"
8
+ s.version = "0.3.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-08-04}
12
+ s.date = %q{2011-08-07}
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
@@ -6,6 +6,7 @@ module JsRoutes
6
6
  js = File.read(File.dirname(__FILE__) + "/routes.js")
7
7
  options[:namespace] ||= "Routes"
8
8
  js.gsub!("NAMESPACE", options[:namespace])
9
+ js.gsub!("DEFAULT_FORMAT", options[:default_format].to_s)
9
10
  js.gsub!("ROUTES", js_routes(options))
10
11
  end
11
12
 
@@ -27,37 +28,32 @@ module JsRoutes
27
28
  exclude = Array(options[:exclude])
28
29
 
29
30
  Rails.application.reload_routes!
30
- Rails.application.routes.named_routes.routes.map do |name, route|
31
- if exclude.find {|e| name.to_s =~ e}
32
- ""
31
+ Rails.application.routes.named_routes.routes.map do |_, route|
32
+ if exclude.find {|e| route.name =~ e}
33
+ nil
33
34
  else
34
- build_js(name, route, options)
35
+ build_js(route, options)
35
36
  end
36
- end.join("\n")
37
+ end.compact.join(",\n")
37
38
  end
38
39
 
39
- def build_js(name, route, options)
40
- _ = <<-JS
40
+ def build_js(route, options)
41
+ _ = <<-JS.strip!
41
42
  // #{route.name} => #{route.path}
42
- #{name.to_s}_path: function(#{build_params route}) {
43
+ #{route.name}_path: function(#{build_params route}) {
43
44
  var opts = options || {};
44
- var format = opts.format || '#{options[:default_format]}';
45
- delete opts.format;
45
+ var format = Utils.extract_format(opts);
46
46
  #{build_default_params route};
47
- return Routes.check_path('#{build_path route}' + format) + Routes.serialize(opts);
48
- },
47
+ return Utils.check_path('#{build_path route}' + format) + Utils.serialize(opts);
48
+ }
49
49
  JS
50
50
  end
51
51
 
52
52
 
53
53
  def build_params route
54
54
  route.conditions[:path_info].captures.map do |cap|
55
- if cap.is_a?(Rack::Mount::GeneratableRegexp::DynamicSegment)
56
- if cap.name.to_s == "format"
57
- nil
58
- else
59
- cap.name.to_s.gsub(':', '')
60
- end
55
+ if cap.is_a?(Rack::Mount::GeneratableRegexp::DynamicSegment) && !(cap.name.to_s == "format")
56
+ cap.name.to_s.gsub(':', '')
61
57
  end
62
58
  end.compact.<<("options").join(', ')
63
59
  end
@@ -66,7 +62,7 @@ JS
66
62
  route.conditions[:path_info].captures.map do |cap|
67
63
  if cap.is_a?(Rack::Mount::GeneratableRegexp::DynamicSegment)
68
64
  segg = cap.name.to_s.gsub(':', '')
69
- "#{segg} = Routes.check_parameter(#{segg});"
65
+ "#{segg} = Utils.check_parameter(#{segg});"
70
66
  end
71
67
  end.join("\n")
72
68
  end
data/lib/routes.js CHANGED
@@ -1,8 +1,8 @@
1
1
  (function(){
2
2
 
3
- var Routes = {
3
+ var Utils = {
4
4
 
5
- ROUTES
5
+ default_format: 'DEFAULT_FORMAT',
6
6
 
7
7
  serialize: function(obj){
8
8
  if (obj == null) {return '';}
@@ -26,9 +26,16 @@
26
26
 
27
27
  check_path: function(path) {
28
28
  return path.replace(/\.$/m, '');
29
- }
29
+ },
30
30
 
31
+ extract_format: function(options) {
32
+ var format = options.hasOwnProperty("format") ? options.format : Utils.default_format;
33
+ delete options.format;
34
+ return format || "";
35
+ }
31
36
  };
32
37
 
33
- window.NAMESPACE = Routes;
38
+ window.NAMESPACE = {
39
+ ROUTES
40
+ };
34
41
  })();
@@ -2,34 +2,35 @@ require 'spec_helper'
2
2
  require "fileutils"
3
3
 
4
4
 
5
-
6
5
  describe JsRoutes do
7
6
  before(:each) do
8
7
  Rails.application.stub!(:reload_routes!).and_return(true)
9
- evaljs("var window = {};")
8
+ evaljs("var window = this;")
9
+ evaljs(_presetup)
10
10
  evaljs(JsRoutes.generate(_options))
11
11
  end
12
12
 
13
+ let(:_presetup) { "this;" }
13
14
  let(:_options) { {} }
14
15
 
15
16
  it "should generate collection routing" do
16
- evaljs("window.Routes.inboxes_path()").should == "/inboxes"
17
+ evaljs("Routes.inboxes_path()").should == "/inboxes"
17
18
  end
18
19
 
19
20
  it "should generate member routing" do
20
- evaljs("window.Routes.inbox_path(1)").should == "/inboxes/1"
21
+ evaljs("Routes.inbox_path(1)").should == "/inboxes/1"
21
22
  end
22
23
 
23
24
  it "should generate nested routing" do
24
- evaljs("window.Routes.inbox_message_path(1,2)").should == "/inboxes/1/messages/2"
25
+ evaljs("Routes.inbox_message_path(1,2)").should == "/inboxes/1/messages/2"
25
26
  end
26
27
 
27
28
  it "should generate routing with format" do
28
- evaljs("window.Routes.inbox_path(1, {format: 'json'})").should == "/inboxes/1.json"
29
+ evaljs("Routes.inbox_path(1, {format: 'json'})").should == "/inboxes/1.json"
29
30
  end
30
31
 
31
32
  it "should support get parameters" do
32
- evaljs("window.Routes.inbox_path(1, {format: 'json', q: 'hello', lang: 'ua'})").should == "/inboxes/1.json?q=hello&lang=ua"
33
+ evaljs("Routes.inbox_path(1, {format: 'json', q: 'hello', lang: 'ua'})").should == "/inboxes/1.json?q=hello&lang=ua"
33
34
  end
34
35
 
35
36
  context "when exclude is specified" do
@@ -37,7 +38,11 @@ describe JsRoutes do
37
38
  let(:_options) { {:exclude => /^admin_/} }
38
39
 
39
40
  it "should exclude specified routes from file" do
40
- evaljs("window.Routes.admin_users_path").should be_nil
41
+ evaljs("Routes.admin_users_path").should be_nil
42
+ end
43
+
44
+ it "should not exclude routes not under specified pattern" do
45
+ evaljs("Routes.inboxes_path()").should_not be_nil
41
46
  end
42
47
  end
43
48
 
@@ -45,11 +50,15 @@ describe JsRoutes do
45
50
  let(:_options) { {:default_format => "json"} }
46
51
 
47
52
  it "should render routing with default_format" do
48
- evaljs("window.Routes.inbox_path(1)").should == "/inboxes/1.json"
53
+ evaljs("Routes.inbox_path(1)").should == "/inboxes/1.json"
54
+ end
55
+
56
+ it "should override default_format wehn spefified implicitly" do
57
+ evaljs("Routes.inbox_path(1, {format: 'xml'})").should == "/inboxes/1.xml"
49
58
  end
50
59
 
51
- it "should override default_format implicitly" do
52
- evaljs("window.Routes.inbox_path(1, {format: 'xml'})").should == "/inboxes/1.xml"
60
+ it "should override nullify implicitly when specified implicitly" do
61
+ evaljs("Routes.inbox_path(1, {format: null})").should == "/inboxes/1"
53
62
  end
54
63
 
55
64
  end
@@ -58,7 +67,15 @@ describe JsRoutes do
58
67
  let(:_options) { {:namespace => "PHM"} }
59
68
  it "should use this name space for routing" do
60
69
  evaljs("window.Routes").should be_nil
61
- evaljs("window.PHM.inbox_path").should_not be_nil
70
+ evaljs("PHM.inbox_path").should_not be_nil
71
+ end
72
+
73
+ end
74
+ context "when nested namspace option is specified" do
75
+ let(:_presetup) { "window.PHM = {}" }
76
+ let(:_options) { {:namespace => "PHM.Routes"} }
77
+ it "should use this name space for routing" do
78
+ evaljs("PHM.Routes.inbox_path").should_not be_nil
62
79
  end
63
80
 
64
81
  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: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 0
10
- version: 0.3.0
9
+ - 1
10
+ version: 0.3.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-08-04 00:00:00 +03:00
18
+ date: 2011-08-07 00:00:00 +03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency