Skip to content

Commit

Permalink
Return empty json when nil in json_safe_and_pretty
Browse files Browse the repository at this point in the history
  • Loading branch information
dzirtusss committed Apr 27, 2017
1 parent 2dcafd3 commit 4fb52e5
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Contributors: please follow the recommendations outlined at [keepachangelog.com]

## [Unreleased]
*Please add entries here for your pull requests.*
- Return empty json when nil in json_safe_and_pretty [#824](https://github.com/shakacode/react_on_rails/pull/824) by [dzirtusss](https://github.com/dzirtusss)

## [7.0.3] - 2017-04-27
Same as 7.0.1.
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/react_on_rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def server_render_js(js_expression, options = {})
end

def json_safe_and_pretty(hash_or_string)
return if hash_or_string.nil?
return "{}" if hash_or_string.nil?
unless hash_or_string.class.in?([Hash, String])
raise "#{__method__} only accepts String or Hash as argument "\
"(#{hash_or_string.class} given)."
Expand Down
4 changes: 2 additions & 2 deletions spec/dummy/spec/helpers/react_on_rails_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
expect { helper.json_safe_and_pretty(false) }.to raise_error
end

it "should allow nil values" do
it "should return empty json when nil" do
escaped_json = helper.json_safe_and_pretty(nil)
expect(escaped_json).to eq(nil)
expect(escaped_json).to eq("{}")
end

it "converts a hash to escaped JSON" do
Expand Down

0 comments on commit 4fb52e5

Please sign in to comment.