rabl 0.8.2 → 0.8.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +5 -0
- data/CHANGELOG.md +12 -0
- data/Gemfile +1 -0
- data/Gemfile.ci +2 -1
- data/README.md +3 -0
- data/fixtures/rails2/Gemfile +1 -0
- data/fixtures/rails2/db/schema.rb +1 -1
- data/lib/rabl/builder.rb +9 -1
- data/lib/rabl/configuration.rb +22 -20
- data/lib/rabl/engine.rb +1 -1
- data/lib/rabl/template.rb +2 -2
- data/lib/rabl/version.rb +1 -1
- data/test/builder_test.rb +13 -0
- data/test/configuration_test.rb +11 -0
- data/test/engine_test.rb +25 -0
- data/test/models/user.rb +4 -6
- metadata +4 -4
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,17 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 0.8.5
|
4
|
+
|
5
|
+
* FIX #386 Support disabling root on child calls (Thanks @digger69)
|
6
|
+
|
7
|
+
## 0.8.4
|
8
|
+
|
9
|
+
* NEW #411 Add "replace nil values with empty strings" option (Thanks @manuelmeurer)
|
10
|
+
|
11
|
+
## 0.8.3
|
12
|
+
|
13
|
+
* Closes #421 bug with locals in engine
|
14
|
+
|
3
15
|
## 0.8.2
|
4
16
|
|
5
17
|
* Passing locals when rendering templates via partials or inheritance (Thanks @simsalabim)
|
data/Gemfile
CHANGED
data/Gemfile.ci
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
|
3
3
|
# Specify your gem's dependencies in rabl.gemspec
|
4
4
|
gemspec
|
@@ -8,6 +8,7 @@ gem "i18n", '~> 0.6'
|
|
8
8
|
|
9
9
|
platforms :mri_18 do
|
10
10
|
gem 'SystemTimer'
|
11
|
+
gem 'json'
|
11
12
|
end
|
12
13
|
|
13
14
|
gem 'activesupport', :require => 'active_support'
|
data/README.md
CHANGED
@@ -137,6 +137,7 @@ Rabl.configure do |config|
|
|
137
137
|
# config.xml_options = { :dasherize => true, :skip_types => false }
|
138
138
|
# config.view_paths = []
|
139
139
|
# config.raise_on_missing_attribute = true # Defaults to false
|
140
|
+
# config.replace_nil_values_with_empty_strings = true # Defaults to false
|
140
141
|
end
|
141
142
|
```
|
142
143
|
|
@@ -171,6 +172,8 @@ attempts to render an attribute that does not exist. Otherwise, the attribute wi
|
|
171
172
|
Setting this to true during development may help increase the robustness of your code, but using `true` in
|
172
173
|
production code is not recommended.
|
173
174
|
|
175
|
+
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.
|
176
|
+
|
174
177
|
If you wish to use [oj](https://github.com/ohler55/oj) as
|
175
178
|
the primary JSON encoding engine simply add that to your Gemfile:
|
176
179
|
|
data/fixtures/rails2/Gemfile
CHANGED
@@ -4,6 +4,7 @@ gem "rails", "~> 2.3.18"
|
|
4
4
|
gem "sqlite3", :require => "sqlite3"
|
5
5
|
gem 'json'
|
6
6
|
gem 'rabl', :path => File.expand_path(File.dirname(__FILE__) + "/../../")
|
7
|
+
gem 'rake', "~> 0.9.2.2"
|
7
8
|
|
8
9
|
gem 'riot', :group => "test"
|
9
10
|
gem 'rack-test', :require => "rack/test", :group => "test"
|
@@ -9,7 +9,7 @@
|
|
9
9
|
#
|
10
10
|
# It's strongly recommended to check this file into your version control system.
|
11
11
|
|
12
|
-
ActiveRecord::Schema.define(:version =>
|
12
|
+
ActiveRecord::Schema.define(:version => 20111002092024) do
|
13
13
|
|
14
14
|
create_table "phone_numbers", :force => true do |t|
|
15
15
|
t.integer "user_id"
|
data/lib/rabl/builder.rb
CHANGED
@@ -57,6 +57,14 @@ module Rabl
|
|
57
57
|
@_root_name = nil
|
58
58
|
end
|
59
59
|
|
60
|
+
# Replace nil values with empty strings if configured
|
61
|
+
if Rabl.configuration.replace_nil_values_with_empty_strings
|
62
|
+
@_result = @_result.inject({}) do |hash, (k, v)|
|
63
|
+
hash[k] = v.nil? ? '' : v
|
64
|
+
hash
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
60
68
|
# Return Results
|
61
69
|
@_root_name ? { @_root_name => @_result } : @_result
|
62
70
|
end
|
@@ -92,7 +100,7 @@ module Rabl
|
|
92
100
|
def child(data, options={}, &block)
|
93
101
|
return false unless data.present? && resolve_condition(options)
|
94
102
|
name, object = data_name(data), data_object(data)
|
95
|
-
include_root = is_collection?(object) && @options[:child_root] # child @users
|
103
|
+
include_root = is_collection?(object) && options.fetch(:object_root, @options[:child_root]) # child @users
|
96
104
|
engine_options = @options.slice(:child_root).merge(:root => include_root)
|
97
105
|
object = { object => name } if data.respond_to?(:each_pair) && object # child :users => :people
|
98
106
|
@_result[name] = self.object_to_hash(object, engine_options, &block)
|
data/lib/rabl/configuration.rb
CHANGED
@@ -47,30 +47,32 @@ module Rabl
|
|
47
47
|
attr_accessor :cache_engine
|
48
48
|
attr_accessor :raise_on_missing_attribute
|
49
49
|
attr_accessor :perform_caching
|
50
|
+
attr_accessor :replace_nil_values_with_empty_strings
|
50
51
|
|
51
52
|
DEFAULT_XML_OPTIONS = { :dasherize => true, :skip_types => false }
|
52
53
|
|
53
54
|
def initialize
|
54
|
-
@include_json_root
|
55
|
-
@include_child_root
|
56
|
-
@include_msgpack_root
|
57
|
-
@include_plist_root
|
58
|
-
@include_xml_root
|
59
|
-
@include_bson_root
|
60
|
-
@enable_json_callbacks
|
61
|
-
@bson_check_keys
|
62
|
-
@bson_move_id
|
63
|
-
@json_engine
|
64
|
-
@msgpack_engine
|
65
|
-
@bson_engine
|
66
|
-
@plist_engine
|
67
|
-
@xml_options
|
68
|
-
@cache_sources
|
69
|
-
@cache_all_output
|
70
|
-
@escape_all_output
|
71
|
-
@view_paths
|
72
|
-
@cache_engine
|
73
|
-
@perform_caching
|
55
|
+
@include_json_root = true
|
56
|
+
@include_child_root = true
|
57
|
+
@include_msgpack_root = true
|
58
|
+
@include_plist_root = true
|
59
|
+
@include_xml_root = false
|
60
|
+
@include_bson_root = true
|
61
|
+
@enable_json_callbacks = false
|
62
|
+
@bson_check_keys = false
|
63
|
+
@bson_move_id = false
|
64
|
+
@json_engine = nil
|
65
|
+
@msgpack_engine = nil
|
66
|
+
@bson_engine = nil
|
67
|
+
@plist_engine = nil
|
68
|
+
@xml_options = {}
|
69
|
+
@cache_sources = false
|
70
|
+
@cache_all_output = false
|
71
|
+
@escape_all_output = false
|
72
|
+
@view_paths = []
|
73
|
+
@cache_engine = Rabl::CacheEngine.new
|
74
|
+
@perform_caching = false
|
75
|
+
@replace_nil_values_with_empty_strings = false
|
74
76
|
end
|
75
77
|
|
76
78
|
# @return The JSON engine used to encode Rabl templates into JSON
|
data/lib/rabl/engine.rb
CHANGED
@@ -21,9 +21,9 @@ module Rabl
|
|
21
21
|
# Rabl::Engine.new("...source...", { :format => "xml" }).render(scope, { :foo => "bar", :object => @user })
|
22
22
|
def render(scope, locals, &block)
|
23
23
|
reset_options!
|
24
|
-
locals.merge!(locals.delete(:locals) || {})
|
25
24
|
@_locals, @_scope = locals, scope
|
26
25
|
self.copy_instance_variables_from(@_scope, [:@assigns, :@helpers])
|
26
|
+
locals.merge!(locals.delete(:locals) || {})
|
27
27
|
locals.each { |k,v| instance_variable_set(:"@#{k}", v) }
|
28
28
|
@_options[:scope] = @_scope
|
29
29
|
@_options[:format] ||= self.request_format
|
data/lib/rabl/template.rb
CHANGED
@@ -20,7 +20,7 @@ if defined?(Tilt)
|
|
20
20
|
end
|
21
21
|
|
22
22
|
# Rails 2.X Template
|
23
|
-
if defined?(ActionView) && defined?(Rails) && Rails.version =~ /^2/
|
23
|
+
if defined?(ActionView) && defined?(Rails) && Rails.version.to_s =~ /^2/
|
24
24
|
require 'action_view/base'
|
25
25
|
require 'action_view/template'
|
26
26
|
|
@@ -41,7 +41,7 @@ if defined?(ActionView) && defined?(Rails) && Rails.version =~ /^2/
|
|
41
41
|
end
|
42
42
|
|
43
43
|
# Rails 3.X Template
|
44
|
-
if defined?(ActionView) && defined?(Rails) && Rails.version =~ /^[34]/
|
44
|
+
if defined?(ActionView) && defined?(Rails) && Rails.version.to_s =~ /^[34]/
|
45
45
|
module ActionView
|
46
46
|
module Template::Handlers
|
47
47
|
class Rabl
|
data/lib/rabl/version.rb
CHANGED
data/test/builder_test.rb
CHANGED
@@ -42,6 +42,19 @@ context "Rabl::Builder" do
|
|
42
42
|
topic.build(User.new, :root => false)
|
43
43
|
end.equivalent_to({ :name => "rabl" })
|
44
44
|
end
|
45
|
+
|
46
|
+
context "when nil values are replaced with empty strings" do
|
47
|
+
setup do
|
48
|
+
Rabl.configuration.replace_nil_values_with_empty_strings = true
|
49
|
+
builder({ :attributes => { :name => {} } })
|
50
|
+
end
|
51
|
+
asserts "that an empty string is returned as the value" do
|
52
|
+
topic.build(User.new(:name => nil))
|
53
|
+
end.equivalent_to({ :name => "" })
|
54
|
+
teardown do
|
55
|
+
Rabl.configuration.replace_nil_values_with_empty_strings = false
|
56
|
+
end
|
57
|
+
end
|
45
58
|
end
|
46
59
|
|
47
60
|
context "#attribute" do
|
data/test/configuration_test.rb
CHANGED
@@ -13,6 +13,7 @@ context 'Rabl::Configuration' do
|
|
13
13
|
asserts(:view_paths).equals []
|
14
14
|
asserts(:json_engine).equals { json_engine }
|
15
15
|
asserts(:cache_engine).is_a?(Rabl::CacheEngine)
|
16
|
+
asserts(:replace_nil_values_with_empty_strings).equals false
|
16
17
|
end
|
17
18
|
|
18
19
|
context 'custom JSON engine configured as Symbol' do
|
@@ -44,4 +45,14 @@ context 'Rabl::Configuration' do
|
|
44
45
|
|
45
46
|
asserts(:raise_on_missing_attribute).equals true
|
46
47
|
end # raise on missing
|
48
|
+
|
49
|
+
context 'replace nil values with empty strings' do
|
50
|
+
setup do
|
51
|
+
Rabl.configure do |c|
|
52
|
+
c.replace_nil_values_with_empty_strings = true
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
asserts(:replace_nil_values_with_empty_strings).equals true
|
57
|
+
end # replace nil values with empty strings
|
47
58
|
end
|
data/test/engine_test.rb
CHANGED
@@ -280,6 +280,31 @@ context "Rabl::Engine" do
|
|
280
280
|
scope.instance_variable_set :@user, User.new(:name => 'leo')
|
281
281
|
template.render(scope)
|
282
282
|
end.equals "{\"user\":{\"person\":{\"name\":\"leo\"}}}"
|
283
|
+
|
284
|
+
asserts "it sets root node for child collection" do
|
285
|
+
template = rabl %{
|
286
|
+
object @user
|
287
|
+
attribute :name
|
288
|
+
child(@users) { attribute :city }
|
289
|
+
}
|
290
|
+
scope = Object.new
|
291
|
+
scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
|
292
|
+
scope.instance_variable_set :@users, [User.new(:name => 'one', :city => 'UNO'), User.new(:name => 'two', :city => 'DOS')]
|
293
|
+
template.render(scope)
|
294
|
+
end.equals "{\"user\":{\"name\":\"leo\",\"users\":[{\"user\":{\"city\":\"UNO\"}},{\"user\":{\"city\":\"DOS\"}}]}}"
|
295
|
+
|
296
|
+
asserts "it allows suppression of root node for child collection" do
|
297
|
+
template = rabl %{
|
298
|
+
object @user
|
299
|
+
attribute :name
|
300
|
+
child(@users, :object_root => false) { attribute :city }
|
301
|
+
}
|
302
|
+
scope = Object.new
|
303
|
+
scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
|
304
|
+
scope.instance_variable_set :@users, [User.new(:name => 'one', :city => 'UNO'), User.new(:name => 'two', :city => 'DOS')]
|
305
|
+
template.render(scope)
|
306
|
+
end.equals "{\"user\":{\"name\":\"leo\",\"users\":[{\"city\":\"UNO\"},{\"city\":\"DOS\"}]}}"
|
307
|
+
|
283
308
|
end
|
284
309
|
|
285
310
|
context "#glue" do
|
data/test/models/user.rb
CHANGED
@@ -10,12 +10,10 @@ unless defined?(User)
|
|
10
10
|
DEFAULT_HOBBIES = ['Photography']
|
11
11
|
|
12
12
|
def initialize(attributes={})
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
self.
|
17
|
-
self.float = attributes[:float] || DEFAULT_FLOAT
|
18
|
-
self.hobbies = (attributes[:hobbies] || DEFAULT_HOBBIES).map { |h| Hobby.new(h) }
|
13
|
+
%w(age city name first float hobbies).each do |attr|
|
14
|
+
self.send "#{attr}=", (attributes.has_key?(attr.to_sym) ? attributes[attr.to_sym] : self.class.const_get("DEFAULT_#{attr.upcase}"))
|
15
|
+
end
|
16
|
+
self.hobbies = self.hobbies.map { |h| Hobby.new(h) }
|
19
17
|
end
|
20
18
|
end
|
21
19
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rabl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-05-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -421,7 +421,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
421
421
|
version: '0'
|
422
422
|
segments:
|
423
423
|
- 0
|
424
|
-
hash:
|
424
|
+
hash: -2073633213331132502
|
425
425
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
426
426
|
none: false
|
427
427
|
requirements:
|
@@ -430,7 +430,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
430
430
|
version: '0'
|
431
431
|
segments:
|
432
432
|
- 0
|
433
|
-
hash:
|
433
|
+
hash: -2073633213331132502
|
434
434
|
requirements: []
|
435
435
|
rubyforge_project: rabl
|
436
436
|
rubygems_version: 1.8.25
|