rabl-rails 0.6.1 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4fc57874aa491be7a0c8cc778a9d4e0a307740cf4648c3733d5e49f855eb1cc4
4
- data.tar.gz: d4024078d1b780d66a297fb0a2aef0a7e37e6c4db392fa577afa1f16686c595c
3
+ metadata.gz: ac2d0bc8954f6b27e54002015d71ed0d00f4264c8c1d57411dc789216c627e44
4
+ data.tar.gz: 98757a9fe142bafb324782725e56e85e3ba83d5a018e76a75cda00c37c73ebf4
5
5
  SHA512:
6
- metadata.gz: 6b80cd63b8e17d97833f5696d04e82535c154d69b830ea291c7db344c485dead0ffb355cb75a83b1d155b992801f67815a142aa6da2657a4f7ce65820dd7d929
7
- data.tar.gz: 05f604364890ce38ab76e311d76c51ce328b2f5bcf1c389b2837f95f17b89c31b7529dcadd42e52d745d11ce1be2e74f8a5a83d2a13e3e9478185a69c5e47d23
6
+ metadata.gz: d55bc5c73b2add30025cfb41a1735548a236928ca0372c4cbc2aeec731cfbb1db60376c9498570b0ea0a40e9e4c6f79caf32cc474698ad48163845fed5da5d3a
7
+ data.tar.gz: 4bb4b91c9f4045f94dca99ec126e8fb64389736f675af3ec85466f6c92b75844e1ddc30a8b61633ddce8531ed68d2a7e3b0abf0072a61b48c803f847c6084d28
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.6.2
4
+ * Add `fetch` node
5
+
3
6
  ## 0.6.1
4
7
  * Fix bug when template contains double quotes
5
8
 
data/README.md CHANGED
@@ -196,6 +196,23 @@ end
196
196
  # => { "custom_data": [...] }
197
197
  ```
198
198
 
199
+ You can use a Hash-like data source, as long as keys match a method or attribute of your main resource, using the `fetch` keyword:
200
+
201
+ ```ruby
202
+ # assuming you have something similar in your controller
203
+ # @users_hash = { 1 => User.new(pseudo: 'Batman') }
204
+
205
+ # in the view
206
+ object :@post
207
+
208
+ fetch(:@users_hash, as: :user, field: :user_id) do
209
+ attributes :pseudo
210
+ end
211
+ # => { user: { pseudo: 'Batman' } }
212
+ ```
213
+
214
+ This comes very handy when adding attributes from external queries not really bound to a relation, like statistics.
215
+
199
216
  ### Constants
200
217
 
201
218
  Adds a new field to the response using an immutable value.
@@ -85,6 +85,21 @@ module RablRails
85
85
  @template.add_node Nodes::Glue.new(template)
86
86
  end
87
87
 
88
+ #
89
+ # Creates a node to be added to the output by fetching an object using
90
+ # current resource's field as key to the data, and appliying given
91
+ # template to said object
92
+ # Example:
93
+ # fetch(:@stats, field: :id) { attributes :total }
94
+ #
95
+ def fetch(name_or_data, options = {})
96
+ data, name = extract_data_and_name(name_or_data)
97
+ name = options[:as] if options.key?(:as)
98
+ field = options.fetch(:field, :id)
99
+ template = partial_or_block(data, options) { yield }
100
+ @template.add_node Nodes::Fetch.new(name, template, field)
101
+ end
102
+
88
103
  #
89
104
  # Creates an arbitrary node in the json output.
90
105
  # It accepts :if option to create conditionnal nodes. The current data will
@@ -171,7 +186,7 @@ module RablRails
171
186
  protected
172
187
 
173
188
  def partial_or_block(data, options)
174
- if options.key?(:partial)
189
+ if options&.key?(:partial)
175
190
  template = Library.instance.compile_template_from_path(options[:partial], @view)
176
191
  template.data = data
177
192
  template
@@ -7,3 +7,4 @@ require 'rabl-rails/nodes/condition'
7
7
  require 'rabl-rails/nodes/extend'
8
8
  require 'rabl-rails/nodes/polymorphic'
9
9
  require 'rabl-rails/nodes/lookup'
10
+ require 'rabl-rails/nodes/fetch'
@@ -0,0 +1,12 @@
1
+ module RablRails
2
+ module Nodes
3
+ class Fetch < Child
4
+ attr_reader :field
5
+
6
+ def initialize(name, template, field)
7
+ super(name, template)
8
+ @field = field
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,3 +1,3 @@
1
1
  module RablRails
2
- VERSION = '0.6.1'
2
+ VERSION = '0.6.2'
3
3
  end
@@ -39,6 +39,18 @@ module Visitors
39
39
  @_result.merge!(sub_visit(object, n.nodes)) if object
40
40
  end
41
41
 
42
+ def visit_Fetch n
43
+ hash = object_from_data(_resource, n)
44
+ key = _resource.public_send(n.field)
45
+ object = hash[key]
46
+
47
+ @_result[n.name] = if object
48
+ collection?(object) ? object.map { |o| sub_visit(o, n.nodes) } : sub_visit(object, n.nodes)
49
+ else
50
+ nil
51
+ end
52
+ end
53
+
42
54
  def visit_Code n
43
55
  if !n.condition || instance_exec(_resource, &(n.condition))
44
56
  result = instance_exec _resource, &(n.block)
@@ -180,8 +180,6 @@ class TestCompiler < Minitest::Test
180
180
  end
181
181
 
182
182
  it "compiles child with inline partial notation" do
183
-
184
-
185
183
  t = @compiler.compile_source(%{child(:user, :partial => 'user') })
186
184
  child_node = t.nodes.first
187
185
 
@@ -232,6 +230,29 @@ class TestCompiler < Minitest::Test
232
230
  assert_equal([{ :id => :id }], extract_attributes(glue_node.nodes))
233
231
  end
234
232
 
233
+ it "compiles fetch with record association" do
234
+ t = @compiler.compile_source(%{ fetch :address do attributes :foo end})
235
+
236
+ assert_equal(1, t.nodes.size)
237
+ fetch_node = t.nodes.first
238
+
239
+ assert_equal(:address, fetch_node.name)
240
+ assert_equal(:address, fetch_node.data)
241
+ assert_equal(:id, fetch_node.field)
242
+ assert_equal([{ foo: :foo }], extract_attributes(fetch_node.nodes))
243
+ end
244
+
245
+ it "compiles fetch with options" do
246
+ t = @compiler.compile_source(%{
247
+ fetch(:user, as: :author, field: :uid) do attributes :foo end
248
+ })
249
+
250
+ fetch_node = t.nodes.first
251
+ assert_equal(:author, fetch_node.name)
252
+ assert_equal(:user, fetch_node.data)
253
+ assert_equal(:uid, fetch_node.field)
254
+ end
255
+
235
256
  it "compiles constant node" do
236
257
  t = @compiler.compile_source(%{
237
258
  const(:locale, 'fr_FR')
@@ -105,6 +105,17 @@ class TestHashVisitor < Minitest::Test
105
105
  assert_equal({ name: 'Marty'}, visitor_result)
106
106
  end
107
107
 
108
+ it 'renders fetch node' do
109
+ template = RablRails::CompiledTemplate.new
110
+ template.add_node(RablRails::Nodes::Attribute.new(name: :name))
111
+ template.data = :@users_hash
112
+
113
+ @nodes << RablRails::Nodes::Fetch.new(:user, template, :id)
114
+ @context.assigns['users_hash'] = { @resource.id => @resource }
115
+
116
+ assert_equal({ user: { name: 'Marty' } }, visitor_result)
117
+ end
118
+
108
119
  describe 'with a code node' do
109
120
  before do
110
121
  @proc = ->(object) { object.name }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rabl-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Cocchi-Perrier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-05 00:00:00.000000000 Z
11
+ date: 2021-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -114,6 +114,7 @@ files:
114
114
  - lib/rabl-rails/nodes/condition.rb
115
115
  - lib/rabl-rails/nodes/const.rb
116
116
  - lib/rabl-rails/nodes/extend.rb
117
+ - lib/rabl-rails/nodes/fetch.rb
117
118
  - lib/rabl-rails/nodes/glue.rb
118
119
  - lib/rabl-rails/nodes/lookup.rb
119
120
  - lib/rabl-rails/nodes/polymorphic.rb
@@ -158,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
158
159
  - !ruby/object:Gem::Version
159
160
  version: '0'
160
161
  requirements: []
161
- rubygems_version: 3.1.4
162
+ rubygems_version: 3.0.3
162
163
  signing_key:
163
164
  specification_version: 4
164
165
  summary: Fast Rails 4+ templating system with JSON, XML and PList support