delegate_if_nil 0.2.0 → 0.6.0
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/README.md +45 -5
- data/lib/delegate_if_nil/version.rb +1 -1
- data/lib/delegate_if_nil.rb +15 -1
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '0974b95301d59910e8fee453a67dbad18c8ae91444558952d51764a8a58ef338'
|
4
|
+
data.tar.gz: 6a431e935d57e06b902eb5e4944e8a5aa2310f9d717e1a9c5528a78600a5ca26
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa21eb61e75dccf9eb2470b1abdadd43e9f3bb5bc89cd1079adf47c5187f8ba3319facc440f12d9563cc05600e08e5ec6d6a96547c0fad30210a8557248a5e13
|
7
|
+
data.tar.gz: 7c542e65f901c3bdf7fe1f4a3a51096ba928d01453ed539a8f5cb505751a3b0117ef95489e19ff402930019a0e634ceaca7c4d904bcf96645e02f9efeced0ad6
|
data/README.md
CHANGED
@@ -17,6 +17,19 @@ end
|
|
17
17
|
|
18
18
|
`animal` will now delegate to `owning_model` if it's `nil` on an instance of `SomeModel`.
|
19
19
|
|
20
|
+
`delegate_if_nil` is available as an alias:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
class SomeModel < ApplicationRecord
|
24
|
+
extend DelegateIfNil
|
25
|
+
belongs_to :owning_model
|
26
|
+
|
27
|
+
nil_delegate :animal, to: :owning_model
|
28
|
+
delegate_if_nil :animal, to: :owning_model # Exact same as the line above
|
29
|
+
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
20
33
|
You also get `_source` methods, which will tell you where the `animal` value comes from. If it's not set on either one, it will return `"unset"`
|
21
34
|
|
22
35
|
You'll get the following results:
|
@@ -24,19 +37,21 @@ You'll get the following results:
|
|
24
37
|
```ruby
|
25
38
|
om = OwningModel.create(animal: "Cat")
|
26
39
|
|
27
|
-
some_model = SomeModel.create(animal: nil, owning_model
|
40
|
+
some_model = SomeModel.create(animal: nil, owning_model: om)
|
28
41
|
some_model.animal # "Cat"
|
29
42
|
some_model.animal_source # "owning_model"
|
43
|
+
some_model.animal_delegated? # true
|
30
44
|
|
31
|
-
some_model = SomeModel.create(animal: "Dog", owning_model
|
45
|
+
some_model = SomeModel.create(animal: "Dog", owning_model: om)
|
32
46
|
some_model.animal # Dog
|
33
47
|
some_model.animal_source # "self"
|
48
|
+
some_model.animal_delegated? # false
|
34
49
|
|
35
50
|
om = OwningModel.create(animal: nil)
|
36
|
-
some_model = SomeModel.create(animal: nil, owning_model
|
51
|
+
some_model = SomeModel.create(animal: nil, owning_model: om)
|
37
52
|
some_model.animal # nil
|
38
53
|
some_model.animal_source # "unset"
|
39
|
-
|
54
|
+
some_model.animal_delegated? # true
|
40
55
|
```
|
41
56
|
|
42
57
|
It also works for multiple attributes. EG:
|
@@ -52,11 +67,36 @@ end
|
|
52
67
|
|
53
68
|
It also resolves correctly for recursive delegations. IE: if `owning_model` delgates an attribute if `nil`, it will correctly report the source all the way down the chain, terminating either in `self`, `unset`, or the association name.
|
54
69
|
|
70
|
+
## Example
|
71
|
+
|
72
|
+
This is an example based off a real-world project.
|
73
|
+
|
74
|
+
A `User` has many `notification_channels`. Each `user` and `notification_channel` has a `notifications_enabled` boolean flag.
|
75
|
+
|
76
|
+
With this setup, a `user` can set a default value for `notifications_enabled`, and each channel can override the default.
|
77
|
+
|
78
|
+
eg:
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
class User < ActiveRecord::Base
|
82
|
+
# notifications_enabled is a boolean attribute on the user model
|
83
|
+
has_many :notification_channels
|
84
|
+
end
|
85
|
+
|
86
|
+
class NotificationChannel < ActiveRecord::Base
|
87
|
+
# notifications_enabled is a boolean attribute on the notification_channel model
|
88
|
+
belongs_to :user
|
89
|
+
nil_delegate :notifications_enabled, to: :user
|
90
|
+
end
|
91
|
+
```
|
92
|
+
|
93
|
+
Now, to see if a specific channel should be enabled, simple call `notification_channel.notifications_enabled`, and it will resolve the correct value for that channel.
|
94
|
+
|
55
95
|
## Installation
|
56
96
|
Add this line to your application's Gemfile:
|
57
97
|
|
58
98
|
```ruby
|
59
|
-
gem 'delegate_if_nil'
|
99
|
+
gem 'delegate_if_nil'
|
60
100
|
```
|
61
101
|
|
62
102
|
And then execute:
|
data/lib/delegate_if_nil.rb
CHANGED
@@ -4,12 +4,26 @@ require "delegate_if_nil/railtie"
|
|
4
4
|
|
5
5
|
module DelegateIfNil
|
6
6
|
extend ActiveSupport::Concern
|
7
|
-
|
7
|
+
|
8
8
|
def nil_delegate(*attrs, to:)
|
9
9
|
attrs.each do |attr|
|
10
10
|
define_method attr do
|
11
11
|
self[attr].nil? ? send(to)&.send(attr) : self[attr]
|
12
12
|
end
|
13
|
+
|
14
|
+
source_object_method = "#{attr}_source_object".to_sym
|
15
|
+
define_method source_object_method do
|
16
|
+
return self unless self[attr].nil?
|
17
|
+
|
18
|
+
if send(to)&.respond_to?(source_object_method)
|
19
|
+
to_source = send(to).send(source_object_method)
|
20
|
+
return to if to_source == self
|
21
|
+
|
22
|
+
return to_source
|
23
|
+
else
|
24
|
+
return send(to)&.send(attr).nil? ? nil : to
|
25
|
+
end
|
26
|
+
end
|
13
27
|
|
14
28
|
source_method = "#{attr}_source".to_sym
|
15
29
|
define_method source_method do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: delegate_if_nil
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steven Allen
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-05-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '6.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '6.0'
|
27
27
|
description: Add to a model to simply delegate attributes to an associated record
|
28
28
|
email:
|
29
29
|
- sallen@amberstyle.ca
|
@@ -42,7 +42,7 @@ homepage: https://github.com/stevenallen05/delegate_if_nil
|
|
42
42
|
licenses:
|
43
43
|
- MIT
|
44
44
|
metadata: {}
|
45
|
-
post_install_message:
|
45
|
+
post_install_message:
|
46
46
|
rdoc_options: []
|
47
47
|
require_paths:
|
48
48
|
- lib
|
@@ -57,8 +57,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
57
57
|
- !ruby/object:Gem::Version
|
58
58
|
version: '0'
|
59
59
|
requirements: []
|
60
|
-
rubygems_version: 3.
|
61
|
-
signing_key:
|
60
|
+
rubygems_version: 3.3.3
|
61
|
+
signing_key:
|
62
62
|
specification_version: 4
|
63
63
|
summary: Simple delegation of nil attributes to an association
|
64
64
|
test_files: []
|