rabl 0.8.5 → 0.8.6

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.8.6
4
+
5
+ * FIX #142 #467 Do not use a collection as the default object. (Thanks @mrbrdo)
6
+ * NEW Options hash for glue function added (Thanks @sassysolutions)
7
+ * FIX #470 Better template lookups when Padrino is defined (Thanks @LTe)
8
+
3
9
  ## 0.8.5
4
10
 
5
11
  * FIX #386 Support disabling root on child calls (Thanks @digger69)
data/README.md CHANGED
@@ -298,6 +298,13 @@ child :address do
298
298
  end
299
299
  ```
300
300
 
301
+ You can also disable object root for child node:
302
+ ```ruby
303
+ child :posts, :object_root => false do
304
+ attributes :id, :title
305
+ end
306
+ ```
307
+
301
308
  You can also add child nodes from an arbitrary data source:
302
309
 
303
310
  ```ruby
@@ -541,7 +548,7 @@ node(:title) { @custom_title }
541
548
  ### Content Type Headers ###
542
549
 
543
550
  Currently in RABL, the content-type of your response is not set automatically. This is because RABL is intended
544
- to work for any Rack-based framework and as agostic to format as possible.
551
+ to work for any Rack-based framework and as agnostic to format as possible.
545
552
  Check [this issue](https://github.com/nesquena/rabl/issues/185#issuecomment-4501232) for more
546
553
  details, and if you have any ideas or patches please let me know.
547
554
 
data/lib/rabl/builder.rb CHANGED
@@ -47,7 +47,7 @@ module Rabl
47
47
  end if @options.has_key?(:child)
48
48
  # Glues
49
49
  @options[:glue].each do |settings|
50
- glue(settings[:data], &settings[:block])
50
+ glue(settings[:data], settings[:options], &settings[:block])
51
51
  end if @options.has_key?(:glue)
52
52
 
53
53
  # Wrap result in root
@@ -108,8 +108,8 @@ module Rabl
108
108
 
109
109
  # Glues data from a child node to the json_output
110
110
  # glue(@user) { attribute :full_name => :user_full_name }
111
- def glue(data, &block)
112
- return false unless data.present?
111
+ def glue(data, options={}, &block)
112
+ return false unless data.present? && resolve_condition(options)
113
113
  object = data_object(data)
114
114
  glued_attributes = self.object_to_hash(object, :root => false, &block)
115
115
  @_result.merge!(glued_attributes) if glued_attributes
data/lib/rabl/engine.rb CHANGED
@@ -183,8 +183,8 @@ module Rabl
183
183
 
184
184
  # Glues data from a child node to the json_output
185
185
  # glue(@user) { attribute :full_name => :user_full_name }
186
- def glue(data, &block)
187
- @_options[:glue].push({ :data => data, :block => block })
186
+ def glue(data, options={}, &block)
187
+ @_options[:glue].push({ :data => data, :options => options, :block => block })
188
188
  end
189
189
 
190
190
  # Extends an existing rabl template with additional attributes in the block
@@ -209,7 +209,8 @@ module Rabl
209
209
  if context_scope.respond_to?(:controller)
210
210
  controller_name = context_scope.controller.controller_name
211
211
  stripped_name = controller_name.split(%r{::|\/}).last
212
- instance_variable_get("@#{stripped_name}")
212
+ ivar_object = instance_variable_get("@#{stripped_name}")
213
+ ivar_object if is_object?(ivar_object)
213
214
  end
214
215
  end
215
216
 
data/lib/rabl/partials.rb CHANGED
@@ -32,7 +32,7 @@ module Rabl
32
32
  def fetch_source(file, options={})
33
33
  view_paths = Array(options[:view_path]) + Array(Rabl.configuration.view_paths)
34
34
  Rabl.source_cache(file, view_paths) do
35
- file_path = if defined?(Padrino) && context_scope.respond_to?(:settings)
35
+ file_path = if defined?(Padrino) && context_scope.respond_to?(:settings) && context_scope.respond_to?(:resolve_template)
36
36
  fetch_padrino_source(file, options)
37
37
  elsif defined?(Rails) && context_scope.respond_to?(:view_paths)
38
38
  _view_paths = view_paths + Array(context_scope.view_paths.to_a)
data/lib/rabl/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rabl
2
- VERSION = "0.8.5"
2
+ VERSION = "0.8.6"
3
3
  end
data/test/builder_test.rb CHANGED
@@ -137,18 +137,18 @@ context "Rabl::Builder" do
137
137
  end.equals({})
138
138
 
139
139
  asserts "that it generates the glue attributes" do
140
- b = builder :glue => [{ :data => @user, :block => lambda { |u| attribute :name }}]
140
+ b = builder :glue => [{ :data => @user, :options => {}, :block => lambda { |u| attribute :name }}]
141
141
  mock(b).object_to_hash(@user, { :root => false }).returns({:user => 'xyz'}).subject
142
142
  b.build(@user)
143
143
  end.equivalent_to({ :user => 'xyz' })
144
144
 
145
145
  asserts "that it appends the glue attributes to result" do
146
- b = builder :glue => [{ :data => @user, :block => lambda { |u| attribute :name => :user_name }}]
146
+ b = builder :glue => [{ :data => @user, :options => {}, :block => lambda { |u| attribute :name => :user_name }}]
147
147
  b.build(@user)
148
148
  end.equivalent_to({ :user_name => 'rabl' })
149
149
 
150
150
  asserts "that it does not generate new attributes if no glue attributes are present" do
151
- b = builder :glue => [{ :data => @user, :block => lambda { |u| attribute :name }}]
151
+ b = builder :glue => [{ :data => @user, :options => {}, :block => lambda { |u| attribute :name }}]
152
152
  mock(b).object_to_hash(@user,{ :root => false }).returns({}).subject
153
153
  b.build(@user)
154
154
  end.equals({})
data/test/engine_test.rb CHANGED
@@ -373,6 +373,14 @@ context "Rabl::Engine" do
373
373
  template.render(scope).split
374
374
  end.equals "{\"name\":\"rabl\"}".split
375
375
 
376
+ asserts "that it does not set a collection as default object" do
377
+ template = rabl %{
378
+ attribute :name
379
+ }
380
+ scope = context_scope('user', [])
381
+ template.render(scope).split
382
+ end.equals "{}".split
383
+
376
384
  asserts "that it sets data source" do
377
385
  template = rabl %q{
378
386
  object @user
@@ -624,6 +632,17 @@ context "Rabl::Engine" do
624
632
  scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA', :age => 12)
625
633
  JSON.parse(template.render(scope))
626
634
  end.equals JSON.parse("{\"name\":\"leo\",\"city\":\"LA\",\"age\":12}")
635
+
636
+ asserts "that it can be passed conditionals" do
637
+ template = rabl %{
638
+ object @user
639
+ attribute :name
640
+ glue(@user, {:if => lambda { |i| false }}) { attribute :age }
641
+ }
642
+ scope = Object.new
643
+ scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA', :age => 12)
644
+ JSON.parse(template.render(scope))
645
+ end.equals JSON.parse("{\"name\":\"leo\"}")
627
646
  end
628
647
 
629
648
  teardown do
@@ -89,6 +89,32 @@ context "Rabl::Partials" do
89
89
  end
90
90
  end
91
91
 
92
+ context "fetch source with custom scope" do
93
+ context "when Padrino is defined" do
94
+ helper(:tmp_path) { @tmp_path ||= Pathname.new(Dir.mktmpdir) }
95
+
96
+ setup do
97
+ ::Padrino = stub(Class.new)
98
+ Rabl.configuration.cache_sources = false
99
+ @it = TestPartial.new
100
+
101
+ def @it.context_scope; @context_scope ||= Object.new; end
102
+ context_scope = @it.context_scope
103
+ def context_scope.settings; end
104
+
105
+ File.open(tmp_path + "test.json.rabl", "w") { |f| f.puts "content" }
106
+ end
107
+
108
+ asserts('Padrino constant dont break manual lookup') do
109
+ @it.fetch_source('test', :view_path => tmp_path.to_s)
110
+ end.equals do
111
+ ["content\n", (tmp_path + "test.json.rabl").to_s ]
112
+ end
113
+
114
+ teardown { Object.send(:remove_const, :Padrino) }
115
+ end
116
+ end
117
+
92
118
  context "fetch source with Rails" do
93
119
  context "and :view_path" do
94
120
  helper(:tmp_path) { @tmp_path ||= Pathname.new(Dir.mktmpdir) }
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.5
4
+ version: 0.8.6
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-05-23 00:00:00.000000000 Z
12
+ date: 2013-06-28 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: -2073633213331132502
424
+ hash: 3931971749616157374
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: -2073633213331132502
433
+ hash: 3931971749616157374
434
434
  requirements: []
435
435
  rubyforge_project: rabl
436
436
  rubygems_version: 1.8.25