alba 3.7.3 → 3.7.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +8 -0
- data/lib/alba/conditional_attribute.rb +2 -2
- data/lib/alba/resource.rb +5 -8
- data/lib/alba/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 19dd24ca7012ad65ea2c69fbd5f7fe9e0fe8931ac2a04320a74292d9d033943d
|
4
|
+
data.tar.gz: 95595afe6847cbaa2dde39d5d287d5d6ce82ec28182a3c12153d437dd140147e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a0ff2aa000c80343b9bd02d0cbbe8789477aa4adf1a357fac87600c0b7821f992eec1986848ce97b81c678bb5e4ecbadfa50a076eb0a015caa9fbf7a814492db
|
7
|
+
data.tar.gz: 676be0a714bf6b5325b0e6412fcce1abac2f1d517b48dde00bdc4d8ca4ce5fa10838d735cede7f893dee8291001ef8331841160a7374a02da267d87859066ff6
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
+
## 3.7.4 2025-07-24
|
10
|
+
|
11
|
+
### Fixed
|
12
|
+
|
13
|
+
- Fix trait instance method access [#452](https://github.com/okuramasafumi/alba/pull/452)
|
14
|
+
- Thank you, @ether-moon
|
15
|
+
|
9
16
|
## [3.7.3] 2025-06-28
|
10
17
|
|
11
18
|
### Fixed
|
data/README.md
CHANGED
@@ -432,6 +432,8 @@ UserResource.new(user).serialize
|
|
432
432
|
# => '{"id":1,"articles":[{"title":"Hello World!"},{"title":"Super nice"}]}'
|
433
433
|
```
|
434
434
|
|
435
|
+
#### Inline associations
|
436
|
+
|
435
437
|
You can define associations inline if you don't need a class for association.
|
436
438
|
|
437
439
|
```ruby
|
@@ -461,6 +463,8 @@ class AnotherUserResource
|
|
461
463
|
end
|
462
464
|
```
|
463
465
|
|
466
|
+
#### Filtering associations
|
467
|
+
|
464
468
|
You can "filter" association using second proc argument. This proc takes association object, `params` and initial object.
|
465
469
|
|
466
470
|
This feature is useful when you want to modify association, such as adding `includes` or `order` to ActiveRecord relations.
|
@@ -519,6 +523,8 @@ UserResource.new(user, params: {filter: :even?}).serialize
|
|
519
523
|
# => '{"id":1,"articles":[{"title":"Super nice"}]}'
|
520
524
|
```
|
521
525
|
|
526
|
+
#### Changing a key
|
527
|
+
|
522
528
|
You can change a key for association with `key` option.
|
523
529
|
|
524
530
|
```ruby
|
@@ -535,6 +541,8 @@ UserResource.new(user).serialize
|
|
535
541
|
# => '{"id":1,"my_articles":[{"title":"Hello World!"}]}'
|
536
542
|
```
|
537
543
|
|
544
|
+
#### Determining a resource for the association
|
545
|
+
|
538
546
|
You can omit the resource option if you enable Alba's [inference](#inference-configuration) feature.
|
539
547
|
|
540
548
|
```ruby
|
@@ -23,7 +23,7 @@ module Alba
|
|
23
23
|
return Alba::REMOVE_KEY unless condition_passes?(resource, object)
|
24
24
|
|
25
25
|
fetched_attribute = yield(@body)
|
26
|
-
return fetched_attribute unless with_two_arity_proc_condition
|
26
|
+
return fetched_attribute unless with_two_arity_proc_condition?
|
27
27
|
|
28
28
|
return Alba::REMOVE_KEY unless resource.instance_exec(object, second_object(object), &@condition)
|
29
29
|
|
@@ -46,7 +46,7 @@ module Alba
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
-
def with_two_arity_proc_condition
|
49
|
+
def with_two_arity_proc_condition?
|
50
50
|
@condition.is_a?(Proc) && @condition.arity >= 2
|
51
51
|
end
|
52
52
|
|
data/lib/alba/resource.rb
CHANGED
@@ -113,18 +113,15 @@ module Alba
|
|
113
113
|
private
|
114
114
|
|
115
115
|
def hash_from_traits(obj)
|
116
|
-
|
117
|
-
return h if @with_traits.nil?
|
116
|
+
return {} if @with_traits.nil?
|
118
117
|
|
119
|
-
Array(@with_traits).
|
118
|
+
Array(@with_traits).each_with_object({}) do |trait, hash|
|
120
119
|
body = @_traits.fetch(trait) { raise Alba::Error, "Trait not found: #{trait}" }
|
121
|
-
|
122
|
-
resource_class = Alba.resource_class
|
120
|
+
resource_class = Class.new(self.class)
|
123
121
|
resource_class.class_eval(&body)
|
124
122
|
resource_class.transform_keys(@_transform_type) unless @_transform_type == :none
|
125
|
-
|
123
|
+
hash.merge!(resource_class.new(obj, params: params, within: @within, select: method(:select)).serializable_hash)
|
126
124
|
end
|
127
|
-
h
|
128
125
|
end
|
129
126
|
|
130
127
|
def deprecated_serializable_hash
|
@@ -273,7 +270,7 @@ module Alba
|
|
273
270
|
when :ignore then nil
|
274
271
|
when Proc
|
275
272
|
key, value = on_error.call(error, obj, key, attribute, self.class)
|
276
|
-
hash[key] = value
|
273
|
+
hash[key] = value unless Alba::REMOVE_KEY == key # rubocop:disable Style/YodaCondition
|
277
274
|
else
|
278
275
|
# :nocov:
|
279
276
|
raise Alba::Error, 'Impossible path'
|
data/lib/alba/version.rb
CHANGED