praxis 2.0.pre.26 → 2.0.pre.28
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/lib/praxis/mapper/resource.rb +20 -2
- data/lib/praxis/mapper/selector_generator.rb +21 -2
- data/lib/praxis/version.rb +1 -1
- data/spec/praxis/mapper/resource_spec.rb +22 -3
- data/spec/praxis/mapper/selector_generator_spec.rb +72 -0
- data/spec/support/spec_resources.rb +11 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 516db7752a3c4d1e8aa48abe0967781c9f6b31a65155a18285febb30c058e823
|
4
|
+
data.tar.gz: f404aaf78564a04b4835c70928584a82f3313b013c71f1d5e28e78f3e814296c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b1eae94ededb45b3cd48fdfe2440c5ac41af66b5c251e3a682906709d67a15d02d7463c7dc2e384fb4c217a745b7cec438b0d919e614f5f4c4c499a94cb17a4
|
7
|
+
data.tar.gz: fe8c7d881560b09702a4fe63680046f472efebb18aa1c09b01138b6dd9569727d29759bfc0e765095d52ec92df1c136e1de1419bf3fe282cc0b56e92cb3b1c9e
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,14 @@
|
|
1
1
|
# Praxis Changelog
|
2
2
|
|
3
3
|
## next
|
4
|
+
## 2.0.pre.28
|
5
|
+
* Enhance the mapper's Resource property to allow for a couple more powerful options using the `as:` keyword:
|
6
|
+
* `as: :self` will provide a way to map any further incoming fields on top of the already existing object. This is useful when we want to expose some properties for a resource, grouped within a sub structure, but that in reality they exist directly in the resource's underlying model (i.e., to organize the information of the model in a more structured/groupable way).
|
7
|
+
* `as: 'association1.association2'` allows us to traverse more than 1 association, and continue applying the incoming fields under that. This is commonly used when we want to expose a relationship on a resource, which is really coming from more than a single association level depth.
|
8
|
+
## 2.0.pre.27
|
9
|
+
* Introduce a new `as:` option for resource's `property`, to indicate that the underlying association method it is connected to, has a different name.
|
10
|
+
* This also will create a delegation function for the property name, that instead of calling the underlying association on the record, and wrapping the result with a resource instance, it will simply call the aliased method name (which is likely gonna hit the autogenerated code for that properyty, unless we have overriden it)
|
11
|
+
* With this change, the selector generator (i.e., the thing that looks at the incoming `fields` parameters and calculates which select and includes are necessary to query all the data we need), will be able to understand this aliasing cases, and properly pass along, and continue expanding any nested fields that are under the property name (before this, and further inner fields would be not included as soon as we hit a property that didn't have that direct association underneath).
|
4
12
|
|
5
13
|
## 2.0.pre.26
|
6
14
|
* Make POST action forwarding more robust against technically malformed GET requests with no body but passing `Content-Type`. This could cause issues when using the `enable_large_params_proxy_action` DSL.
|
@@ -60,8 +60,10 @@ module Praxis
|
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
63
|
-
|
64
|
-
|
63
|
+
# The `as:` can be used for properties that correspond to an underlying association of a different name. With this the selector generator, is able to not only add
|
64
|
+
# any extra dependencies needed for the property, but it also follow and pass any incoming nested fields when necessary (as opposed to only add dependencies and discard nested fields)
|
65
|
+
def self.property(name, dependencies: nil, through: nil, as: name) # rubocop:disable Naming/MethodParameterName
|
66
|
+
properties[name] = { dependencies: dependencies, through: through, as: as }
|
65
67
|
end
|
66
68
|
|
67
69
|
def self.batch_computed(attribute, with_instance_method: true, &block)
|
@@ -119,11 +121,27 @@ module Praxis
|
|
119
121
|
def self.define_model_accessors
|
120
122
|
return if model.nil?
|
121
123
|
|
124
|
+
define_aliased_methods
|
125
|
+
|
122
126
|
model._praxis_associations.each do |k, v|
|
123
127
|
define_model_association_accessor(k, v) unless instance_methods.include? k
|
124
128
|
end
|
125
129
|
end
|
126
130
|
|
131
|
+
def self.define_aliased_methods
|
132
|
+
with_different_alias_name = properties.reject { |name, opts| name == opts[:as] }
|
133
|
+
with_different_alias_name.each do |prop_name, opts|
|
134
|
+
next if instance_methods.include? prop_name
|
135
|
+
|
136
|
+
# Straight call to another association method (that we will generate automatically in our association accessors)
|
137
|
+
module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
138
|
+
def #{prop_name}
|
139
|
+
#{opts[:as]}
|
140
|
+
end
|
141
|
+
RUBY
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
127
145
|
def self.hookup_callbacks
|
128
146
|
return unless ancestors.include?(Praxis::Mapper::Resources::Callbacks)
|
129
147
|
|
@@ -66,8 +66,27 @@ module Praxis
|
|
66
66
|
def add_property(name, fields)
|
67
67
|
dependencies = resource.properties[name][:dependencies]
|
68
68
|
# Always add the underlying association if we're overriding the name...
|
69
|
-
praxis_compat_model = resource.model&.respond_to?(:_praxis_associations)
|
70
|
-
|
69
|
+
if (praxis_compat_model = resource.model&.respond_to?(:_praxis_associations))
|
70
|
+
aliased_as = resource.properties[name][:as]
|
71
|
+
if aliased_as == :self
|
72
|
+
# Special keyword to add itself as the association, but still continue procesing the fields
|
73
|
+
# This is useful when we expose resource fields tucked inside another sub-struct, this way
|
74
|
+
# we can make sure that if the fields necessary to compute things inside the struct, they are preloaded
|
75
|
+
add(fields)
|
76
|
+
else
|
77
|
+
first, *rest = aliased_as.to_s.split('.').map(&:to_sym)
|
78
|
+
|
79
|
+
extended_fields = \
|
80
|
+
if rest.empty?
|
81
|
+
fields
|
82
|
+
else
|
83
|
+
rest.reverse.inject(fields) do |accum, prop|
|
84
|
+
{ prop => accum }
|
85
|
+
end
|
86
|
+
end
|
87
|
+
add_association(first, extended_fields) if resource.model._praxis_associations[first]
|
88
|
+
end
|
89
|
+
end
|
71
90
|
dependencies&.each do |dependency|
|
72
91
|
# To detect recursion, let's allow mapping depending fields to the same name of the property
|
73
92
|
# but properly detecting if it's a real association...in which case we've already added it above
|
data/lib/praxis/version.rb
CHANGED
@@ -5,6 +5,8 @@ require 'spec_helper'
|
|
5
5
|
describe Praxis::Mapper::Resource do
|
6
6
|
let(:parent_record) { ParentModel.new(id: 100, name: 'george sr') }
|
7
7
|
let(:parent_records) { [ParentModel.new(id: 101, name: 'georgia'), ParentModel.new(id: 102, name: 'georgina')] }
|
8
|
+
let(:other_model) { OtherModel.new(name: 'other george') }
|
9
|
+
|
8
10
|
let(:record) { SimpleModel.new(id: 103, name: 'george xvi') }
|
9
11
|
let(:model) { SimpleModel }
|
10
12
|
|
@@ -16,15 +18,19 @@ describe Praxis::Mapper::Resource do
|
|
16
18
|
subject(:properties) { resource.properties }
|
17
19
|
|
18
20
|
it 'includes directly-set properties' do
|
19
|
-
expect(properties[:other_resource]).to eq(dependencies: [:other_model], through: nil)
|
21
|
+
expect(properties[:other_resource]).to eq(dependencies: [:other_model], through: nil, as: :other_resource)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'includes aliases as well if different from name' do
|
25
|
+
expect(properties[:aliased_association]).to eq(dependencies: [:name], through: nil, as: :other_model)
|
20
26
|
end
|
21
27
|
|
22
28
|
it 'inherits from a superclass' do
|
23
|
-
expect(properties[:href]).to eq(dependencies: [:id], through: nil)
|
29
|
+
expect(properties[:href]).to eq(dependencies: [:id], through: nil, as: :href)
|
24
30
|
end
|
25
31
|
|
26
32
|
it 'properly overrides a property from the parent' do
|
27
|
-
expect(properties[:name]).to eq(dependencies: [:simple_name], through: nil)
|
33
|
+
expect(properties[:name]).to eq(dependencies: [:simple_name], through: nil, as: :name)
|
28
34
|
end
|
29
35
|
end
|
30
36
|
end
|
@@ -106,6 +112,19 @@ describe Praxis::Mapper::Resource do
|
|
106
112
|
expect(parents.collect(&:record)).to match_array(parent_records)
|
107
113
|
end
|
108
114
|
end
|
115
|
+
|
116
|
+
context 'for aliased properties' do
|
117
|
+
before { expect(record).to receive(:other_model).and_return(other_model) }
|
118
|
+
it 'skips creation of aliased method if the resource already has it defined' do
|
119
|
+
expect(subject.overriden_aliased_association).to be_kind_of(OtherModel) # Our override return a bare model
|
120
|
+
expect(subject.method(:overriden_aliased_association).source_location).to_not include(%r{/mapper/resource.rb$}) # Not created by us
|
121
|
+
end
|
122
|
+
|
123
|
+
it 'creates the aliased method name, instead of the property name' do
|
124
|
+
expect(subject.aliased_association).to be_kind_of(OtherResource) # Calling the other generated method also wraps it
|
125
|
+
expect(subject.method(:aliased_association).source_location).to include(%r{/mapper/resource.rb$}) # created by us
|
126
|
+
end
|
127
|
+
end
|
109
128
|
end
|
110
129
|
|
111
130
|
context 'resource_delegate' do
|
@@ -213,6 +213,78 @@ describe Praxis::Mapper::SelectorGenerator do
|
|
213
213
|
end
|
214
214
|
it_behaves_like 'a proper selector'
|
215
215
|
end
|
216
|
+
context 'Aliased underlying associations follows any nested fields...' do
|
217
|
+
let(:fields) do
|
218
|
+
{
|
219
|
+
parent_id: true,
|
220
|
+
aliased_association: {
|
221
|
+
display_name: true
|
222
|
+
}
|
223
|
+
}
|
224
|
+
end
|
225
|
+
let(:selectors) do
|
226
|
+
{
|
227
|
+
model: SimpleModel,
|
228
|
+
columns: %i[other_model_id parent_id simple_name],
|
229
|
+
tracks: {
|
230
|
+
other_model: {
|
231
|
+
model: OtherModel,
|
232
|
+
columns: %i[id name]
|
233
|
+
}
|
234
|
+
}
|
235
|
+
}
|
236
|
+
end
|
237
|
+
it_behaves_like 'a proper selector'
|
238
|
+
end
|
239
|
+
context 'Deep aliased underlying associations also follows any nested fields at the end of the chain...' do
|
240
|
+
let(:fields) do
|
241
|
+
{
|
242
|
+
parent_id: true,
|
243
|
+
deep_aliased_association: {
|
244
|
+
name: true
|
245
|
+
}
|
246
|
+
}
|
247
|
+
end
|
248
|
+
let(:selectors) do
|
249
|
+
{
|
250
|
+
model: SimpleModel,
|
251
|
+
columns: %i[parent_id simple_name], # No added_column, as it does not follow the dotted reference as properties, just associations
|
252
|
+
tracks: {
|
253
|
+
parent: {
|
254
|
+
model: ParentModel,
|
255
|
+
columns: %i[id],
|
256
|
+
tracks: {
|
257
|
+
simple_children: {
|
258
|
+
model: SimpleModel,
|
259
|
+
columns: %i[parent_id simple_name]
|
260
|
+
}
|
261
|
+
}
|
262
|
+
}
|
263
|
+
}
|
264
|
+
}
|
265
|
+
end
|
266
|
+
it_behaves_like 'a proper selector'
|
267
|
+
end
|
268
|
+
context 'Using self for the underlying association: follows any nested fields skipping the association name and still applies dependencies' do
|
269
|
+
let(:fields) do
|
270
|
+
{
|
271
|
+
parent_id: true,
|
272
|
+
sub_struct: {
|
273
|
+
display_name: true
|
274
|
+
}
|
275
|
+
}
|
276
|
+
end
|
277
|
+
let(:selectors) do
|
278
|
+
{
|
279
|
+
model: SimpleModel,
|
280
|
+
# Parent_id is because we asked for it at the top
|
281
|
+
# display_name because we asked for it under sub_struct, but it is marked as :self
|
282
|
+
# alway_necessary_attribute because it is a dependency of sub_struct
|
283
|
+
columns: %i[parent_id display_name alway_necessary_attribute]
|
284
|
+
}
|
285
|
+
end
|
286
|
+
it_behaves_like 'a proper selector'
|
287
|
+
end
|
216
288
|
end
|
217
289
|
|
218
290
|
context 'string associations' do
|
@@ -125,6 +125,12 @@ class SimpleResource < BaseResource
|
|
125
125
|
other_model
|
126
126
|
end
|
127
127
|
|
128
|
+
def overriden_aliased_association
|
129
|
+
# My custom override (instead of the auto-generated delegator)
|
130
|
+
# For fun, we'll just return the raw model, without wrapping it in the resource
|
131
|
+
record.other_model
|
132
|
+
end
|
133
|
+
|
128
134
|
batch_computed(:computed_name) do |rows_by_id:|
|
129
135
|
rows_by_id.transform_values do |v|
|
130
136
|
"BATCH_COMPUTED_#{v.name}"
|
@@ -146,6 +152,11 @@ class SimpleResource < BaseResource
|
|
146
152
|
property :no_deps, dependencies: []
|
147
153
|
|
148
154
|
property :deep_nested_deps, dependencies: ['parent.simple_children.other_model.parent.display_name']
|
155
|
+
property :aliased_association, as: :other_model, dependencies: [:name]
|
156
|
+
property :overriden_aliased_association, as: :other_model, dependencies: [:name]
|
157
|
+
property :deep_aliased_association, as: 'parent.simple_children' , dependencies: [:name]
|
158
|
+
|
159
|
+
property :sub_struct, as: :self, dependencies: [:alway_necessary_attribute]
|
149
160
|
|
150
161
|
before(:update!, :do_before_update)
|
151
162
|
around(:update!, :do_around_update_nested)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: praxis
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.pre.
|
4
|
+
version: 2.0.pre.28
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josep M. Blanquer
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2023-01-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|