replace_with_destroy 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +49 -6
- data/Rakefile +0 -5
- data/lib/replace_with_destroy.rb +0 -6
- data/lib/replace_with_destroy/version.rb +1 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9651f245d829790a6af5844f5158b3a8ea96a0c2cfbc99dde6c90447aadbe6cf
|
4
|
+
data.tar.gz: 2232361b4341baf823038e34ed8ebe2d1d1c94fec8b0b1f21b7df680bddb2da3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a806b8164eff5ca9b5189529715e7646a24381268656445241ff41997a57152e4a86c89ed6dc2bd402631651fe35eda64db40cc8ced0ecabab30163db99e1ed9
|
7
|
+
data.tar.gz: 33065c4c9e87a5d2344d321f92e18e04e06e70e68aae49d0c3178d692a859f093a20faa708acf64e143a786ed8d166b8cf4a570d9f10c09963f264a985be27d6
|
data/README.md
CHANGED
@@ -1,8 +1,9 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# Replace with Destroy
|
2
|
+
Based on Rails guide [Automatic deletion of join models is direct, no destroy callbacks are triggered.](https://guides.rubyonrails.org/association_basics.html#the-has-many-through-association), which means, Rails will not run destroy callbacks for `has_many` through join model.
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
This gem allow you running the destroy callbacks in the join model.
|
5
|
+
|
6
|
+
This could be useful if you are using [paranoia](https://github.com/rubysherpas/paranoia) and you want change it to un-active instead delete the join model.
|
6
7
|
|
7
8
|
## Installation
|
8
9
|
Add this line to your application's Gemfile:
|
@@ -21,8 +22,50 @@ Or install it yourself as:
|
|
21
22
|
$ gem install replace_with_destroy
|
22
23
|
```
|
23
24
|
|
24
|
-
##
|
25
|
-
|
25
|
+
## Usage
|
26
|
+
### *replace_with_destroy* option.
|
27
|
+
Pass the option `replace_with_destroy: true` to the `has_many` method.
|
28
|
+
```
|
29
|
+
class User < ApplicationRecord
|
30
|
+
has_many :bookings
|
31
|
+
has_many :rooms, through: :bookings, replace_with_destroy: true
|
32
|
+
end
|
33
|
+
|
34
|
+
class Booking < ApplicationRecord
|
35
|
+
belongs_to :user
|
36
|
+
belongs_to :room
|
37
|
+
|
38
|
+
after_destroy :my_custom_method
|
39
|
+
|
40
|
+
def my_custom_method
|
41
|
+
# do whatever you want here ...
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class Room < ApplicationRecord
|
46
|
+
...
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
Then, given the User `user` with rooms `[1,2,3]` you can change his `rooms` and this automatically call Booking destroy callbacks.
|
51
|
+
```
|
52
|
+
# given a User with rooms
|
53
|
+
> user.room_ids
|
54
|
+
[1, 2, 3]
|
55
|
+
|
56
|
+
# you can change his rooms
|
57
|
+
user.room_ids = [3, 4, 5]
|
58
|
+
# this add the rooms 4 and 5 and remove the rooms 1 and 2
|
59
|
+
# my_custom_method will be executed for rooms 1 and 2 when being removed
|
60
|
+
```
|
61
|
+
|
62
|
+
### global *replace_with_destroy* option.
|
63
|
+
You can add this behavior for all `has_many` methods without need to pass it as option.
|
64
|
+
|
65
|
+
To do that, create an initializer, for example `config/initializers/replace_with_destroy.rb`, and put the following code
|
66
|
+
```
|
67
|
+
ReplaceWithDestroy::ALL = true
|
68
|
+
```
|
26
69
|
|
27
70
|
## License
|
28
71
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
CHANGED
data/lib/replace_with_destroy.rb
CHANGED
@@ -27,9 +27,3 @@ module ActiveRecord
|
|
27
27
|
end
|
28
28
|
|
29
29
|
ActiveRecord::Associations::Builder::Association::VALID_OPTIONS << :replace_with_destroy
|
30
|
-
|
31
|
-
p "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
|
32
|
-
p "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
|
33
|
-
p "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
|
34
|
-
p "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
|
35
|
-
p "Running Replace with destroy"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: replace_with_destroy
|
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
|
- Alexander Leyva
|
@@ -30,7 +30,8 @@ dependencies:
|
|
30
30
|
- - ">="
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: 5.0.7.2
|
33
|
-
description:
|
33
|
+
description: By default automatic deletion of join models is direct, no destroy callbacks
|
34
|
+
are triggered. This gem allow changes the default Rails behavior.
|
34
35
|
email:
|
35
36
|
- cyberxander90@gmail.com
|
36
37
|
executables: []
|
@@ -42,7 +43,7 @@ files:
|
|
42
43
|
- Rakefile
|
43
44
|
- lib/replace_with_destroy.rb
|
44
45
|
- lib/replace_with_destroy/version.rb
|
45
|
-
homepage:
|
46
|
+
homepage: https://github.com/cyberxander90/replace_with_destroy
|
46
47
|
licenses:
|
47
48
|
- MIT
|
48
49
|
metadata: {}
|
@@ -65,5 +66,5 @@ rubyforge_project:
|
|
65
66
|
rubygems_version: 2.7.6
|
66
67
|
signing_key:
|
67
68
|
specification_version: 4
|
68
|
-
summary:
|
69
|
+
summary: Call destroy callback on removed records when Rails update join models
|
69
70
|
test_files: []
|