Skip to content

Commit

Permalink
Merge pull request #143 from masarakki/fix-for-sprockets-3.0.x
Browse files Browse the repository at this point in the history
fix for sprockets-3.0.x
  • Loading branch information
bogdan committed Apr 17, 2015
2 parents 12a60d5 + 777650e commit caa3aae
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 70 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Expand Up @@ -53,4 +53,8 @@ pkg
Gemfile.lock
gemfiles/*.lock

.DS_Store
.DS_Store

/spec/dummy/app/assets/javascripts/routes.js
/spec/dummy/logs
/spec/dummy/tmp
3 changes: 3 additions & 0 deletions .travis.yml
Expand Up @@ -15,6 +15,9 @@ gemfile:
- gemfiles/rails40.gemfile
- gemfiles/rails41.gemfile
- gemfiles/rails42.gemfile
- gemfiles/rails40_sprockets3.gemfile
- gemfiles/rails41_sprockets3.gemfile
- gemfiles/rails42_sprockets3.gemfile

sudo: false

Expand Down
18 changes: 9 additions & 9 deletions Appraisals
Expand Up @@ -3,14 +3,14 @@ appraise "rails32" do
gem 'tzinfo'
end

appraise "rails40" do
gem "railties", "~> 4.0.5"
end

appraise "rails41" do
gem "railties", "~> 4.1.1"
end
{rails40: '4.0.5', rails41: '4.1.1', rails42: '4.2.0'}.each do |rails, version|
appraise "#{rails}" do
gem "railties", "~> #{version}"
gem "sprockets", "< 3"
end

appraise "rails42" do
gem "railties", "~> 4.2.0"
appraise "#{rails}-sprockets3" do
gem "railties", "~> #{version}"
gem "sprockets", "~> 3.0"
end
end
1 change: 1 addition & 0 deletions gemfiles/rails40.gemfile
Expand Up @@ -3,5 +3,6 @@
source "http://rubygems.org"

gem "railties", "~> 4.0.5"
gem "sprockets", "< 3"

gemspec :path => "../"
8 changes: 8 additions & 0 deletions gemfiles/rails40_sprockets3.gemfile
@@ -0,0 +1,8 @@
# This file was generated by Appraisal

source "http://rubygems.org"

gem "railties", "~> 4.0.5"
gem "sprockets", "~> 3.0"

gemspec :path => "../"
1 change: 1 addition & 0 deletions gemfiles/rails41.gemfile
Expand Up @@ -3,5 +3,6 @@
source "http://rubygems.org"

gem "railties", "~> 4.1.1"
gem "sprockets", "< 3"

gemspec :path => "../"
8 changes: 8 additions & 0 deletions gemfiles/rails41_sprockets3.gemfile
@@ -0,0 +1,8 @@
# This file was generated by Appraisal

source "http://rubygems.org"

gem "railties", "~> 4.1.1"
gem "sprockets", "~> 3.0"

gemspec :path => "../"
1 change: 1 addition & 0 deletions gemfiles/rails42.gemfile
Expand Up @@ -3,5 +3,6 @@
source "http://rubygems.org"

gem "railties", "~> 4.2.0"
gem "sprockets", "< 3"

gemspec :path => "../"
8 changes: 8 additions & 0 deletions gemfiles/rails42_sprockets3.gemfile
@@ -0,0 +1,8 @@
# This file was generated by Appraisal

source "http://rubygems.org"

gem "railties", "~> 4.2.0"
gem "sprockets", "~> 3.0"

gemspec :path => "../"
8 changes: 7 additions & 1 deletion lib/js_routes/engine.rb
Expand Up @@ -3,13 +3,19 @@ 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')
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
data
end
end

# only sprockets >= 3.0
if Rails.application.assets.respond_to?(:depend_on)
Rails.application.assets.depend_on "file-digest://#{routes}"
end
end
end
end
56 changes: 19 additions & 37 deletions lib/routes.js
@@ -1,6 +1,6 @@
(function() {
var NodeTypes, ParameterMissing, Utils, createGlobalJsRoutesObject, defaults, root,
__hasProp = {}.hasOwnProperty;
hasProp = {}.hasOwnProperty;

root = typeof exports !== "undefined" && exports !== null ? exports : this;

Expand All @@ -19,8 +19,7 @@

Utils = {
serialize: function(object, prefix) {
var element, i, key, prop, result, s, _i, _len;

var element, i, j, key, len, prop, result, s;
if (prefix == null) {
prefix = null;
}
Expand All @@ -37,27 +36,27 @@
s = [];
switch (this.get_object_type(object)) {
case "array":
for (i = _i = 0, _len = object.length; _i < _len; i = ++_i) {
for (i = j = 0, len = object.length; j < len; i = ++j) {
element = object[i];
s.push(this.serialize(element, prefix + "[]"));
}
break;
case "object":
for (key in object) {
if (!__hasProp.call(object, key)) continue;
if (!hasProp.call(object, key)) continue;
prop = object[key];
if (!(prop != null)) {
continue;
}
if (prefix != null) {
key = "" + prefix + "[" + key + "]";
key = prefix + "[" + key + "]";
}
s.push(this.serialize(prop, key));
}
break;
default:
if (object) {
s.push("" + (encodeURIComponent(prefix.toString())) + "=" + (encodeURIComponent(object.toString())));
s.push((encodeURIComponent(prefix.toString())) + "=" + (encodeURIComponent(object.toString())));
}
}
if (!s.length) {
Expand All @@ -67,27 +66,24 @@
},
clean_path: function(path) {
var last_index;

path = path.split("://");
last_index = path.length - 1;
path[last_index] = path[last_index].replace(/\/+/g, "/");
return path.join("://");
},
set_default_url_options: function(optional_parts, options) {
var i, part, _i, _len, _results;

_results = [];
for (i = _i = 0, _len = optional_parts.length; _i < _len; i = ++_i) {
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]);
results.push(options[part] = defaults.default_url_options[part]);
}
}
return _results;
return results;
},
extract_anchor: function(options) {
var anchor;

anchor = "";
if (options.hasOwnProperty("anchor")) {
anchor = "#" + options.anchor;
Expand All @@ -97,7 +93,6 @@
},
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;
Expand All @@ -110,7 +105,6 @@
},
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();
Expand All @@ -123,7 +117,6 @@
},
path_identifier: function(object) {
var property;

if (object === 0) {
return "0";
}
Expand All @@ -147,23 +140,21 @@
},
clone: function(obj) {
var attr, copy, key;

if ((obj == null) || "object" !== this.get_object_type(obj)) {
return obj;
}
copy = obj.constructor();
for (key in obj) {
if (!__hasProp.call(obj, key)) continue;
if (!hasProp.call(obj, key)) continue;
attr = obj[key];
copy[key] = attr;
}
return copy;
},
prepare_parameters: function(required_parameters, actual_parameters, options) {
var i, result, val, _i, _len;

var i, j, len, result, val;
result = this.clone(options) || {};
for (i = _i = 0, _len = required_parameters.length; _i < _len; i = ++_i) {
for (i = j = 0, len = required_parameters.length; j < len; i = ++j) {
val = required_parameters[i];
if (i < actual_parameters.length) {
result[val] = actual_parameters[i];
Expand All @@ -173,7 +164,6 @@
},
build_path: function(required_parameters, optional_parts, route, args) {
var anchor, opts, parameters, result, trailing_slash, url, url_params;

args = Array.prototype.slice.call(args);
opts = this.extract_options(required_parameters.length, args);
if (args.length > required_parameters.length) {
Expand All @@ -196,7 +186,6 @@
},
visit: function(route, parameters, optional) {
var left, left_part, right, right_part, type, value;

if (optional == null) {
optional = false;
}
Expand Down Expand Up @@ -235,7 +224,6 @@
},
build_path_spec: function(route, wildcard) {
var left, right, type;

if (wildcard == null) {
wildcard = false;
}
Expand Down Expand Up @@ -264,7 +252,6 @@
},
visit_globbing: function(route, parameters, optional) {
var left, right, type, value;

type = route[0], left = route[1], right = route[2];
if (left.replace(/^\*/i, "") !== left) {
route[1] = left = left.replace(/^\*/i, "");
Expand All @@ -285,16 +272,14 @@
},
get_prefix: function() {
var prefix;

prefix = defaults.prefix;
if (prefix !== "") {
prefix = (prefix.match("/$") ? prefix : "" + prefix + "/");
prefix = (prefix.match("/$") ? prefix : prefix + "/");
}
return prefix;
},
route: function(required_parts, optional_parts, route_spec) {
var path_fn;

path_fn = function() {
return Utils.build_path(required_parts, optional_parts, route_spec, arguments);
};
Expand All @@ -306,15 +291,14 @@
},
_classToTypeCache: null,
_classToType: function() {
var name, _i, _len, _ref;

var j, len, name, ref;
if (this._classToTypeCache != null) {
return this._classToTypeCache;
}
this._classToTypeCache = {};
_ref = "Boolean Number String Function Array Date RegExp Object Error".split(" ");
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
name = _ref[_i];
ref = "Boolean Number String Function Array Date RegExp Object Error".split(" ");
for (j = 0, len = ref.length; j < len; j++) {
name = ref[j];
this._classToTypeCache["[object " + name + "]"] = name.toLowerCase();
}
return this._classToTypeCache;
Expand All @@ -336,10 +320,8 @@

createGlobalJsRoutesObject = function() {
var namespace;

namespace = function(mainRoot, namespaceString) {
var current, parts;

parts = (namespaceString ? namespaceString.split(".") : []);
if (!parts.length) {
return;
Expand Down
Empty file.
File renamed without changes.
6 changes: 5 additions & 1 deletion spec/js_routes/generated_javascript_spec.rb
Expand Up @@ -36,13 +36,17 @@

describe ".generate!" do

let(:name) { "#{File.dirname(__FILE__)}/../routes.js" }
let(:name) { Rails.root.join('app', 'assets', 'javascripts', 'routes.js') }

before(:each) do
FileUtils.rm_f(name)
JsRoutes.generate!({:file => name})
end

after(:each) do
FileUtils.rm_f(name)
end

after(:all) do
FileUtils.rm_f("#{File.dirname(__FILE__)}/../routes.js") # let(:name) is not available here
end
Expand Down

0 comments on commit caa3aae

Please sign in to comment.