with_record 0.1.0 → 0.1.1
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 +80 -4
- data/lib/with_record/railtie.rb +4 -4
- data/lib/with_record/version.rb +1 -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: 44c03e0867828bf1a25fd554d3890846f04b11dfe7a09dafd77af6442e243ceb
|
4
|
+
data.tar.gz: bc5bb9ae0132ea0da8d352fde7fa4828685f6caaa84611269f7688f1bf23df8a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 40f932d4c3ffa854adea095f6584488ea1e56a9c971cf95a03dd8c5d8b48c87783f19f4cdab4eb5ed7219b905ca421eddcf668999a3934e4d93df079aa65634f
|
7
|
+
data.tar.gz: 78e709238a2be3939aa156e11f168ed5d670b39a4eb8ea4f6123ed77df8e7a0c35450f2190a952b6204121a89b30526fdd6464e3d12ff13340648f4d606ac34b
|
data/README.md
CHANGED
@@ -1,8 +1,70 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# with_record
|
2
|
+
|
3
|
+
[](https://www.railsjazz.com)
|
4
|
+
[](https://www.patreon.com/igorkasyanchuk)
|
5
|
+
|
6
|
+
This gem could DRY your code and return association/relation with additioanl records. Could be used in case you have form with soft-deleted value.
|
7
|
+
|
8
|
+
**The example of problem:**
|
9
|
+
|
10
|
+
You have 2 models: User and Project. User has many projects. Project belongs to "assignee" (or author).
|
11
|
+
|
12
|
+
Let's say you have "soft-delete" (for example using paranoia" gem) and deleted user. Same time, all projects are still available (you just have such logic).
|
13
|
+
|
14
|
+
Now you want to edit the project. And in the form you have a dropdown with all active users. In this case list will be blank (because user was deleted).
|
15
|
+
|
16
|
+
So usually you need to do something like:
|
17
|
+
|
18
|
+
```slim
|
19
|
+
# just an example with problem (!!!)
|
20
|
+
= f.input :assignee_id, as: :select, collection: User.all + [User.unscoped.find_by(id: f.object.assineed_id)]
|
21
|
+
```
|
22
|
+
|
23
|
+
But now you can do the following:
|
24
|
+
```slim
|
25
|
+
# just an example with solution
|
26
|
+
= f.input :assignee_id, as: :select, collection: User.all.with_record(f.object.assineed_id)
|
27
|
+
```
|
28
|
+
|
29
|
+
And as a bonus you can also call scopes for example:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
User.all.with_record(f.object.assineed_id).ordered` # to return by first_name
|
33
|
+
```
|
34
|
+
|
35
|
+
Or
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
@project.users.with_record(42).ordered # to include user by ID
|
39
|
+
@project.users.with_records(42, 43, 44).ordered # to include user by ID's
|
40
|
+
```
|
3
41
|
|
4
42
|
## Usage
|
5
|
-
|
43
|
+
|
44
|
+
This gem is adding two methods to ActiveRecord Relations and Associations - `with_record(...)` and `with_records(...)`.
|
45
|
+
|
46
|
+
You can do the following:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
u1 = User.create(name: 'John')
|
50
|
+
u2 = User.create(name: 'Bob')
|
51
|
+
|
52
|
+
p1 = u1.projects.create(name: 'apple')
|
53
|
+
p2 = u2.projects.create(name: 'google')
|
54
|
+
p3 = u2.projects.create(name: 'amazon')
|
55
|
+
|
56
|
+
assert_equal u1.projects.with_record(p2), [p1, p2]
|
57
|
+
assert_equal u1.projects.with_records(p2, p3), [p1, p2, p3]
|
58
|
+
assert_equal u1.projects.with_records([p2, p3]), [p1, p2, p3]
|
59
|
+
|
60
|
+
assert_equal Project.where(id: p1.id).with_record(p2), [p1, p2]
|
61
|
+
assert_equal Project.where(id: p1.id).with_records(p2, p3), [p1, p2, p3]
|
62
|
+
assert_equal Project.where(id: p1.id).with_records([p2, p3]), [p1, p2, p3]
|
63
|
+
|
64
|
+
assert_equal Project.where(id: p1.id).with_record(nil), [p1]
|
65
|
+
assert_equal Project.where(id: p1.id).with_records(nil), [p1]
|
66
|
+
```
|
67
|
+
|
6
68
|
|
7
69
|
## Installation
|
8
70
|
Add this line to your application's Gemfile:
|
@@ -21,8 +83,22 @@ Or install it yourself as:
|
|
21
83
|
$ gem install with_record
|
22
84
|
```
|
23
85
|
|
86
|
+
## How it works and limitations
|
87
|
+
|
88
|
+
- it's adding 2 methods to AR Relations and Associations.
|
89
|
+
- it's executing original scope, returning and ID's (or other PK values), then adding a new ID into it and returning a new releation
|
90
|
+
- joins, includes, ... before call `.with_record` won't be returned in final scope
|
91
|
+
|
24
92
|
## Contributing
|
25
|
-
|
93
|
+
|
94
|
+
You are welcome to contribute.
|
26
95
|
|
27
96
|
## License
|
97
|
+
|
28
98
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
99
|
+
|
100
|
+
[<img src="https://github.com/igorkasyanchuk/rails_time_travel/blob/main/docs/more_gems.png?raw=true"
|
101
|
+
/>](https://www.railsjazz.com/)
|
102
|
+
|
103
|
+
[<img src="https://github.com/igorkasyanchuk/rails_time_travel/blob/main/docs/more_gems.png?raw=true"
|
104
|
+
/>](https://www.railsjazz.com/?utm_source=github&utm_medium=bottom&utm_campaign=with_record)
|
data/lib/with_record/railtie.rb
CHANGED
@@ -15,10 +15,10 @@ module WithRecord
|
|
15
15
|
end
|
16
16
|
|
17
17
|
config.after_initialize do
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
ActiveSupport.on_load(:active_record) do
|
19
|
+
ActiveRecord::Relation.send :include, Extension
|
20
|
+
ActiveRecord::Associations::CollectionProxy.send :include, Extension
|
21
|
+
end
|
22
22
|
end
|
23
23
|
end
|
24
24
|
end
|
data/lib/with_record/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: with_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Igor Kasyanchuk
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-07-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 2.0.2
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 2.0.2
|
41
41
|
description: Include more records in your relations/associations, just to DRY your
|
42
42
|
code.
|
43
43
|
email:
|
@@ -57,7 +57,7 @@ homepage: https://github.com/igorkasyanchuk/with_record
|
|
57
57
|
licenses:
|
58
58
|
- MIT
|
59
59
|
metadata: {}
|
60
|
-
post_install_message:
|
60
|
+
post_install_message:
|
61
61
|
rdoc_options: []
|
62
62
|
require_paths:
|
63
63
|
- lib
|
@@ -72,8 +72,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
72
|
- !ruby/object:Gem::Version
|
73
73
|
version: '0'
|
74
74
|
requirements: []
|
75
|
-
rubygems_version: 3.
|
76
|
-
signing_key:
|
75
|
+
rubygems_version: 3.3.7
|
76
|
+
signing_key:
|
77
77
|
specification_version: 4
|
78
78
|
summary: Include more records in your relations/associations, just to DRY your code.
|
79
79
|
test_files: []
|