js-routes 0.2.0 → 0.3.0

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/Readme.md CHANGED
@@ -14,11 +14,29 @@ Your application initializer, like `config/initializers/jsroutes.rb`:
14
14
 
15
15
  ``` ruby
16
16
  JsRoutes.generate!(
17
- :file => "#{Rails.root}app/assets/javascripts/routes.js", # This is default
18
- :default_format => "json" # Default is blank
17
+ :file => "#{Rails.root}app/assets/javascripts/routes.js",
18
+ :default_format => "json",
19
+ :exclude => [/^admin_/, /paypal/, /^api_/]
19
20
  )
20
21
  ```
21
22
 
23
+ Available options:
24
+
25
+ * `:file` - the file to generate the routes. Default:
26
+ * `#{Rails.root}/app/assets/javascripts/routes.js` for Rails >= 3.1
27
+ * `#{Rails.root}/public/javascripts/routes.js` for Rails < 3.1
28
+ * `:default_format` - Format to append to urls. Default: blank
29
+ * `:exclude` - Array of regexps to exclude form js routes. Default: []
30
+ * Note that regexp applied to **named route** not to *URL*
31
+ * `:namespace` - global object used to access routes. Default: `Routes`
32
+ * Possible variants: `MyProject.routes`, `MyProjectRoutes`
33
+
34
+ In order to generate routes to string and manipulate them yourself:
35
+
36
+ ``` ruby
37
+ JsRoutes.generate(options)
38
+ ```
39
+
22
40
  ### Usage
23
41
 
24
42
  This will create a nice javascript file with `Routes` object that has all the rails routes available:
@@ -26,7 +44,7 @@ This will create a nice javascript file with `Routes` object that has all the ra
26
44
  ``` js
27
45
  Routes.users_path() // => "/users"
28
46
  Routes.user_path(1, {format: 'json'}) // => "/users/1.json"
29
- Routes.urer_project_path(1,2, {q: 'hello', custom: true}) // => "/users/1/projects/2?q=hello&custom=true"
47
+ Routes.user_project_path(1,2, {q: 'hello', custom: true}) // => "/users/1/projects/2?q=hello&custom=true"
30
48
  ```
31
49
 
32
50
  In order to make routes helpers available globally:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
@@ -4,8 +4,8 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{jsroutes}
8
- s.version = "0.2.0"
7
+ s.name = %q{js-routes}
8
+ s.version = "0.3.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"]
@@ -24,7 +24,7 @@ Gem::Specification.new do |s|
24
24
  "Rakefile",
25
25
  "Readme.md",
26
26
  "VERSION",
27
- "jsroutes.gemspec",
27
+ "js-routes.gemspec",
28
28
  "lib/js_routes.rb",
29
29
  "lib/jsroutes.rb",
30
30
  "lib/routes.js",
@@ -32,7 +32,7 @@ Gem::Specification.new do |s|
32
32
  "spec/js_routes_spec.rb",
33
33
  "spec/spec_helper.rb"
34
34
  ]
35
- s.homepage = %q{http://github.com/bogdan/jsroutes}
35
+ s.homepage = %q{http://github.com/railsware/js-routes}
36
36
  s.licenses = ["MIT"]
37
37
  s.require_paths = ["lib"]
38
38
  s.rubygems_version = %q{1.3.7}
@@ -43,14 +43,14 @@ Gem::Specification.new do |s|
43
43
  s.specification_version = 3
44
44
 
45
45
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
- s.add_runtime_dependency(%q<rails>, [">= 0"])
46
+ s.add_runtime_dependency(%q<rails>, [">= 3.0"])
47
47
  s.add_development_dependency(%q<therubyracer>, [">= 0"])
48
48
  s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
49
49
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
50
50
  s.add_development_dependency(%q<jeweler>, ["~> 1.6.2"])
51
51
  s.add_development_dependency(%q<rcov>, [">= 0"])
52
52
  else
53
- s.add_dependency(%q<rails>, [">= 0"])
53
+ s.add_dependency(%q<rails>, [">= 3.0"])
54
54
  s.add_dependency(%q<therubyracer>, [">= 0"])
55
55
  s.add_dependency(%q<rspec>, ["~> 2.3.0"])
56
56
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
@@ -58,7 +58,7 @@ Gem::Specification.new do |s|
58
58
  s.add_dependency(%q<rcov>, [">= 0"])
59
59
  end
60
60
  else
61
- s.add_dependency(%q<rails>, [">= 0"])
61
+ s.add_dependency(%q<rails>, [">= 3.0"])
62
62
  s.add_dependency(%q<therubyracer>, [">= 0"])
63
63
  s.add_dependency(%q<rspec>, ["~> 2.3.0"])
64
64
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
data/lib/js_routes.rb CHANGED
@@ -4,7 +4,9 @@ module JsRoutes
4
4
 
5
5
  def generate(options = {})
6
6
  js = File.read(File.dirname(__FILE__) + "/routes.js")
7
- js.gsub!("IDENTITY", js_routes(options))
7
+ options[:namespace] ||= "Routes"
8
+ js.gsub!("NAMESPACE", options[:namespace])
9
+ js.gsub!("ROUTES", js_routes(options))
8
10
  end
9
11
 
10
12
  def generate!(options = {})
@@ -22,10 +24,20 @@ module JsRoutes
22
24
  def js_routes(options = {})
23
25
 
24
26
  options[:default_format] ||= ""
27
+ exclude = Array(options[:exclude])
25
28
 
26
29
  Rails.application.reload_routes!
27
30
  Rails.application.routes.named_routes.routes.map do |name, route|
28
- <<-JS
31
+ if exclude.find {|e| name.to_s =~ e}
32
+ ""
33
+ else
34
+ build_js(name, route, options)
35
+ end
36
+ end.join("\n")
37
+ end
38
+
39
+ def build_js(name, route, options)
40
+ _ = <<-JS
29
41
  // #{route.name} => #{route.path}
30
42
  #{name.to_s}_path: function(#{build_params route}) {
31
43
  var opts = options || {};
@@ -35,7 +47,6 @@ module JsRoutes
35
47
  return Routes.check_path('#{build_path route}' + format) + Routes.serialize(opts);
36
48
  },
37
49
  JS
38
- end.join("\n")
39
50
  end
40
51
 
41
52
 
data/lib/routes.js CHANGED
@@ -1,29 +1,34 @@
1
- var Routes = {
1
+ (function(){
2
2
 
3
- IDENTITY
3
+ var Routes = {
4
4
 
5
- serialize: function(obj){
6
- if (obj == null) {return '';}
7
- var s = [];
8
- for (prop in obj){
9
- s.push(prop + "=" + obj[prop]);
10
- }
11
- if (s.length == 0) {
12
- return '';
13
- }
14
- return "?" + s.join('&');
15
- },
5
+ ROUTES
6
+
7
+ serialize: function(obj){
8
+ if (obj == null) {return '';}
9
+ var s = [];
10
+ for (prop in obj){
11
+ s.push(prop + "=" + obj[prop]);
12
+ }
13
+ if (s.length == 0) {
14
+ return '';
15
+ }
16
+ return "?" + s.join('&');
17
+ },
16
18
 
17
19
 
18
- check_parameter: function(param) {
19
- if (param === undefined) {
20
- param = '';
20
+ check_parameter: function(param) {
21
+ if (param === undefined) {
22
+ param = '';
23
+ }
24
+ return param;
25
+ },
26
+
27
+ check_path: function(path) {
28
+ return path.replace(/\.$/m, '');
21
29
  }
22
- return param;
23
- },
24
-
25
- check_path: function(path) {
26
- return path.replace(/\.$/m, '');
27
- }
28
-
29
- }
30
+
31
+ };
32
+
33
+ window.NAMESPACE = Routes;
34
+ })();
@@ -5,45 +5,64 @@ require "fileutils"
5
5
 
6
6
  describe JsRoutes do
7
7
  before(:each) do
8
+ Rails.application.stub!(:reload_routes!).and_return(true)
9
+ evaljs("var window = {};")
8
10
  evaljs(JsRoutes.generate(_options))
9
11
  end
10
12
 
11
13
  let(:_options) { {} }
12
14
 
13
15
  it "should generate collection routing" do
14
- evaljs("Routes.inboxes_path()").should == "/inboxes"
16
+ evaljs("window.Routes.inboxes_path()").should == "/inboxes"
15
17
  end
16
18
 
17
19
  it "should generate member routing" do
18
- evaljs("Routes.inbox_path(1)").should == "/inboxes/1"
20
+ evaljs("window.Routes.inbox_path(1)").should == "/inboxes/1"
19
21
  end
20
22
 
21
23
  it "should generate nested routing" do
22
- evaljs("Routes.inbox_message_path(1,2)").should == "/inboxes/1/messages/2"
24
+ evaljs("window.Routes.inbox_message_path(1,2)").should == "/inboxes/1/messages/2"
23
25
  end
24
26
 
25
27
  it "should generate routing with format" do
26
- evaljs("Routes.inbox_path(1, {format: 'json'})").should == "/inboxes/1.json"
28
+ evaljs("window.Routes.inbox_path(1, {format: 'json'})").should == "/inboxes/1.json"
27
29
  end
28
30
 
29
31
  it "should support get parameters" do
30
- evaljs("Routes.inbox_path(1, {format: 'json', q: 'hello', lang: 'ua'})").should == "/inboxes/1.json?q=hello&lang=ua"
32
+ evaljs("window.Routes.inbox_path(1, {format: 'json', q: 'hello', lang: 'ua'})").should == "/inboxes/1.json?q=hello&lang=ua"
31
33
  end
32
34
 
35
+ context "when exclude is specified" do
36
+
37
+ let(:_options) { {:exclude => /^admin_/} }
38
+
39
+ it "should exclude specified routes from file" do
40
+ evaljs("window.Routes.admin_users_path").should be_nil
41
+ end
42
+ end
33
43
 
34
44
  context "when default_format is specified" do
35
45
  let(:_options) { {:default_format => "json"} }
36
46
 
37
47
  it "should render routing with default_format" do
38
- evaljs("Routes.inbox_path(1)").should == "/inboxes/1.json"
48
+ evaljs("window.Routes.inbox_path(1)").should == "/inboxes/1.json"
39
49
  end
40
50
 
41
51
  it "should override default_format implicitly" do
42
- evaljs("Routes.inbox_path(1, {format: 'xml'})").should == "/inboxes/1.xml"
52
+ evaljs("window.Routes.inbox_path(1, {format: 'xml'})").should == "/inboxes/1.xml"
43
53
  end
44
54
 
45
55
  end
46
56
 
57
+ context "when namspace option is specified" do
58
+ let(:_options) { {:namespace => "PHM"} }
59
+ it "should use this name space for routing" do
60
+ evaljs("window.Routes").should be_nil
61
+ evaljs("window.PHM.inbox_path").should_not be_nil
62
+ end
63
+
64
+ end
65
+
47
66
  describe ".generate!" do
48
67
  let(:name) { "#{File.dirname(__FILE__)}/../routes.js" }
49
68
  it "should generate routes file" do
data/spec/spec_helper.rb CHANGED
@@ -11,16 +11,20 @@ def evaljs(string)
11
11
  @context.eval(string)
12
12
  end
13
13
 
14
+
14
15
  class App < Rails::Application
15
16
  self.routes.draw do
16
17
  resources :inboxes do
17
18
  resources :messages
18
19
  end
20
+
21
+ namespace :admin do
22
+ resources :users
23
+ end
19
24
  end
20
25
  end
21
26
 
22
27
 
23
-
24
28
  # Requires supporting files with custom matchers and macros, etc,
25
29
  # in ./support/ and its subdirectories.
26
30
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
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: 23
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 0.2.0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Bogdan Gusiev
@@ -126,7 +126,7 @@ files:
126
126
  - Rakefile
127
127
  - Readme.md
128
128
  - VERSION
129
- - jsroutes.gemspec
129
+ - js-routes.gemspec
130
130
  - lib/js_routes.rb
131
131
  - lib/jsroutes.rb
132
132
  - lib/routes.js