js-routes 0.8.7 → 0.8.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,132 @@
1
+ ParameterMissing = (@message) -> #
2
+ ParameterMissing:: = new Error()
3
+ defaults =
4
+ prefix: "PREFIX"
5
+ default_url_options: DEFAULT_URL_OPTIONS
6
+
7
+ NodeTypes = NODE_TYPES
8
+ Utils =
9
+ serialize: (obj) ->
10
+ return "" unless obj
11
+ if window.jQuery
12
+ result = window.jQuery.param(obj)
13
+ return (if not result then "" else "?#{result}")
14
+ s = []
15
+ for own key, prop of obj
16
+ if prop?
17
+ if prop instanceof Array
18
+ for val, i in prop
19
+ s.push "#{key}#{encodeURIComponent("[]")}=#{encodeURIComponent(val.toString())}"
20
+ else
21
+ s.push "#{key}=#{encodeURIComponent(prop.toString())}"
22
+ return "" unless s.length
23
+ "?#{s.join("&")}"
24
+
25
+ clean_path: (path) ->
26
+ path = path.split("://")
27
+ last_index = path.length - 1
28
+ path[last_index] = path[last_index].replace(/\/+/g, "/").replace(/\/$/m, "")
29
+ path.join "://"
30
+
31
+ set_default_url_options: (optional_parts, options) ->
32
+ for part, i in optional_parts
33
+ if not options.hasOwnProperty(part) and defaults.default_url_options.hasOwnProperty(part)
34
+ options[part] = defaults.default_url_options[part]
35
+
36
+ extract_anchor: (options) ->
37
+ anchor = ""
38
+ if options.hasOwnProperty("anchor")
39
+ anchor = "##{options.anchor}"
40
+ options.anchor = null
41
+ anchor
42
+
43
+ extract_options: (number_of_params, args) ->
44
+ ret_value = {}
45
+ if args.length > number_of_params and typeof (args[args.length - 1]) is "object"
46
+ ret_value = args.pop()
47
+ ret_value
48
+
49
+ path_identifier: (object) ->
50
+ return "0" if object is 0
51
+ # null, undefined, false or ''
52
+ return "" unless object
53
+ property = object
54
+ if typeof (object) is "object"
55
+ property = object.to_param or object.id or object
56
+ property = property.call(object) if typeof (property) is "function"
57
+ property.toString()
58
+
59
+ clone: (obj) ->
60
+ return obj if null is obj or "object" isnt typeof obj
61
+ copy = obj.constructor()
62
+ copy[key] = attr for own key, attr of obj
63
+ copy
64
+
65
+ prepare_parameters: (required_parameters, actual_parameters, options) ->
66
+ result = @clone(options) or {}
67
+ result[val] = actual_parameters[i] for val, i in required_parameters
68
+ result
69
+
70
+ build_path: (required_parameters, optional_parts, route, args) ->
71
+ args = Array::slice.call(args)
72
+ opts = @extract_options(required_parameters.length, args)
73
+ throw new Error("Too many parameters provided for path") if args.length > required_parameters.length
74
+ parameters = @prepare_parameters(required_parameters, args, opts)
75
+ @set_default_url_options optional_parts, parameters
76
+ result = "#{Utils.get_prefix()}#{@visit(route, parameters)}"
77
+ Utils.clean_path("#{result}#{Utils.extract_anchor(parameters)}") + Utils.serialize(parameters)
78
+ #
79
+ # This function is JavaScript impelementation of the
80
+ # Journey::Visitors::Formatter that builds route by given parameters
81
+ # from route binary tree.
82
+ # Binary tree is serialized in the following way:
83
+ # [node type, left node, right node ]
84
+ #
85
+ # @param {Boolean} optional Marks the currently visited branch as optional.
86
+ # If set to `true`, this method will not throw when encountering
87
+ # a missing parameter (used in recursive calls).
88
+ #
89
+ visit: (route, parameters, optional) ->
90
+ [type, left, right] = route
91
+ switch type
92
+ when NodeTypes.GROUP, NodeTypes.STAR
93
+ @visit left, parameters, true
94
+ when NodeTypes.LITERAL, NodeTypes.SLASH, NodeTypes.DOT
95
+ left
96
+ when NodeTypes.CAT
97
+ left_part = @visit(left, parameters, optional)
98
+ right_part = @visit(right, parameters, optional)
99
+ return "" if optional and not (left_part and right_part)
100
+ "#{left_part}#{right_part}"
101
+ when NodeTypes.SYMBOL
102
+ value = parameters[left]
103
+ if value?
104
+ parameters[left] = null
105
+ return @path_identifier(value)
106
+ if optional
107
+ "" # missing parameter
108
+ else
109
+ throw new ParameterMissing("Route parameter missing: #{left}")
110
+ #
111
+ # I don't know what is this node type
112
+ # Please send your PR if you do
113
+ #
114
+ # when NodeTypes.OR:
115
+ else
116
+ throw new Error("Unknown Rails node type")
117
+
118
+ get_prefix: ->
119
+ prefix = defaults.prefix
120
+ prefix = (if prefix.match("/$") then prefix else "#{prefix}/") if prefix isnt ""
121
+ prefix
122
+
123
+ namespace: (root, namespaceString) ->
124
+ parts = (if namespaceString then namespaceString.split(".") else [])
125
+ return unless parts.length
126
+ current = parts.shift()
127
+ root[current] = root[current] or {}
128
+ Utils.namespace root[current], parts.join(".")
129
+
130
+ Utils.namespace window, "NAMESPACE"
131
+ window.NAMESPACE = ROUTES
132
+ window.NAMESPACE.options = defaults
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe JsRoutes, "options" do
4
-
4
+
5
5
  before(:each) do
6
6
  evaljs(_presetup)
7
7
  with_warnings(_warnings) do
@@ -14,7 +14,7 @@ describe JsRoutes, "options" do
14
14
  let(:_warnings) { true }
15
15
 
16
16
  context "when exclude is specified" do
17
-
17
+
18
18
  let(:_options) { {:exclude => /^admin_/} }
19
19
 
20
20
  it "should exclude specified routes from file" do
@@ -26,7 +26,7 @@ describe JsRoutes, "options" do
26
26
  end
27
27
  end
28
28
  context "when include is specified" do
29
-
29
+
30
30
  let(:_options) { {:include => /^admin_/} }
31
31
 
32
32
  it "should exclude specified routes from file" do
@@ -39,7 +39,7 @@ describe JsRoutes, "options" do
39
39
  end
40
40
 
41
41
  context "when prefix with trailing slash is specified" do
42
-
42
+
43
43
  let(:_options) { {:prefix => "/myprefix/" } }
44
44
 
45
45
  it "should render routing with prefix" do
@@ -54,22 +54,22 @@ describe JsRoutes, "options" do
54
54
  end
55
55
 
56
56
  context "when prefix with http:// is specified" do
57
-
57
+
58
58
  let(:_options) { {:prefix => "http://localhost:3000" } }
59
59
 
60
60
  it "should render routing with prefix" do
61
61
  evaljs("Routes.inbox_path(1)").should == _options[:prefix] + routes.inbox_path(1)
62
62
  end
63
63
  end
64
-
64
+
65
65
  context "when prefix without trailing slash is specified" do
66
-
66
+
67
67
  let(:_options) { {:prefix => "/myprefix" } }
68
68
 
69
69
  it "should render routing with prefix" do
70
70
  evaljs("Routes.inbox_path(1)").should == "/myprefix#{routes.inbox_path(1)}"
71
71
  end
72
-
72
+
73
73
  it "should render routing with prefix set in JavaScript" do
74
74
  evaljs("Routes.options.prefix = '/newprefix'")
75
75
  evaljs("Routes.inbox_path(1)").should == "/newprefix#{routes.inbox_path(1)}"
@@ -80,7 +80,7 @@ describe JsRoutes, "options" do
80
80
  context "when default_format is specified" do
81
81
  let(:_options) { {:default_format => "json"} }
82
82
  let(:_warnings) { nil }
83
-
83
+
84
84
  it "should render routing with default_format" do
85
85
  evaljs("Routes.inbox_path(1)").should == routes.inbox_path(1, :format => "json")
86
86
  end
@@ -110,7 +110,7 @@ describe JsRoutes, "options" do
110
110
  end
111
111
  end
112
112
 
113
- describe "when nested namespace option is specified" do
113
+ describe "when nested namespace option is specified" do
114
114
  context "and defined on client" do
115
115
  let(:_presetup) { "window.PHM = {}" }
116
116
  let(:_options) { {:namespace => "PHM.Routes"} }
@@ -123,7 +123,7 @@ describe JsRoutes, "options" do
123
123
  let(:_options) { {:namespace => "PHM.Routes"} }
124
124
  it "should initialize namespace" do
125
125
  evaljs("window.PHM.Routes.inbox_path").should_not be_nil
126
- end
126
+ end
127
127
  end
128
128
 
129
129
  context "and some parts are defined" do
@@ -152,4 +152,24 @@ describe JsRoutes, "options" do
152
152
  end
153
153
  end
154
154
  end
155
+
156
+ describe "camel_case" do
157
+ context "with default option" do
158
+ let(:_options) { Hash.new }
159
+ it "should use snake case routes" do
160
+ evaljs("Routes.inbox_path(1)").should == routes.inbox_path(1)
161
+ evaljs("Routes.inboxPath").should be_nil
162
+ end
163
+ end
164
+
165
+ context "with true" do
166
+ let(:_options) { { :camel_case => true } }
167
+ it "should generate camel case routes" do
168
+ evaljs("Routes.inbox_path").should be_nil
169
+ evaljs("Routes.inboxPath").should_not be_nil
170
+ evaljs("Routes.inboxPath(1)").should == routes.inbox_path(1)
171
+ evaljs("Routes.inboxMessagesPath(10)").should == routes.inbox_messages_path(:inbox_id => 10)
172
+ end
173
+ end
174
+ end
155
175
  end
@@ -54,11 +54,11 @@ describe JsRoutes, "compatibility with Rails" do
54
54
  it "should support url anchor given as parameter" do
55
55
  evaljs("Routes.inbox_path(1, {anchor: 'hello'})").should == routes.inbox_path(1, :anchor => "hello")
56
56
  end
57
-
57
+
58
58
  it "should support engine routes" do
59
59
  evaljs("Routes.blog_app_posts_path()").should == blog_routes.posts_path()
60
60
  end
61
-
61
+
62
62
  it "should support engine routes with parameter" do
63
63
  evaljs("Routes.blog_app_post_path(1)").should == blog_routes.post_path(1)
64
64
  end
@@ -149,7 +149,7 @@ describe JsRoutes, "compatibility with Rails" do
149
149
  end
150
150
 
151
151
  context "when wrong parameters given" do
152
-
152
+
153
153
  it "should throw Exception if not enough parameters" do
154
154
  lambda {
155
155
  evaljs("Routes.inbox_path()")
data/spec/spec_helper.rb CHANGED
@@ -1,13 +1,14 @@
1
1
  # encoding: utf-8
2
2
 
3
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
- $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
+ $:.unshift(File.dirname(__FILE__))
5
5
  require 'rspec'
6
6
  require 'rails/all'
7
7
  require 'js-routes'
8
8
  require "v8"
9
9
  require "cgi"
10
10
  require "active_support/core_ext/hash/slice"
11
+ require 'coffee-script'
11
12
 
12
13
  def jscontext
13
14
  @context ||= V8::Context.new
@@ -44,7 +45,7 @@ def draw_routes
44
45
  BlogEngine::Engine.routes.draw do
45
46
  resources :posts
46
47
  end
47
- App.routes.draw do
48
+ App.routes.draw do
48
49
  resources :inboxes do
49
50
  resources :messages do
50
51
  resources :attachments
@@ -63,7 +64,7 @@ def draw_routes
63
64
  scope "(/optional/:optional_id)" do
64
65
  resources :things
65
66
  end
66
-
67
+
67
68
  match "/other_optional/(:optional_id)" => "foo#foo", :as => :foo
68
69
 
69
70
  match 'books/*section/:title' => 'books#show', :as => :book
@@ -87,14 +88,20 @@ Rails.configuration.active_support.deprecation = :log
87
88
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
88
89
 
89
90
  RSpec.configure do |config|
90
-
91
+
91
92
  config.before(:each) do
92
93
  evaljs("var window = this;")
93
- jscontext[:cgi] = CGI
94
- evaljs("function encodeURIComponent(string) {return cgi.escape(string);}")
94
+ # No need to replace native V8 functions for now
95
+ #jscontext[:cgi] = CGI
96
+ #evaljs("function encodeURIComponent(string) {return cgi.escape(string);}")
95
97
  jscontext[:log] = lambda {|arg| puts arg.inspect}
96
98
  end
97
99
  config.before(:all) do
100
+ # compile all js files begin
101
+ Dir["#{File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))}/**/*.coffee"].each do |coffee|
102
+ File.open(coffee.gsub(/\.coffee$/, ""), 'w') {|f| f.write(CoffeeScript.compile(File.read(coffee))) }
103
+ end
104
+ # compile all js files end
98
105
  draw_routes
99
106
  end
100
107
  end
metadata CHANGED
@@ -1,163 +1,185 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: js-routes
3
- version: !ruby/object:Gem::Version
4
- hash: 49
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.8.8
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 8
9
- - 7
10
- version: 0.8.7
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Bogdan Gusiev
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2013-02-13 00:00:00 +02:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
22
- requirement: &id001 !ruby/object:Gem::Requirement
23
- none: false
24
- requirements:
25
- - - ">="
26
- - !ruby/object:Gem::Version
27
- hash: 3
28
- segments:
29
- - 3
30
- - 2
31
- version: "3.2"
32
- version_requirements: *id001
33
- prerelease: false
12
+ date: 2013-02-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
34
15
  name: rails
35
- type: :runtime
36
- - !ruby/object:Gem::Dependency
37
- requirement: &id002 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
38
17
  none: false
39
- requirements:
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- hash: 3
43
- segments:
44
- - 0
45
- version: "0"
46
- version_requirements: *id002
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.2'
22
+ type: :runtime
47
23
  prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '3.2'
30
+ - !ruby/object:Gem::Dependency
48
31
  name: therubyracer
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
49
38
  type: :development
50
- - !ruby/object:Gem::Dependency
51
- requirement: &id003 !ruby/object:Gem::Requirement
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
52
41
  none: false
53
- requirements:
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
54
51
  - - ~>
55
- - !ruby/object:Gem::Version
56
- hash: 39
57
- segments:
58
- - 2
59
- - 10
60
- - 0
52
+ - !ruby/object:Gem::Version
61
53
  version: 2.10.0
62
- version_requirements: *id003
63
- prerelease: false
64
- name: rspec
65
54
  type: :development
66
- - !ruby/object:Gem::Dependency
67
- requirement: &id004 !ruby/object:Gem::Requirement
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
68
57
  none: false
69
- requirements:
58
+ requirements:
70
59
  - - ~>
71
- - !ruby/object:Gem::Version
72
- hash: 19
73
- segments:
74
- - 1
75
- - 1
76
- - 0
60
+ - !ruby/object:Gem::Version
61
+ version: 2.10.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
77
69
  version: 1.1.0
78
- version_requirements: *id004
70
+ type: :development
79
71
  prerelease: false
80
- name: bundler
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 1.1.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: guard
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
81
86
  type: :development
82
- - !ruby/object:Gem::Dependency
83
- requirement: &id005 !ruby/object:Gem::Requirement
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
84
89
  none: false
85
- requirements:
86
- - - ~>
87
- - !ruby/object:Gem::Version
88
- hash: 11
89
- segments:
90
- - 1
91
- - 6
92
- - 2
93
- version: 1.6.2
94
- version_requirements: *id005
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rb-fsevent
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
95
103
  prerelease: false
96
- name: jeweler
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: guard-coffeescript
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
97
118
  type: :development
98
- description: Generates javascript file that defines all Rails named routes as javascript helpers
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description: Generates javascript file that defines all Rails named routes as javascript
127
+ helpers
99
128
  email: agresso@gmail.com
100
129
  executables: []
101
-
102
130
  extensions: []
103
-
104
- extra_rdoc_files:
131
+ extra_rdoc_files:
105
132
  - LICENSE.txt
106
- files:
133
+ files:
107
134
  - .document
135
+ - .gitignore
108
136
  - .rspec
109
137
  - .travis.yml
110
138
  - Gemfile
111
- - Gemfile.lock
139
+ - Guardfile
112
140
  - LICENSE.txt
113
141
  - Rakefile
114
142
  - Readme.md
115
- - VERSION
116
143
  - app/assets/javascripts/js-routes.js.erb
117
144
  - js-routes.gemspec
118
145
  - lib/js-routes.rb
119
146
  - lib/js_routes.rb
120
147
  - lib/js_routes/engine.rb
148
+ - lib/js_routes/version.rb
121
149
  - lib/routes.js
150
+ - lib/routes.js.coffee
122
151
  - lib/tasks/js_routes.rake
123
152
  - spec/js_routes/generated_javascript_spec.rb
124
153
  - spec/js_routes/options_spec.rb
125
154
  - spec/js_routes/post_rails_init_spec.rb
126
155
  - spec/js_routes/rails_routes_compatibility_spec.rb
127
156
  - spec/spec_helper.rb
128
- has_rdoc: true
129
157
  homepage: http://github.com/railsware/js-routes
130
- licenses:
158
+ licenses:
131
159
  - MIT
132
160
  post_install_message:
133
161
  rdoc_options: []
134
-
135
- require_paths:
162
+ require_paths:
136
163
  - lib
137
- required_ruby_version: !ruby/object:Gem::Requirement
164
+ required_ruby_version: !ruby/object:Gem::Requirement
138
165
  none: false
139
- requirements:
140
- - - ">="
141
- - !ruby/object:Gem::Version
142
- hash: 3
143
- segments:
166
+ requirements:
167
+ - - ! '>='
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ segments:
144
171
  - 0
145
- version: "0"
146
- required_rubygems_version: !ruby/object:Gem::Requirement
172
+ hash: -1021076515432540813
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
174
  none: false
148
- requirements:
149
- - - ">="
150
- - !ruby/object:Gem::Version
151
- hash: 3
152
- segments:
153
- - 0
154
- version: "0"
175
+ requirements:
176
+ - - ! '>='
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
155
179
  requirements: []
156
-
157
180
  rubyforge_project:
158
- rubygems_version: 1.6.0
181
+ rubygems_version: 1.8.24
159
182
  signing_key:
160
183
  specification_version: 3
161
184
  summary: Brings Rails named routes to javascript
162
185
  test_files: []
163
-