commit rubygem-js-routes for openSUSE:Factory
Hello community, here is the log from the commit of package rubygem-js-routes for openSUSE:Factory checked in at 2016-03-18 21:39:29 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Comparing /work/SRC/openSUSE:Factory/rubygem-js-routes (Old) and /work/SRC/openSUSE:Factory/.rubygem-js-routes.new (New) ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Package is "rubygem-js-routes" Changes: -------- --- /work/SRC/openSUSE:Factory/rubygem-js-routes/rubygem-js-routes.changes 2015-09-27 08:38:57.000000000 +0200 +++ /work/SRC/openSUSE:Factory/.rubygem-js-routes.new/rubygem-js-routes.changes 2016-03-18 21:39:31.000000000 +0100 @@ -1,0 +2,42 @@ +Sun Feb 14 05:31:32 UTC 2016 - coolo@suse.com + +- updated to version 1.2.4 + see installed CHANGELOG.md + +------------------------------------------------------------------- +Thu Jan 28 05:30:45 UTC 2016 - coolo@suse.com + +- updated to version 1.2.3 + see installed CHANGELOG.md + + ## v1.2.3 + + * Sprockets ~= 3.0 support + + ## v1.2.2 + + * Sprockets ~= 3.0 support + * Support default parameters specified in route.rb file + +------------------------------------------------------------------- +Thu Jan 21 05:38:29 UTC 2016 - coolo@suse.com + +- updated to version 1.2.2 + see installed CHANGELOG.md + + ## v1.2.1 + + * Fixes for Rails 5 + + ## v1.2.0 + + * Support host, port and protocol inline parameters + * Support host, port and protocol parameters given to a route explicitly + * Remove all incompatibilities between actiondispatch and js-routes in handling route URLs + + ## v1.1.2 + + * Bugfix support nested object null parameters #164 + * Bugfix support for nested optional parameters #162 #163 + +------------------------------------------------------------------- Old: ---- js-routes-1.1.2.gem New: ---- js-routes-1.2.4.gem ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Other differences: ------------------ ++++++ rubygem-js-routes.spec ++++++ --- /var/tmp/diff_new_pack.5LskRy/_old 2016-03-18 21:39:32.000000000 +0100 +++ /var/tmp/diff_new_pack.5LskRy/_new 2016-03-18 21:39:32.000000000 +0100 @@ -1,7 +1,7 @@ # # spec file for package rubygem-js-routes # -# Copyright (c) 2015 SUSE LINUX GmbH, Nuernberg, Germany. +# Copyright (c) 2016 SUSE LINUX GmbH, Nuernberg, Germany. # # All modifications and additions to the file contributed by third parties # remain the property of their copyright owners, unless otherwise agreed @@ -24,7 +24,7 @@ # Name: rubygem-js-routes -Version: 1.1.2 +Version: 1.2.4 Release: 0 %define mod_name js-routes %define mod_full_name %{mod_name}-%{version} ++++++ js-routes-1.1.2.gem -> js-routes-1.2.4.gem ++++++ diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/.travis.yml new/.travis.yml --- old/.travis.yml 2015-09-14 16:16:33.000000000 +0200 +++ new/.travis.yml 2016-02-13 09:48:00.000000000 +0100 @@ -30,12 +30,7 @@ - master matrix: - include: - - gemfile: gemfiles/rails_edge.gemfile - rvm: 2.2 allow_failures: - rvm: jruby-19mode - rvm: ruby-head - rvm: jruby-head - - gemfile: gemfiles/rails_edge.gemfile - rvm: 2.2 diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/Appraisals new/Appraisals --- old/Appraisals 2015-09-14 16:16:33.000000000 +0200 +++ new/Appraisals 2016-02-13 09:48:00.000000000 +0100 @@ -13,9 +13,4 @@ gem "railties", "~> #{version}" gem "sprockets", "~> 3.0" end -end - -appraise "rails-edge" do - gem "railties", github: 'rails/rails' - gem "sprockets", "~> 3.0" end \ No newline at end of file diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/CHANGELOG.md new/CHANGELOG.md --- old/CHANGELOG.md 2015-09-14 16:16:33.000000000 +0200 +++ new/CHANGELOG.md 2016-02-13 09:48:00.000000000 +0100 @@ -1,5 +1,29 @@ ## master +## v1.2.3 + +* Sprockets ~= 3.0 support + +## v1.2.2 + +* Sprockets ~= 3.0 support +* Support default parameters specified in route.rb file + +## v1.2.1 + +* Fixes for Rails 5 + +## v1.2.0 + +* Support host, port and protocol inline parameters +* Support host, port and protocol parameters given to a route explicitly +* Remove all incompatibilities between actiondispatch and js-routes in handling route URLs + +## v1.1.2 + +* Bugfix support nested object null parameters #164 +* Bugfix support for nested optional parameters #162 #163 + ## v1.1.1 * Bugfix regression in serialisation on blank strings caused by [#155](https://github.com/railsware/js-routes/pull/155/files) diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/Readme.md new/Readme.md --- old/Readme.md 2015-09-14 16:16:33.000000000 +0200 +++ new/Readme.md 2016-02-13 09:48:00.000000000 +0100 @@ -11,7 +11,7 @@ gem "js-routes" ``` -### Basic Setup (Asset Pipeline) +### Basic Setup Require js routes file in `application.js` or other bundle @@ -45,8 +45,7 @@ * `default_url_options` - default parameters used when generating URLs * Note that only specific options are supported at this time. - * Supported options: `:format`, `:trailing_slash`, `:protocol`, `:host`, `:port` - * Example: {:format => "json", :trailing_slash => true, :protocol => "https", :host => "example.com", :port => 3000} + * Example: {:format => "json", :trailing\_slash => true, :protocol => "https", :host => "example.com", :port => 3000} * Default: {} * `exclude` - Array of regexps to exclude from js routes. * Default: [] @@ -65,7 +64,7 @@ * `url_links` (version >= 0.8.9) - Generate `*_url` helpers (in addition to the default `*_path` helpers). * Example: true * Default: false - * Be sure to specify a default host in `default_url_options`. Routes which specify a specific host, protocol, or port will be used instead of their corresponding default. + * Note: generated URLs will first use the protocol, host, and port options specified in the route definition. Otherwise, the URL will be based on the option specified in the `default_url_options` config. If no default option has been set, then the URL will fallback to the current URL based on `window.location`. * `compact` (version > 0.9.9) - Remove `_path` suffix in path routes(`*_url` routes stay untouched if they were enabled) * Default: false * Sample route call when option is set to true: Routes.users() => `/users` @@ -118,6 +117,7 @@ Routes.users_path() // => "/users" Routes.user_path(1) // => "/users/1" Routes.user_path(1, {format: 'json'}) // => "/users/1.json" +Routes.user_path(1, {anchor: 'profile'}) // => "/users/1#profile" Routes.new_user_project_path(1, {format: 'json'}) // => "/users/1/projects/new.json" Routes.user_project_path(1,2, {q: 'hello', custom: true}) // => "/users/1/projects/2?q=hello&custom=true" Routes.user_project_path(1,2, {hello: ['world', 'mars']}) // => "/users/1/projects/2?hello%5B%5D=world&hello%5B%5D=mars" @@ -182,7 +182,7 @@ For example create routes.js.erb in assets folder with needed content: ``` erb -<%= JsRoutes.generate({ options }) %> +<%= JsRoutes.generate(options) %> ``` This should just work. @@ -192,8 +192,9 @@ There are some alternatives available. Most of them has only basic feature and don't reach the level of quality I accept. Advantages of this one are: -* Rails3 & Rails4 support +* Rails 3-5 support * Rich options set +* Full rails compatibility * Support Rails `#to_param` convention for seo optimized paths * Well tested diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/app/assets/javascripts/js-routes.js.erb new/app/assets/javascripts/js-routes.js.erb --- old/app/assets/javascripts/js-routes.js.erb 2015-09-14 16:16:33.000000000 +0200 +++ new/app/assets/javascripts/js-routes.js.erb 2016-02-13 09:48:00.000000000 +0100 @@ -1,2 +1,3 @@ <%# encoding: UTF-8 %> +<% depend_on "#{Rails.root.join 'config', 'routes.rb'}" if JsRoutes::SPROCKETS3 %> <%= JsRoutes.assert_usable_configuration! && JsRoutes.generate %> Files old/checksums.yaml.gz and new/checksums.yaml.gz differ diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/gemfiles/rails_edge.gemfile new/gemfiles/rails_edge.gemfile --- old/gemfiles/rails_edge.gemfile 2015-09-14 16:16:33.000000000 +0200 +++ new/gemfiles/rails_edge.gemfile 1970-01-01 01:00:00.000000000 +0100 @@ -1,8 +0,0 @@ -# This file was generated by Appraisal - -source "http://rubygems.org" - -gem "railties", :github => "rails/rails" -gem "sprockets", "~> 3.0" - -gemspec :path => "../" diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/lib/js_routes/engine.rb new/lib/js_routes/engine.rb --- old/lib/js_routes/engine.rb 2015-09-14 16:16:33.000000000 +0200 +++ new/lib/js_routes/engine.rb 2016-02-13 09:48:00.000000000 +0100 @@ -1,21 +1,18 @@ +require 'sprockets/version' + class JsRoutes + SPROCKETS3 = Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new('3.0.0') class Engine < ::Rails::Engine - JS_ROUTES_ASSET = 'js-routes' initializer 'js-routes.dependent_on_routes', after: "sprockets.environment" do - routes = Rails.root.join('config', 'routes.rb').to_s if Rails.application.assets.respond_to?(:register_preprocessor) + routes = Rails.root.join('config', 'routes.rb').to_s Rails.application.assets.register_preprocessor 'application/javascript', :'js-routes_dependent_on_routes' do |ctx,data| - ctx.depend_on(routes) if ctx.logical_path == JS_ROUTES_ASSET + ctx.depend_on(routes) if ctx.logical_path == 'js-routes' data end end - - # only sprockets >= 3.0 - if Rails.application.assets.respond_to?(:depend_on) - Rails.application.assets.depend_on Rack::Utils.escape("file-digest://#{routes}") - end - end + end unless SPROCKETS3 end end diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/lib/js_routes/version.rb new/lib/js_routes/version.rb --- old/lib/js_routes/version.rb 2015-09-14 16:16:33.000000000 +0200 +++ new/lib/js_routes/version.rb 2016-02-13 09:48:00.000000000 +0100 @@ -1,3 +1,3 @@ class JsRoutes - VERSION = "1.1.2" + VERSION = "1.2.4" end diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/lib/js_routes.rb new/lib/js_routes.rb --- old/lib/js_routes.rb 2015-09-14 16:16:33.000000000 +0200 +++ new/lib/js_routes.rb 2016-02-13 09:48:00.000000000 +0100 @@ -16,7 +16,7 @@ include: //, file: DEFAULT_PATH, prefix: nil, - url_links: nil, + url_links: false, camel_case: false, default_url_options: {}, compact: false, @@ -35,6 +35,7 @@ } LAST_OPTIONS_KEY = "options".freeze + FILTERED_DEFAULT_PARTS = [:controller, :action] class Options < Struct.new(*DEFAULTS.keys) def to_hash @@ -59,7 +60,7 @@ def generate(opts = {}) # Ensure routes are loaded. If they're not, load them. - if Rails.application.routes.named_routes.routes.keys.empty? + if Rails.application.routes.named_routes.to_a.empty? Rails.application.reload_routes! end @@ -98,24 +99,40 @@ end def generate - js = File.read(File.dirname(__FILE__) + "/routes.js") - js.gsub!("GEM_VERSION", JsRoutes::VERSION) - js.gsub!("APP_CLASS", Rails.application.class.to_s) - js.gsub!("NAMESPACE", @options[:namespace]) - js.gsub!("DEFAULT_URL_OPTIONS", json(@options[:default_url_options].merge(deprecated_default_format))) - js.gsub!("PREFIX", @options[:prefix] || "") - js.gsub!("NODE_TYPES", json(NODE_TYPES)) - js.gsub!("SERIALIZER", @options[:serializer] || "null") - js.gsub!("ROUTES", js_routes) + { + "GEM_VERSION" => JsRoutes::VERSION, + "APP_CLASS" => Rails.application.class.to_s, + "NAMESPACE" => @options[:namespace], + "DEFAULT_URL_OPTIONS" => json(@options[:default_url_options].merge(deprecate_url_options)), + "PREFIX" => @options[:prefix] || "", + "NODE_TYPES" => json(NODE_TYPES), + "SERIALIZER" => @options[:serializer] || "null", + "ROUTES" => js_routes, + }.inject(File.read(File.dirname(__FILE__) + "/routes.js")) do |js, (key, value)| + js.gsub!(key, value) + end end - def deprecated_default_format + def deprecate_url_options + result = {} if @options.key?(:default_format) warn("default_format option is deprecated. Use default_url_options = { format: <format> } instead") - { format: @options[:default_format] } - else - {} + result.merge!( format: @options[:default_format] ) + end + if @options[:url_links].is_a?(String) + ActiveSupport::Deprecation.warn('js-routes url_links config value must be a boolean. Use default_url_options for specifying a default host.') + + 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? + uri = URI.parse(@options[:url_links]) + default_port = uri.scheme == "https" ? 443 : 80 + port = uri.port == default_port ? nil : uri.port + result.merge!( + host: uri.host, + port: port, + protocol: uri.scheme, + ) end + result end def generate!(file_name = nil) @@ -133,7 +150,7 @@ protected def js_routes - js_routes = Rails.application.routes.named_routes.routes.sort_by(&:to_s).flat_map do |_, route| + js_routes = Rails.application.routes.named_routes.to_a.sort_by(&:to_s).flat_map do |_, route| rails_engine_app = get_app_from_route(route) if rails_engine_app.respond_to?(:superclass) && rails_engine_app.superclass == Rails::Engine && !route.path.anchored rails_engine_app.routes.named_routes.map do |_, engine_route| @@ -173,45 +190,35 @@ def build_js(route, parent_route) name = [parent_route.try(:name), route.name].compact - parent_spec = parent_route.try(:path).try(:spec) - required_parts, optional_parts = route.required_parts.clone, (route.parts-route.required_parts) - optional_parts.push(required_parts.delete :format) if required_parts.include?(:format) route_name = generate_route_name(name, (:path unless @options[:compact])) - url_link = generate_url_link(name, route_name, required_parts, route) + parent_spec = parent_route.try(:path).try(:spec) + route_arguments = route_js_arguments(route, parent_spec) + url_link = generate_url_link(name, route_name, route_arguments, route) _ = <<-JS.strip! // #{name.join('.')} => #{parent_spec}#{route.path.spec} - // function(#{[required_parts, LAST_OPTIONS_KEY].flatten.join(', ')}) - #{route_name}: Utils.route(#{json(required_parts)}, #{json(optional_parts)}, #{json(serialize(route.path.spec, parent_spec))}, arguments)#{",\n" + url_link if url_link.length > 0} + // function(#{build_params(route.required_parts)}) + #{route_name}: Utils.route(#{route_arguments})#{",\n" + url_link if url_link.length > 0} JS end - def generate_url_link(name, route_name, required_parts, route) - return "" unless @options[:url_links] - _ = <<-JS.strip! - #{generate_route_name(name, :url)}: function(#{build_params(required_parts)}) { - return #{generate_base_url_js(route)} + this.#{route_name}(#{build_params(required_parts)}); - } - JS + def route_js_arguments(route, parent_spec) + required_parts = route.required_parts.clone + optional_parts = route.parts - required_parts + default_parts = route.defaults.reject do |part, _| + FILTERED_DEFAULT_PARTS.include?(part) + end + [ + required_parts, optional_parts, serialize(route.path.spec, parent_spec), default_parts + ].map do |argument| + json(argument) + end.join(", ") end - def generate_base_url_js(route) - # preserve and deprecate previous behavior - unless @options[:url_links] == true - ActiveSupport::Deprecation.warn('js-routes url_links config value must be a boolean. Use default_url_options for specifying a default host.') - 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? - return "#{@options[:url_links].inspect}" - else - protocol = route.defaults[:protocol] || @options[:default_url_options][:protocol] || 'http' - hostname = route.defaults[:host] || @options[:default_url_options][:host] - port = route.defaults[:port] || (@options[:default_url_options][:port] unless route.defaults[:host]) - port = ":#{port}" if port - - unless hostname - raise "A :default_url_options[:host] must be configured in order to generate *_url helpers" - end - - return %Q|'#{protocol}://#{hostname}#{port}'| - end + def generate_url_link(name, route_name, route_arguments, route) + return "" unless @options[:url_links] + <<-JS.strip! + #{generate_route_name(name, :url)}: Utils.route(#{route_arguments}, true) + JS end def generate_route_name(name, suffix) @@ -225,11 +232,7 @@ end def build_params(required_parts) - params = required_parts.map do |name| - # prepending each parameter name with underscore - # to prevent conflict with JS reserved words - "_#{name}" - end << LAST_OPTIONS_KEY + params = required_parts + [LAST_OPTIONS_KEY] params.join(", ") end diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/lib/routes.js new/lib/routes.js --- old/lib/routes.js 2015-09-14 16:16:33.000000000 +0200 +++ new/lib/routes.js 2016-02-13 09:48:00.000000000 +0100 @@ -4,8 +4,9 @@ */ (function() { - var NodeTypes, ParameterMissing, Utils, createGlobalJsRoutesObject, defaults, root, - hasProp = {}.hasOwnProperty; + var NodeTypes, ParameterMissing, ReservedOptions, Utils, createGlobalJsRoutesObject, defaults, root, + hasProp = {}.hasOwnProperty, + slice = [].slice; root = typeof exports !== "undefined" && exports !== null ? exports : this; @@ -22,6 +23,8 @@ NodeTypes = NODE_TYPES; + ReservedOptions = ['anchor', 'trailing_slash', 'host', 'port', 'protocol']; + Utils = { default_serializer: function(object, prefix) { var element, i, j, key, len, prop, s; @@ -82,48 +85,16 @@ path[last_index] = path[last_index].replace(/\/+/g, "/"); return path.join("://"); }, - set_default_url_options: function(optional_parts, options) { - var i, j, len, part, results; - results = []; - for (i = j = 0, len = optional_parts.length; j < len; i = ++j) { - part = optional_parts[i]; - if (!options.hasOwnProperty(part) && defaults.default_url_options.hasOwnProperty(part)) { - results.push(options[part] = defaults.default_url_options[part]); - } - } - return results; - }, - extract_anchor: function(options) { - var anchor; - anchor = ""; - if (options.hasOwnProperty("anchor")) { - anchor = "#" + options.anchor; - delete options.anchor; - } - return anchor; - }, - extract_trailing_slash: function(options) { - var trailing_slash; - trailing_slash = false; - if (defaults.default_url_options.hasOwnProperty("trailing_slash")) { - trailing_slash = defaults.default_url_options.trailing_slash; - } - if (options.hasOwnProperty("trailing_slash")) { - trailing_slash = options.trailing_slash; - delete options.trailing_slash; - } - return trailing_slash; - }, extract_options: function(number_of_params, args) { var last_el; last_el = args[args.length - 1]; - if (args.length > number_of_params || ((last_el != null) && "object" === this.get_object_type(last_el) && !this.look_like_serialized_model(last_el))) { - return args.pop(); + if ((args.length > number_of_params && last_el === void 0) || ((last_el != null) && "object" === this.get_object_type(last_el) && !this.looks_like_serialized_model(last_el))) { + return args.pop() || {}; } else { return {}; } }, - look_like_serialized_model: function(object) { + looks_like_serialized_model: function(object) { return "id" in object || "to_param" in object; }, path_identifier: function(object) { @@ -162,37 +133,77 @@ } return copy; }, - prepare_parameters: function(required_parameters, actual_parameters, options) { - var i, j, len, result, val; - result = this.clone(options) || {}; + merge: function() { + var tap, xs; + xs = 1 <= arguments.length ? slice.call(arguments, 0) : []; + tap = function(o, fn) { + fn(o); + return o; + }; + if ((xs != null ? xs.length : void 0) > 0) { + return tap({}, function(m) { + var j, k, len, results, v, x; + results = []; + for (j = 0, len = xs.length; j < len; j++) { + x = xs[j]; + results.push((function() { + var results1; + results1 = []; + for (k in x) { + v = x[k]; + results1.push(m[k] = v); + } + return results1; + })()); + } + return results; + }); + } + }, + normalize_options: function(default_parts, required_parameters, optional_parts, actual_parameters) { + var i, j, key, len, options, result, url_parameters, value; + options = this.extract_options(required_parameters.length, actual_parameters); + if (actual_parameters.length > required_parameters.length) { + throw new Error("Too many parameters provided for path"); + } + options = this.merge(defaults.default_url_options, default_parts, options); + result = {}; + url_parameters = {}; + result['url_parameters'] = url_parameters; + for (key in options) { + if (!hasProp.call(options, key)) continue; + value = options[key]; + if (ReservedOptions.indexOf(key) >= 0) { + result[key] = value; + } else { + url_parameters[key] = value; + } + } for (i = j = 0, len = required_parameters.length; j < len; i = ++j) { - val = required_parameters[i]; + value = required_parameters[i]; if (i < actual_parameters.length) { - result[val] = actual_parameters[i]; + url_parameters[value] = actual_parameters[i]; } } return result; }, - build_path: function(required_parameters, optional_parts, route, args) { - var anchor, opts, parameters, result, trailing_slash, url, url_params; + build_route: function(required_parameters, optional_parts, route, default_parts, full_url, args) { + var options, parameters, result, url, url_params; args = Array.prototype.slice.call(args); - opts = this.extract_options(required_parameters.length, args); - if (args.length > required_parameters.length) { - throw new Error("Too many parameters provided for path"); - } - parameters = this.prepare_parameters(required_parameters, args, opts); - this.set_default_url_options(optional_parts, parameters); - anchor = this.extract_anchor(parameters); - trailing_slash = this.extract_trailing_slash(parameters); + options = this.normalize_options(default_parts, required_parameters, optional_parts, args); + parameters = options['url_parameters']; result = "" + (this.get_prefix()) + (this.visit(route, parameters)); url = Utils.clean_path("" + result); - if (trailing_slash === true) { + if (options['trailing_slash'] === true) { url = url.replace(/(.*?)[\/]?$/, "$1/"); } if ((url_params = this.serialize(parameters)).length) { url += "?" + url_params; } - url += anchor; + url += options.anchor ? "#" + options.anchor : ""; + if (full_url) { + url = this.route_url(options) + url; + } return url; }, visit: function(route, parameters, optional) { @@ -289,10 +300,10 @@ } return prefix; }, - route: function(required_parts, optional_parts, route_spec) { + route: function(required_parts, optional_parts, route_spec, default_parts, full_url) { var path_fn; path_fn = function() { - return Utils.build_path(required_parts, optional_parts, route_spec, arguments); + return Utils.build_route(required_parts, optional_parts, route_spec, default_parts, full_url, arguments); }; path_fn.required_params = required_parts; path_fn.toString = function() { @@ -300,6 +311,41 @@ }; return path_fn; }, + route_url: function(route_defaults) { + var hostname, port, protocol; + if (typeof route_defaults === 'string') { + return route_defaults; + } + protocol = route_defaults.protocol || Utils.current_protocol(); + hostname = route_defaults.host || window.location.hostname; + port = route_defaults.port || (!route_defaults.host ? Utils.current_port() : void 0); + port = port ? ":" + port : ''; + return protocol + "://" + hostname + port; + }, + has_location: function() { + return typeof window !== 'undefined' && typeof window.location !== 'undefined'; + }, + current_host: function() { + if (this.has_location()) { + return window.location.hostname; + } else { + return null; + } + }, + current_protocol: function() { + if (this.has_location() && window.location.protocol !== '') { + return window.location.protocol.replace(/:$/, ''); + } else { + return 'http'; + } + }, + current_port: function() { + if (this.has_location() && window.location.port !== '') { + return window.location.port; + } else { + return ''; + } + }, _classToTypeCache: null, _classToType: function() { var j, len, name, ref; diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/lib/routes.js.coffee new/lib/routes.js.coffee --- old/lib/routes.js.coffee 2015-09-14 16:16:33.000000000 +0200 +++ new/lib/routes.js.coffee 2016-02-13 09:48:00.000000000 +0100 @@ -13,6 +13,14 @@ NodeTypes = NODE_TYPES +ReservedOptions = [ + 'anchor' + 'trailing_slash' + 'host' + 'port' + 'protocol' +] + Utils = default_serializer: (object, prefix = null) -> @@ -53,34 +61,14 @@ path[last_index] = path[last_index].replace(/\/+/g, "/") path.join "://" - set_default_url_options: (optional_parts, options) -> - for part, i in optional_parts when (not options.hasOwnProperty(part) and defaults.default_url_options.hasOwnProperty(part)) - options[part] = defaults.default_url_options[part] - - extract_anchor: (options) -> - anchor = "" - if options.hasOwnProperty("anchor") - anchor = "##{options.anchor}" - delete options.anchor - anchor - - extract_trailing_slash: (options) -> - trailing_slash = false - if defaults.default_url_options.hasOwnProperty("trailing_slash") - trailing_slash = defaults.default_url_options.trailing_slash - if options.hasOwnProperty("trailing_slash") - trailing_slash = options.trailing_slash - delete options.trailing_slash - trailing_slash - extract_options: (number_of_params, args) -> last_el = args[args.length - 1] - if args.length > number_of_params or (last_el? and "object" is @get_object_type(last_el) and !@look_like_serialized_model(last_el)) - args.pop() + if (args.length > number_of_params and last_el == undefined) or(last_el? and "object" is @get_object_type(last_el) and !@looks_like_serialized_model(last_el)) + args.pop() || {} else {} - look_like_serialized_model: (object) -> + looks_like_serialized_model: (object) -> # consider object a model if it have a path identifier properties like id and to_param "id" of object or "to_param" of object @@ -107,33 +95,47 @@ copy[key] = attr for own key, attr of obj copy - prepare_parameters: (required_parameters, actual_parameters, options) -> - result = @clone(options) or {} - for val, i in required_parameters when i < actual_parameters.length - result[val] = actual_parameters[i] + merge: (xs...) -> + tap = (o, fn) -> fn(o); o + if xs?.length > 0 + tap {}, (m) -> m[k] = v for k, v of x for x in xs + + normalize_options: (default_parts, required_parameters, optional_parts, actual_parameters) -> + options = @extract_options(required_parameters.length, actual_parameters) + if actual_parameters.length > required_parameters.length + throw new Error("Too many parameters provided for path") + options = @merge(defaults.default_url_options, default_parts, options) + result = {} + url_parameters = {} + result['url_parameters'] = url_parameters + for own key, value of options + if ReservedOptions.indexOf(key) >= 0 + result[key] = value + else + url_parameters[key] = value + + for value, i in required_parameters when i < actual_parameters.length + url_parameters[value] = actual_parameters[i] result - build_path: (required_parameters, optional_parts, route, args) -> + build_route: (required_parameters, optional_parts, route, default_parts, full_url, args) -> args = Array::slice.call(args) - opts = @extract_options(required_parameters.length, args) - if args.length > required_parameters.length - throw new Error("Too many parameters provided for path") - parameters = @prepare_parameters(required_parameters, args, opts) - @set_default_url_options optional_parts, parameters - # options - anchor = @extract_anchor(parameters) - trailing_slash = @extract_trailing_slash(parameters) + options = @normalize_options(default_parts, required_parameters, optional_parts, args) + parameters = options['url_parameters'] + # path result = "#{@get_prefix()}#{@visit(route, parameters)}" url = Utils.clean_path("#{result}") # set trailing_slash - url = url.replace(/(.*?)[\/]?$/, "$1/") if trailing_slash is true + url = url.replace(/(.*?)[\/]?$/, "$1/") if options['trailing_slash'] is true # set additional url params if (url_params = @serialize(parameters)).length url += "?#{url_params}" # set anchor - url += anchor + url += if options.anchor then "##{options.anchor}" else "" + if full_url + url = @route_url(options) + url url # @@ -229,12 +231,44 @@ # # route function: create route path function and add spec to it # - route: (required_parts, optional_parts, route_spec) -> - path_fn = -> Utils.build_path(required_parts, optional_parts, route_spec, arguments) + route: (required_parts, optional_parts, route_spec, default_parts, full_url) -> + path_fn = -> Utils.build_route( + required_parts, optional_parts, route_spec, default_parts, full_url, arguments + ) path_fn.required_params = required_parts path_fn.toString = -> Utils.build_path_spec(route_spec) path_fn + + route_url: (route_defaults) -> + return route_defaults if typeof route_defaults == 'string' + protocol = route_defaults.protocol || Utils.current_protocol() + hostname = route_defaults.host || window.location.hostname + port = route_defaults.port || (Utils.current_port() unless route_defaults.host) + port = if port then ":#{port}" else '' + + protocol + "://" + hostname + port + + + has_location: -> + typeof window != 'undefined' && typeof window.location != 'undefined' + + current_host: -> + if @has_location() then window.location.hostname else null + + current_protocol: () -> + if @has_location() && window.location.protocol != '' + # location.protocol includes the colon character + window.location.protocol.replace(/:$/, '') + else + 'http' + + current_port: () -> + if @has_location() && window.location.port != '' + window.location.port + else + '' + # # This is helper method to define object type. # The typeof operator is probably the biggest design flaw of JavaScript, simply because it's basically completely broken. diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/metadata new/metadata --- old/metadata 2015-09-14 16:16:33.000000000 +0200 +++ new/metadata 2016-02-13 09:48:00.000000000 +0100 @@ -1,14 +1,14 @@ --- !ruby/object:Gem::Specification name: js-routes version: !ruby/object:Gem::Version - version: 1.1.2 + version: 1.2.4 platform: ruby authors: - Bogdan Gusiev autorequire: bindir: bin cert_chain: [] -date: 2015-09-14 00:00:00.000000000 Z +date: 2016-02-13 00:00:00.000000000 Z dependencies: - !ruby/object:Gem::Dependency name: railties @@ -163,7 +163,6 @@ - gemfiles/rails41_sprockets3.gemfile - gemfiles/rails42.gemfile - gemfiles/rails42_sprockets3.gemfile -- gemfiles/rails_edge.gemfile - js-routes.gemspec - lib/js-routes.rb - lib/js_routes.rb @@ -202,7 +201,7 @@ version: '0' requirements: [] rubyforge_project: -rubygems_version: 2.4.5 +rubygems_version: 2.4.7 signing_key: specification_version: 4 summary: Brings Rails named routes to javascript diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/spec/js_routes/generated_javascript_spec.rb new/spec/js_routes/generated_javascript_spec.rb --- old/spec/js_routes/generated_javascript_spec.rb 2015-09-14 16:16:33.000000000 +0200 +++ new/spec/js_routes/generated_javascript_spec.rb 2016-02-13 09:48:00.000000000 +0100 @@ -75,6 +75,10 @@ end describe "compiled javascript asset" do + if JsRoutes::SPROCKETS3 + let(:routes_path){ "#{Rails.root.join 'config', 'routes.rb'}" } + before { expect(self).to receive(:depend_on).with routes_path } + end subject { ERB.new(File.read("app/assets/javascripts/js-routes.js.erb")).result(binding) } it "should have js routes code" do is_expected.to include("inbox_message_path: Utils.route([\"inbox_id\",\"id\"]") diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/spec/js_routes/options_spec.rb new/spec/js_routes/options_spec.rb --- old/spec/js_routes/options_spec.rb 2015-09-14 16:16:33.000000000 +0200 +++ new/spec/js_routes/options_spec.rb 2016-02-13 09:48:00.000000000 +0100 @@ -138,15 +138,18 @@ expect(evaljs("Routes.inbox_path(1, {format: null})")).to eq(routes.inbox_path(1)) end - it "shouldn't include the format when {:format => false} is specified" do - expect(evaljs("Routes.no_format_path()")).to eq(routes.no_format_path) - end it "shouldn't require the format" do + pending if Rails.version < "4.0" expect(evaljs("Routes.json_only_path()")).to eq(routes.json_only_path(:format => 'json')) end end + it "shouldn't include the format when {:format => false} is specified" do + expect(evaljs("Routes.no_format_path()")).to eq(routes.no_format_path()) + expect(evaljs("Routes.no_format_path({format: 'json'})")).to eq(routes.no_format_path(format: 'json')) + end + describe "when namespace option is specified" do let(:_options) { {:namespace => "PHM"} } it "should use this namespace for routing" do @@ -192,7 +195,6 @@ context "with required route parts" do let(:_options) { {:default_url_options => {:inbox_id => "12"}} } it "should use this opions to fill optional parameters" do - pending expect(evaljs("Routes.inbox_messages_path()")).to eq(routes.inbox_messages_path(:inbox_id => 12)) end end @@ -308,12 +310,6 @@ end context "when configuring with default_url_options" do - context "when default host is not specified" do - it "raises an error" do - expect { JsRoutes.generate({ :url_links => true }) }.to raise_error RuntimeError - end - end - context "when only host option is specified" do let(:_options) { { :url_links => true, :default_url_options => {:host => "example.com"} } } @@ -366,7 +362,7 @@ end it "does not override host, protocol, or port when host is specified in route" do - expect(evaljs("Routes.sso_url()")).to eq(routes.sso_url) + expect(evaljs("Routes.sso_url()")).to eq("http://sso.example.com:3000" + routes.sso_path) end it "does not override port when specified in route" do @@ -404,6 +400,54 @@ end end end + + context 'when window.location is present' do + let(:current_protocol) { 'http:' } # window.location.protocol includes the colon character + let(:current_hostname) { 'current.example.com' } + let(:current_port){ '' } # an empty string means port 80 + let(:current_host) do + host = "#{current_hostname}" + host += ":#{current_port}" unless current_port == '' + host + end + + before do + jscontext['window'] = { + :location => { + :protocol => current_protocol, + :hostname => current_hostname, + :port => current_port, + :host => current_host + } + } + end + + context "without specifying a default host" do + let(:_options) { { :url_links => true } } + + it "uses the current host" do + expect(evaljs("Routes.inbox_path")).not_to be_nil + expect(evaljs("Routes.inbox_url")).not_to be_nil + expect(evaljs("Routes.inbox_url(1)")).to eq("http://current.example.com#{routes.inbox_path(1)}") + expect(evaljs("Routes.inbox_url(1, { test_key: \"test_val\" })")).to eq("http://current.example.com#{routes.inbox_path(1, :test_key => "test_val")}") + expect(evaljs("Routes.new_session_url()")).to eq("https://current.example.com#{routes.new_session_path}") + expect(evaljs("Routes.sso_url()")).to eq("http://sso.example.com#{routes.sso_path}") + + end + + it "uses host option as an argument" do + expect(evaljs("Routes.portals_url({host: 'another.com'})")).to eq(routes.portals_url(host: 'another.com')) + end + + it "uses port option as an argument" do + expect(evaljs("Routes.portals_url({host: 'localhost', port: 8080})")).to eq(routes.portals_url(host: 'localhost', port: 8080)) + end + + it "uses protocol option as an argument" do + expect(evaljs("Routes.portals_url({host: 'localhost', protocol: 'https'})")).to eq(routes.portals_url(protocol: 'https', host: 'localhost')) + end + end + end end describe "when the compact mode is enabled" do diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/spec/js_routes/rails_routes_compatibility_spec.rb new/spec/js_routes/rails_routes_compatibility_spec.rb --- old/spec/js_routes/rails_routes_compatibility_spec.rb 2015-09-14 16:16:33.000000000 +0200 +++ new/spec/js_routes/rails_routes_compatibility_spec.rb 2016-02-13 09:48:00.000000000 +0100 @@ -54,6 +54,23 @@ expect(evaljs("Routes.inbox_path(1, {expanded: true, anchor: 'hello'})")).to eq(routes.inbox_path(1, :expanded => true, :anchor => "hello")) end + it "should use irregular ActiveSupport pluralizations" do + expect(evaljs("Routes.budgies_path()")).to eq(routes.budgies_path) + expect(evaljs("Routes.budgie_path(1)")).to eq(routes.budgie_path(1)) + expect(evaljs("Routes.budgy_path")).to eq(nil) + expect(evaljs("Routes.budgie_descendents_path(1)")).to eq(routes.budgie_descendents_path(1)) + end + + describe "when route has defaults" do + it "should support route default format" do + expect(evaljs("Routes.api_purchases_path()")).to eq(routes.api_purchases_path) + end + + it "should support default format override" do + expect(evaljs("Routes.api_purchases_path({format: 'xml'})")).to eq(routes.api_purchases_path(format: 'xml')) + end + end + context "with rails engines" do it "should support simple route" do expect(evaljs("Routes.blog_app_posts_path()")).to eq(blog_routes.posts_path()) @@ -284,7 +301,7 @@ end end - context "when params" do + describe "required_params" do it "should show inbox spec" do expect(evaljs("Routes.inbox_path.required_params").to_a).to eq(["id"]) end @@ -293,4 +310,6 @@ expect(evaljs("Routes.inbox_message_path.required_params").to_a).to eq(["inbox_id", "id"]) end end + + end diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/spec/js_routes/zzz_last_post_rails_init_spec.rb new/spec/js_routes/zzz_last_post_rails_init_spec.rb --- old/spec/js_routes/zzz_last_post_rails_init_spec.rb 2015-09-14 16:16:33.000000000 +0200 +++ new/spec/js_routes/zzz_last_post_rails_init_spec.rb 2016-02-13 09:48:00.000000000 +0100 @@ -55,19 +55,13 @@ FileUtils.rm_f(TEST_ASSET_PATH) end - it "should have registered a preprocessor" do - pps = Rails.application.assets.preprocessors - js_pps = pps['application/javascript'] - klass = sprockets_v3? ? 'LegacyProcProcessor' : 'Processor' - expect(js_pps.map(&:to_s)).to include("Sprockets::#{klass} (js-routes_dependent_on_routes)") - end - context "the preprocessor" do before(:each) do + path = Rails.root.join('config','routes.rb').to_s if sprockets_v3? - expect_any_instance_of(Sprockets::Context).to receive(:depend_on).with(Rails.root.join('config','routes.rb').to_s) + expect_any_instance_of(Sprockets::Context).to receive(:depend_on).with(path) else - expect(ctx).to receive(:depend_on).with(Rails.root.join('config','routes.rb').to_s) + expect(ctx).to receive(:depend_on).with(path) end end let!(:ctx) do diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/spec/spec_helper.rb new/spec/spec_helper.rb --- old/spec/spec_helper.rb 2015-09-14 16:16:33.000000000 +0200 +++ new/spec/spec_helper.rb 2016-02-13 09:48:00.000000000 +0100 @@ -42,6 +42,10 @@ BlogEngine::Engine.routes.url_helpers end +ActiveSupport::Inflector.inflections do |inflect| + inflect.irregular "budgie", "budgies" +end + module BlogEngine class Engine < Rails::Engine @@ -112,12 +116,21 @@ get '/привет' => "foo#foo", :as => :hello get '(/o/:organization)/search/:q' => "foo#foo", as: :search - + resources :sessions, :only => [:new, :create, :destroy], :protocol => 'https' get '/' => 'sso#login', host: 'sso.example.com', as: :sso resources :portals, :port => 8080 + + namespace :api, format: true, defaults: {format: 'json'} do + get "/purchases" => "purchases#index" + end + + resources :budgies do + get "descendents" + end + end end @@ -147,6 +160,22 @@ config.before :each do evaljs("var window = this;", true) - jscontext[:log] = lambda {|context, value| puts value.inspect} + + def inspectify(value) + case value + when V8::Object + value.to_h.map do |k,v| + [k, inspectify(v)] + end.to_h + when String, nil + value + else + raise "wtf #{value.class}?" + end + + end + jscontext[:log] = lambda do |context, value| + puts inspectify(value).to_json + end end end
participants (1)
-
root@hilbert.suse.de