js-routes 0.7.1 → 0.7.2

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.7.1
1
+ 0.7.2
@@ -1 +1 @@
1
- <%= JsRoutes.generate %>
1
+ <%= 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 = %q{js-routes}
8
- s.version = "0.7.1"
8
+ s.version = "0.7.2"
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-10-06}
12
+ s.date = %q{2011-11-05}
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
@@ -47,6 +47,16 @@ class JsRoutes
47
47
  def generate!(opts = {})
48
48
  new(opts).generate!
49
49
  end
50
+
51
+ # Under rails 3.1.1 and higher, perform a check to ensure that the
52
+ # full environment will be available during asset compilation.
53
+ # This is required to ensure routes are loaded.
54
+ def assert_usable_configuration!
55
+ if Rails.version > "3.1.0" && !Rails.application.config.assets.initialize_on_precompile
56
+ raise("Cannot precompile js-routes unless environment is initialized. Please set config.assets.initialize_on_precompile to true.")
57
+ end
58
+ true
59
+ end
50
60
  end
51
61
 
52
62
  #
data/lib/routes.js CHANGED
@@ -40,6 +40,12 @@
40
40
  return format ? "." + format : "";
41
41
  },
42
42
 
43
+ extract_anchor: function(options) {
44
+ var anchor = options.hasOwnProperty("anchor") ? options.anchor : null;
45
+ delete options.anchor;
46
+ return anchor ? "#" + anchor : "";
47
+ },
48
+
43
49
  extract_options: function(number_of_params, args) {
44
50
  if (args.length > number_of_params) {
45
51
  return typeof(args[args.length-1]) == "object" ? args.pop() : {};
@@ -64,7 +70,7 @@
64
70
  var result = Utils.get_prefix();
65
71
  var opts = Utils.extract_options(number_of_params, args);
66
72
  if (args.length > number_of_params) {
67
- throw new Error("Too many parameters provided for path")
73
+ throw new Error("Too many parameters provided for path");
68
74
  }
69
75
  var params_count = 0, optional_params_count = 0;
70
76
  for (var i=0; i < parts.length; i++) {
@@ -86,13 +92,14 @@
86
92
  if (Utils.specified(value)) {
87
93
  result += Utils.path_identifier(value);
88
94
  } else {
89
- throw new Error("Insufficient parameters to build path")
95
+ throw new Error("Insufficient parameters to build path");
90
96
  }
91
97
  }
92
98
  }
93
99
  }
94
100
  var format = Utils.extract_format(opts);
95
- return Utils.clean_path(result + format) + Utils.serialize(opts);
101
+ var anchor = Utils.extract_anchor(opts);
102
+ return Utils.clean_path(result + format + anchor) + Utils.serialize(opts);
96
103
  },
97
104
 
98
105
  specified: function(value) {
@@ -47,6 +47,10 @@ describe JsRoutes do
47
47
  it "should support routes with reserved javascript words as parameters" do
48
48
  evaljs("Routes.object_path(1, 2)").should == routes.object_path(1,2)
49
49
  end
50
+
51
+ it "should support url anchor given as parameter" do
52
+ evaljs("Routes.inbox_path(1, {anchor: 'hello'})").should == routes.inbox_path(1, :anchor => "hello")
53
+ end
50
54
  end
51
55
 
52
56
  context "when wrong parameters given" do
@@ -15,10 +15,10 @@ describe "after Rails initialization" do
15
15
  after(:all) do
16
16
  FileUtils.rm_f(name)
17
17
  end
18
-
18
+
19
19
  context '.generate!' do
20
20
  it "should generate routes file" do
21
- File.exists?(name).should be_true
21
+ File.exists?(name).should be_true
22
22
  end
23
23
  end
24
24
 
@@ -45,26 +45,62 @@ describe "after Rails initialization" do
45
45
  end
46
46
 
47
47
  context "the preprocessor" do
48
+ before(:each) do
49
+ ctx.should_receive(:depend_on).with(Rails.root.join('config','routes.rb'))
50
+ end
51
+ let!(:ctx) do
52
+ Sprockets::Context.new(Rails.application.assets,
53
+ 'js-routes.js',
54
+ Pathname.new('js-routes.js'))
55
+
56
+ end
48
57
  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')
58
+
59
+ #TODO: check why doesn't work with let callbacks
60
+ context 'with Rails 3.1.0' do
61
+ before(:each) do
62
+ Rails.stub!("version").and_return("3.1.0")
63
+ end
64
+
65
+ it "should render some javascript" do
66
+ ctx.evaluate('js-routes.js').should =~ /window\.Routes/
67
+ end
55
68
  end
56
- end
57
69
 
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')
70
+ context "with Rails 3.1.1" do
71
+ before(:each) do
72
+ Rails.stub!("version").and_return("3.1.1")
73
+ end
74
+ context "and initialize on precompile" do
75
+ before(:each) do
76
+ Rails.application.config.assets.initialize_on_precompile = true
77
+ end
78
+ it "should render some javascript" do
79
+ ctx.evaluate('js-routes.js').should =~ /window\.Routes/
80
+ end
81
+ end
82
+ context "and not initialize on precompile" do
83
+ before(:each) do
84
+ Rails.application.config.assets.initialize_on_precompile = false
85
+ end
86
+ it "should raise an exception" do
87
+ lambda { ctx.evaluate('js-routes.js') }.should raise_error(/Cannot precompile/)
88
+ end
89
+ end
90
+
65
91
  end
66
92
  end
67
93
 
94
+
95
+ end
96
+ context "when not dealing with js-routes.js" do
97
+ it "should not depend on routes.rb" do
98
+ ctx = Sprockets::Context.new(Rails.application.assets,
99
+ 'test.js',
100
+ test_asset_path)
101
+ ctx.should_not_receive(:depend_on)
102
+ ctx.evaluate('test.js')
103
+ end
68
104
  end
69
105
  end
70
106
  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: 1
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 7
9
- - 1
10
- version: 0.7.1
9
+ - 2
10
+ version: 0.7.2
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-10-06 00:00:00 +03:00
18
+ date: 2011-11-05 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency