Skip to content

Commit

Permalink
Merge pull request #161 from krzysiek1507/improve_params
Browse files Browse the repository at this point in the history
Serialize object with empty string value
  • Loading branch information
bogdan committed Sep 10, 2015
2 parents 45bdbf8 + 6e766bd commit 05be832
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Based on Rails routes of APP_CLASS
if (prefix == null) {
prefix = null;
}
if (!object) {
if (object == null) {
return "";
}
if (!prefix && !(this.get_object_type(object) === "object")) {
Expand Down Expand Up @@ -56,7 +56,7 @@ Based on Rails routes of APP_CLASS
}
break;
default:
if (object) {
if (object != null) {
s.push((encodeURIComponent(prefix.toString())) + "=" + (encodeURIComponent(object.toString())));
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/routes.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ NodeTypes = NODE_TYPES
Utils =

default_serializer: (object, prefix = null) ->
return "" unless object
return "" unless object?
if !prefix and !(@get_object_type(object) is "object")
throw new Error("Url parameters should be a javascript hash")

Expand All @@ -30,7 +30,7 @@ Utils =
key = "#{prefix}[#{key}]" if prefix?
s.push @default_serializer(prop, key)
else
if object
if object?
s.push "#{encodeURIComponent(prefix.toString())}=#{encodeURIComponent(object.toString())}"

return "" unless s.length
Expand Down
4 changes: 2 additions & 2 deletions spec/js_routes/default_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
end

it "should provide this method" do
expect(evaljs("Routes.default_serializer({a: 1, b: [2,3], c: {d: 4, e: 5}})")).to eq(
"a=1&b%5B%5D=2&b%5B%5D=3&c%5Bd%5D=4&c%5Be%5D=5"
expect(evaljs("Routes.default_serializer({a: 1, b: [2,3], c: {d: 4, e: 5}, f: ''})")).to eq(
"a=1&b%5B%5D=2&b%5B%5D=3&c%5Bd%5D=4&c%5Be%5D=5&f="
)
end

Expand Down
4 changes: 4 additions & 0 deletions spec/js_routes/rails_routes_compatibility_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
expect(evaljs("Routes.json_only_path({format: 'json'})")).to eq(routes.json_only_path(:format => 'json'))
end

it "should serialize object with empty string value" do
expect(evaljs("Routes.inboxes_path({a: '', b: 1})")).to eq(routes.inboxes_path(:a => '', :b => 1))
end

it "should support utf-8 route" do
expect(evaljs("Routes.hello_path()")).to eq(routes.hello_path)
end
Expand Down

0 comments on commit 05be832

Please sign in to comment.