js-routes 1.3.3 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5b720670fa76910ce7f6648985f595280bb07c16
4
- data.tar.gz: 6ea110ece8ff7cf6e5fc1fe6635819f62c15464e
3
+ metadata.gz: da4a6b84210ad4604692606b4ea0e5c79fc9ee7a
4
+ data.tar.gz: 342a9ab0a4b396aef730e88af2abc782d64db743
5
5
  SHA512:
6
- metadata.gz: 37d1e5fa18e348092b4f56525eb6c61626b8b4820e46bffc2b16f28466a181f3a233499c8cc9004db4dc5dee551c3dd1998efc8ed3515b43a9ee1e551e477c0b
7
- data.tar.gz: fce1d09de9f6b5772a9b9b0a6f2a4759722f72f7a45e7032a9bcb7341f7987807624bff591d5115edfebcd1b04403aac258f4a46daa6f496f49f34dbddd94341
6
+ metadata.gz: 34511f3806f9985f569cfca45ead8e4766be104912d851c9e48ffb4ef2b0d25cb0df17d7eeac42c0d681aa847ab72324c4cb4a4046b63d7ad4dfc1ca51ecc915
7
+ data.tar.gz: 905284b66bb0e6f8889a6fee1bdbbd521012990d0d69186ccd13b651e10d0a0a202e8b34493290fb1cdc94844d31e087d7883907d75ab95fe6f5d8186446c2cb
data/.travis.yml CHANGED
@@ -9,6 +9,7 @@ rvm:
9
9
  - 2.1
10
10
  - 2.2.5
11
11
  - 2.3.1
12
+ - 2.4.1
12
13
  - jruby-19mode
13
14
  - ruby-head
14
15
  - jruby-head
@@ -49,3 +50,11 @@ matrix:
49
50
  gemfile: gemfiles/rails50.gemfile
50
51
  - rvm: 2.1
51
52
  gemfile: gemfiles/rails50_sprockets3.gemfile
53
+ - rvm: 2.4.1
54
+ gemfile: gemfiles/rails40.gemfile # Segmentation fault (core dumped) for 2.4
55
+ - rvm: 2.4.1
56
+ gemfile: gemfiles/rails40_sprockets3.gemfile # Segmentation fault (core dumped) for 2.4
57
+ - rvm: 2.4.1
58
+ gemfile: gemfiles/rails41.gemfile # Segmentation fault (core dumped) for 2.4
59
+ - rvm: 2.4.1
60
+ gemfile: gemfiles/rails41_sprockets3.gemfile # Segmentation fault (core dumped) for 2.4
data/Appraisals CHANGED
@@ -1,9 +1,9 @@
1
1
  appraise "rails32" do
2
- gem "railties", "~> 3.2.18"
2
+ gem "railties", "~> 3.2.22.5"
3
3
  gem 'tzinfo'
4
4
  end
5
5
 
6
- {rails40: '4.0.5', rails41: '4.1.1', rails42: '4.2.1', rails50: '5.0.0'}.each do |rails, version|
6
+ {rails40: '4.0.13', rails41: '4.1.16', rails42: '4.2.8', rails50: '5.0.2'}.each do |rails, version|
7
7
  appraise "#{rails}" do
8
8
  gem "railties", "~> #{version}"
9
9
  gem "sprockets", "< 3"
@@ -13,4 +13,4 @@ end
13
13
  gem "railties", "~> #{version}"
14
14
  gem "sprockets", "~> 3.0"
15
15
  end
16
- end
16
+ end
data/CHANGELOG.md CHANGED
@@ -1,5 +1,41 @@
1
1
  ## master
2
2
 
3
+ * __breaking change!__ Implemented Routes.config() and Routes.configure instead of Routes.defaults
4
+
5
+ New methods support 4 options at the moment:
6
+
7
+ ``` js
8
+ Routes.configuration(); // =>
9
+ /*
10
+ {
11
+ prefix: "",
12
+ default_url_options: {},
13
+ special_options_key: '_options',
14
+ custom_serializer: function(...) { ... }
15
+ }
16
+ */
17
+
18
+ Routes.configure({
19
+ prefix: '/app',
20
+ default_url_options: {format: 'json'},
21
+ special_options_key: '_my_options_key',
22
+ custom_serializer: function(...) { ... }
23
+ });
24
+ ```
25
+
26
+ ## v1.3.3
27
+
28
+ * Improved optional parameters support #216
29
+
30
+ ## v1.3.2
31
+
32
+ * Added `application` option #214
33
+
34
+ ## v1.3.1
35
+
36
+ * Raise error object with id null passed as route paramter #209
37
+ * Sprockets bugfixes #212
38
+
3
39
  ## v1.3.0
4
40
 
5
41
  * Introduce the special _options key. Fixes #86
data/Readme.md CHANGED
@@ -39,10 +39,19 @@ JsRoutes.setup do |config|
39
39
  end
40
40
  ```
41
41
 
42
+ Or make a more dynamic configuration in JavaScript, but only specific options support the possibility of such configuration(see the list below):
43
+
44
+ ``` js
45
+ Routes.configure({
46
+ option: value
47
+ });
48
+ Routes.config(); // current config
49
+ ```
50
+
42
51
  Available options:
43
52
 
44
53
  * `default_url_options` - default parameters used when generating URLs
45
- * Note that only specific options are supported at this time.
54
+ * Option is configurable at JS level with `Routes.configure()`
46
55
  * Example: {:format => "json", :trailing\_slash => true, :protocol => "https", :host => "example.com", :port => 3000}
47
56
  * Default: {}
48
57
  * `exclude` - Array of regexps to exclude from js routes.
@@ -55,6 +64,7 @@ Available options:
55
64
  * Supports nested namespace like `MyProject.routes`
56
65
  * Default: `Routes`
57
66
  * `prefix` - String representing a url path to prepend to all paths.
67
+ * Option is configurable at JS level with `Routes.configure()`
58
68
  * Example: `http://yourdomain.com`. This will cause route helpers to generate full path only.
59
69
  * Default: `Rails.application.config.relative_url_root`
60
70
  * `camel_case` (version >= 0.8.8) - Generate camel case route names.
@@ -68,11 +78,13 @@ Available options:
68
78
  * Sample route call when option is set to true: Routes.users() => `/users`
69
79
  * `serializer` (version >= 1.1.0) - Puts a JS function here that serializes a Javascript Hash object into URL paramters: `{a: 1, b: 2} => "a=1&b=2"`.
70
80
  * Default: `nil`. Uses built-in serializer
81
+ * Option is configurable at JS level with `Routes.configure()`
71
82
  * Example: `jQuery.param` - use jQuery's serializer algorithm. You can attach serialize function from your favorite AJAX framework.
72
83
  * Example: `MyApp.custom_serialize` - use completely custom serializer of your application.
73
84
 
74
85
  * `special_options_key` - a special key that helps js-routes to destinguish serialized model from options hash
75
86
  * This option is required because JS doesn't provide a difference between an object and a hash
87
+ * Option is configurable at JS level with `Routes.configure()`
76
88
  * Default: `_options`
77
89
  * `application` - a key to specify which rails engine you want to generate routes too.
78
90
  * This option allows to only generate routes for a specific rails engine, that is mounted into routes instead of all Rails app routes
@@ -2,7 +2,7 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
- gem "railties", "~> 3.2.18"
5
+ gem "railties", "~> 3.2.22.5"
6
6
  gem "tzinfo"
7
7
 
8
8
  gemspec :path => "../"
@@ -2,7 +2,7 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
- gem "railties", "~> 4.0.5"
5
+ gem "railties", "~> 4.0.13"
6
6
  gem "sprockets", "< 3"
7
7
 
8
8
  gemspec :path => "../"
@@ -2,7 +2,7 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
- gem "railties", "~> 4.0.5"
5
+ gem "railties", "~> 4.0.13"
6
6
  gem "sprockets", "~> 3.0"
7
7
 
8
8
  gemspec :path => "../"
@@ -2,7 +2,7 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
- gem "railties", "~> 4.1.1"
5
+ gem "railties", "~> 4.1.16"
6
6
  gem "sprockets", "< 3"
7
7
 
8
8
  gemspec :path => "../"
@@ -2,7 +2,7 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
- gem "railties", "~> 4.1.1"
5
+ gem "railties", "~> 4.1.16"
6
6
  gem "sprockets", "~> 3.0"
7
7
 
8
8
  gemspec :path => "../"
@@ -2,8 +2,7 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
- gem "railties", "~> 4.2.1"
5
+ gem "railties", "~> 4.2.8"
6
6
  gem "sprockets", "< 3"
7
- gem "nokogiri", "< 1.7"
8
7
 
9
8
  gemspec :path => "../"
@@ -2,8 +2,7 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
- gem "railties", "~> 4.2.1"
5
+ gem "railties", "~> 4.2.8"
6
6
  gem "sprockets", "~> 3.0"
7
- gem "nokogiri", "< 1.7"
8
7
 
9
8
  gemspec :path => "../"
@@ -2,7 +2,7 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
- gem "railties", "~> 5.0.0"
5
+ gem "railties", "~> 5.0.2"
6
6
  gem "sprockets", "< 3"
7
7
 
8
8
  gemspec :path => "../"
@@ -2,7 +2,7 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
- gem "railties", "~> 5.0.0"
5
+ gem "railties", "~> 5.0.2"
6
6
  gem "sprockets", "~> 3.0"
7
7
 
8
8
  gemspec :path => "../"
data/js-routes.gemspec CHANGED
@@ -31,7 +31,6 @@ Gem::Specification.new do |s|
31
31
  else
32
32
  s.add_development_dependency(%q<byebug>)
33
33
  s.add_development_dependency(%q<pry-byebug>)
34
- s.add_development_dependency(%q<therubyracer>, [">= 0.12.1"])
34
+ s.add_development_dependency(%q<therubyracer>, [">= 0.12.3"])
35
35
  end
36
36
  end
37
-
data/lib/js_routes.rb CHANGED
@@ -15,13 +15,14 @@ class JsRoutes
15
15
  exclude: [],
16
16
  include: //,
17
17
  file: DEFAULT_PATH,
18
- prefix: nil,
18
+ prefix: -> { Rails.application.config.relative_url_root || "" },
19
19
  url_links: false,
20
20
  camel_case: false,
21
21
  default_url_options: {},
22
22
  compact: false,
23
23
  serializer: nil,
24
24
  special_options_key: "_options",
25
+ application: -> { Rails.application }
25
26
  }
26
27
 
27
28
  NODE_TYPES = {
@@ -39,7 +40,29 @@ class JsRoutes
39
40
  FILTERED_DEFAULT_PARTS = [:controller, :action, :subdomain]
40
41
  URL_OPTIONS = [:protocol, :domain, :host, :port, :subdomain]
41
42
 
42
- class Options < Struct.new(*DEFAULTS.keys)
43
+ class Configuration < Struct.new(*DEFAULTS.keys)
44
+ def initialize(attributes = nil)
45
+ assign(DEFAULTS)
46
+ return unless attributes
47
+ assign(attributes)
48
+ end
49
+
50
+ def assign(attributes)
51
+ attributes.each do |attribute, value|
52
+ value = value.call if value.is_a?(Proc)
53
+ send(:"#{attribute}=", value)
54
+ end
55
+ self
56
+ end
57
+
58
+ def [](attribute)
59
+ send(attribute)
60
+ end
61
+
62
+ def merge(attributes)
63
+ clone.assign(attributes)
64
+ end
65
+
43
66
  def to_hash
44
67
  Hash[*members.zip(values).flatten(1)].symbolize_keys
45
68
  end
@@ -51,13 +74,16 @@ class JsRoutes
51
74
 
52
75
  class << self
53
76
  def setup(&block)
54
- options.tap(&block) if block
77
+ configuration.tap(&block) if block
55
78
  end
56
79
 
57
80
  def options
58
- @options ||= Options.new.tap do |opts|
59
- DEFAULTS.each_pair {|k,v| opts[k] = v}
60
- end
81
+ ActiveSupport::Deprecation.warn('JsRoutes.options method is deprecated use JsRoutes.configuration instead')
82
+ configuration
83
+ end
84
+
85
+ def configuration
86
+ @configuration ||= Configuration.new
61
87
  end
62
88
 
63
89
  def generate(opts = {})
@@ -92,7 +118,7 @@ class JsRoutes
92
118
  #
93
119
 
94
120
  def initialize(options = {})
95
- @options = self.class.options.to_hash.merge(options)
121
+ @configuration = self.class.configuration.merge(options)
96
122
  end
97
123
 
98
124
  def generate
@@ -103,49 +129,28 @@ class JsRoutes
103
129
 
104
130
  {
105
131
  "GEM_VERSION" => JsRoutes::VERSION,
106
- "APP_CLASS" => application.class.to_s,
107
- "NAMESPACE" => @options[:namespace],
108
- "DEFAULT_URL_OPTIONS" => json(@options[:default_url_options].merge(deprecate_url_options)),
109
- "PREFIX" => @options[:prefix] || Rails.application.config.relative_url_root || "",
110
- "NODE_TYPES" => json(NODE_TYPES),
111
- "SERIALIZER" => @options[:serializer] || json(nil),
112
132
  "ROUTES" => js_routes,
113
- "SPECIAL_OPTIONS_KEY" => @options[:special_options_key].to_s,
114
133
  "DEPRECATED_BEHAVIOR" => Rails.version < "4",
134
+ "NODE_TYPES" => json(NODE_TYPES),
135
+
136
+ "APP_CLASS" => application.class.to_s,
137
+ "NAMESPACE" => json(@configuration.namespace),
138
+ "DEFAULT_URL_OPTIONS" => json(@configuration.default_url_options),
139
+ "PREFIX" => json(@configuration.prefix),
140
+ "SPECIAL_OPTIONS_KEY" => json(@configuration.special_options_key),
141
+ "SERIALIZER" => @configuration.serializer || json(nil),
115
142
  }.inject(File.read(File.dirname(__FILE__) + "/routes.js")) do |js, (key, value)|
116
143
  js.gsub!(key, value.to_s)
117
144
  end
118
145
  end
119
146
 
120
- def deprecate_url_options
121
- result = {}
122
- if @options.key?(:default_format)
123
- warn("default_format option is deprecated. Use default_url_options = { format: <format> } instead")
124
- result.merge!( format: @options[:default_format] )
125
- end
126
- if @options[:url_links].is_a?(String)
127
- ActiveSupport::Deprecation.warn('js-routes url_links config value must be a boolean. Use default_url_options for specifying a default host.')
128
-
129
- raise "invalid URL format in url_links (ex: http[s]://example.com)" if @options[:url_links].match(URI::Parser.new.make_regexp(%w(http https))).nil?
130
- uri = URI.parse(@options[:url_links])
131
- default_port = uri.scheme == "https" ? 443 : 80
132
- port = uri.port == default_port ? nil : uri.port
133
- result.merge!(
134
- host: uri.host,
135
- port: port,
136
- protocol: uri.scheme,
137
- )
138
- end
139
- result
140
- end
141
-
142
147
  def generate!(file_name = nil)
143
148
  # Some libraries like Devise do not yet loaded their routes so we will wait
144
149
  # until initialization process finish
145
150
  # https://github.com/railsware/js-routes/issues/7
146
151
  Rails.configuration.after_initialize do
147
- file_name ||= self.class.options['file']
148
- File.open(Rails.root.join(file_name || DEFAULT_PATH), 'w') do |f|
152
+ file_name ||= self.class.configuration['file']
153
+ File.open(Rails.root.join(file_name), 'w') do |f|
149
154
  f.write generate
150
155
  end
151
156
  end
@@ -154,7 +159,7 @@ class JsRoutes
154
159
  protected
155
160
 
156
161
  def application
157
- @options[:application] || Rails.application
162
+ @configuration.application
158
163
  end
159
164
 
160
165
  def named_routes
@@ -189,7 +194,7 @@ class JsRoutes
189
194
  end
190
195
 
191
196
  def build_route_if_match(route, parent_route=nil)
192
- if any_match?(route, parent_route, @options[:exclude]) || !any_match?(route, parent_route, @options[:include])
197
+ if any_match?(route, parent_route, @configuration[:exclude]) || !any_match?(route, parent_route, @configuration[:include])
193
198
  nil
194
199
  else
195
200
  build_js(route, parent_route)
@@ -205,7 +210,7 @@ class JsRoutes
205
210
 
206
211
  def build_js(route, parent_route)
207
212
  name = [parent_route.try(:name), route.name].compact
208
- route_name = generate_route_name(name, (:path unless @options[:compact]))
213
+ route_name = generate_route_name(name, (:path unless @configuration[:compact]))
209
214
  parent_spec = parent_route.try(:path).try(:spec)
210
215
  route_arguments = route_js_arguments(route, parent_spec)
211
216
  url_link = generate_url_link(name, route_name, route_arguments, route)
@@ -236,7 +241,7 @@ class JsRoutes
236
241
  end
237
242
 
238
243
  def generate_url_link(name, route_name, route_arguments, route)
239
- return "" unless @options[:url_links]
244
+ return "" unless @configuration[:url_links]
240
245
  <<-JS.strip!
241
246
  #{generate_route_name(name, :url)}: Utils.route(#{route_arguments}, true)
242
247
  JS
@@ -245,7 +250,7 @@ class JsRoutes
245
250
  def generate_route_name(name, suffix)
246
251
  route_name = name.join('_')
247
252
  route_name << "_#{ suffix }" if suffix
248
- @options[:camel_case] ? route_name.camelize(:lower) : route_name
253
+ @configuration[:camel_case] ? route_name.camelize(:lower) : route_name
249
254
  end
250
255
 
251
256
  def json(string)
@@ -30,13 +30,12 @@ end
30
30
  class Engine < ::Rails::Engine
31
31
  require 'sprockets/version'
32
32
  v2 = Gem::Dependency.new('', ' ~> 2')
33
- v3 = Gem::Dependency.new('', ' >= 3' ,' < 3.7')
34
- v37 = Gem::Dependency.new('', ' >= 3.7')
33
+ vgte3 = Gem::Dependency.new('', ' >= 3')
35
34
  sprockets_version = Gem::Version.new(::Sprockets::VERSION).release
36
35
  initializer_args = case sprockets_version
37
36
  when -> (v) { v2.match?('', v) }
38
37
  { after: "sprockets.environment" }
39
- when -> (v) { v3.match?('', v) || v37.match?('', v) }
38
+ when -> (v) { vgte3.match?('', v) }
40
39
  { after: :engines_blank_point, before: :finisher_hook }
41
40
  else
42
41
  raise StandardError, "Sprockets version #{sprockets_version} is not supported"
@@ -48,8 +47,7 @@ class Engine < ::Rails::Engine
48
47
  initializer 'js-routes.dependent_on_routes', initializer_args do
49
48
  case sprockets_version
50
49
  when -> (v) { v2.match?('', v) },
51
- -> (v) { v3.match?('', v) },
52
- -> (v) { v37.match?('', v) }
50
+ -> (v) { vgte3.match?('', v) }
53
51
 
54
52
  # It seems rails 3.2 is not working if
55
53
  # `Rails.application.config.assets.configure` is used for
@@ -1,3 +1,3 @@
1
1
  class JsRoutes
2
- VERSION = "1.3.3"
2
+ VERSION = "1.4.0"
3
3
  end