Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace nil values with empty strings #411

Merged
merged 2 commits into from Apr 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ Rabl.configure do |config|
# config.xml_options = { :dasherize => true, :skip_types => false }
# config.view_paths = []
# config.raise_on_missing_attribute = true # Defaults to false
# config.replace_nil_values_with_empty_strings = true # Defaults to false
end
```

Expand Down Expand Up @@ -171,6 +172,8 @@ attempts to render an attribute that does not exist. Otherwise, the attribute wi
Setting this to true during development may help increase the robustness of your code, but using `true` in
production code is not recommended.

If `replace_nil_values_with_empty_strings` is set to `true`, all values that are `nil` and would normally be displayed as `null` in the response are converted to empty strings.

If you wish to use [oj](https://github.com/ohler55/oj) as
the primary JSON encoding engine simply add that to your Gemfile:

Expand Down
8 changes: 8 additions & 0 deletions lib/rabl/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ def compile_hash(options={})
@_root_name = nil
end

# Replace nil values with empty strings if configured
if Rabl.configuration.replace_nil_values_with_empty_strings
@_result = @_result.inject({}) do |hash, (k, v)|
hash[k] = v.nil? ? '' : v
hash
end
end

# Return Results
@_root_name ? { @_root_name => @_result } : @_result
end
Expand Down
42 changes: 22 additions & 20 deletions lib/rabl/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,32 @@ class Configuration
attr_accessor :cache_engine
attr_accessor :raise_on_missing_attribute
attr_accessor :perform_caching
attr_accessor :replace_nil_values_with_empty_strings

DEFAULT_XML_OPTIONS = { :dasherize => true, :skip_types => false }

def initialize
@include_json_root = true
@include_child_root = true
@include_msgpack_root = true
@include_plist_root = true
@include_xml_root = false
@include_bson_root = true
@enable_json_callbacks = false
@bson_check_keys = false
@bson_move_id = false
@json_engine = nil
@msgpack_engine = nil
@bson_engine = nil
@plist_engine = nil
@xml_options = {}
@cache_sources = false
@cache_all_output = false
@escape_all_output = false
@view_paths = []
@cache_engine = Rabl::CacheEngine.new
@perform_caching = false
@include_json_root = true
@include_child_root = true
@include_msgpack_root = true
@include_plist_root = true
@include_xml_root = false
@include_bson_root = true
@enable_json_callbacks = false
@bson_check_keys = false
@bson_move_id = false
@json_engine = nil
@msgpack_engine = nil
@bson_engine = nil
@plist_engine = nil
@xml_options = {}
@cache_sources = false
@cache_all_output = false
@escape_all_output = false
@view_paths = []
@cache_engine = Rabl::CacheEngine.new
@perform_caching = false
@replace_nil_values_with_empty_strings = false
end

# @return The JSON engine used to encode Rabl templates into JSON
Expand Down
13 changes: 13 additions & 0 deletions test/builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@
topic.build(User.new, :root => false)
end.equivalent_to({ :name => "rabl" })
end

context "when nil values are replaced with empty strings" do
setup do
Rabl.configuration.replace_nil_values_with_empty_strings = true
builder({ :attributes => { :name => {} } })
end
asserts "that an empty string is returned as the value" do
topic.build(User.new(:name => nil))
end.equivalent_to({ :name => "" })
teardown do
Rabl.configuration.replace_nil_values_with_empty_strings = false
end
end
end

context "#attribute" do
Expand Down
11 changes: 11 additions & 0 deletions test/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
asserts(:view_paths).equals []
asserts(:json_engine).equals { json_engine }
asserts(:cache_engine).is_a?(Rabl::CacheEngine)
asserts(:replace_nil_values_with_empty_strings).equals false
end

context 'custom JSON engine configured as Symbol' do
Expand Down Expand Up @@ -44,4 +45,14 @@

asserts(:raise_on_missing_attribute).equals true
end # raise on missing

context 'replace nil values with empty strings' do
setup do
Rabl.configure do |c|
c.replace_nil_values_with_empty_strings = true
end
end

asserts(:replace_nil_values_with_empty_strings).equals true
end # replace nil values with empty strings
end
10 changes: 4 additions & 6 deletions test/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ class User
DEFAULT_HOBBIES = ['Photography']

def initialize(attributes={})
self.age = attributes[:age] || DEFAULT_AGE
self.city = attributes[:city] || DEFAULT_CITY
self.name = attributes[:name] || DEFAULT_NAME
self.first = attributes[:first] || DEFAULT_FIRST
self.float = attributes[:float] || DEFAULT_FLOAT
self.hobbies = (attributes[:hobbies] || DEFAULT_HOBBIES).map { |h| Hobby.new(h) }
%w(age city name first float hobbies).each do |attr|
self.send "#{attr}=", (attributes.has_key?(attr.to_sym) ? attributes[attr.to_sym] : self.class.const_get("DEFAULT_#{attr.upcase}"))
end
self.hobbies = self.hobbies.map { |h| Hobby.new(h) }
end
end

Expand Down