activerecord-track-dirty-associations 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +0 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +11 -0
- data/Rakefile +2 -0
- data/activerecord-track-dirty-associations.gemspec +31 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/activerecord/track_dirty_associations.rb +122 -0
- data/lib/version.rb +5 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 587159a470a6a857fb43d0fb56bd0b9acb3c17fec0be96740ae4e4114752ff66
|
4
|
+
data.tar.gz: 1e824fc3dc61adf7f2c696ff3a2f8aab38fcc95a0a54213acd8f392c86b0e8fd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6c63e54cdf4e142c4f35a34ed5fe0b844fd8f874da632ad188d91d190814941b903caa12b0099d77a21ce8437dbd686dd5f43f7bc04e8a0d68e4035b817b3d52
|
7
|
+
data.tar.gz: 881c626861a08b50ce5034fb1820bf24c910814d1caaa5a601b5a066eb5683cdca9c966abfa0c90f686607257ec0bc7e2eb28d3bc029c59297a3278be3fe8beb
|
data/.gitignore
ADDED
File without changes
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Lynge Poulsgaard
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# README #
|
2
|
+
|
3
|
+
This is the readme for the activerecord-track-dirty-associations gem. A gem that adds the functionality to track dirty associations in activerecords.
|
4
|
+
|
5
|
+
### How do I get set up? ###
|
6
|
+
|
7
|
+
* Add the gem to your gemfile
|
8
|
+
* Add the line track_dirty_associations *association_types to the model you wish to be able to track dirty associations for
|
9
|
+
* You can now call dirty_associations? to check if tracked associations have changed, be aware that tracking an association does not automatically track that models associations. For this to work you have to add track_dirty_associations for the associations you want to track on that model as well.
|
10
|
+
* You can use dirty_associations to get all dirty associations and their changes (added, removed or changed)
|
11
|
+
* You can use {association}_changed? to see if a specific association have changed or {association}_changes to get the changes for a specific association.
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "activerecord-track-dirty-associations"
|
7
|
+
spec.version = Activerecord::TrackDirtyAssociations::VERSION
|
8
|
+
spec.authors = ["Lynge Poulsgaard"]
|
9
|
+
spec.email = ["lpo@webshipper.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{Define track_dirty_associations method}
|
12
|
+
spec.description = %q{This gem enables tracking of dirty associations on ActiveRecord objects}
|
13
|
+
spec.homepage = %q{https://webshipper.io}
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = "https://bitbucket.org/lyngekp/activerecord-track-dirty-associations"
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
spec.bindir = "exe"
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
|
28
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
29
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
30
|
+
spec.add_runtime_dependency 'activerecord', '~> 5.0'
|
31
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "activerecord/track/dirty/associations"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module TrackDirtyAssociations
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
attr_accessor :dirty_assocs
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def track_dirty_associations *association_types
|
10
|
+
klass = self
|
11
|
+
|
12
|
+
klass.send :after_save, -> { dirty_assocs = {} }
|
13
|
+
|
14
|
+
klass.singleton_class.send :alias_method, :old_has_many, :has_many
|
15
|
+
|
16
|
+
klass.singleton_class.send :define_method, :has_many do |*args, **opts|
|
17
|
+
association_type = args.first
|
18
|
+
options = opts
|
19
|
+
if association_types.empty? || association_types.include?(association_type)
|
20
|
+
create_association_methods association_type
|
21
|
+
options.merge!({
|
22
|
+
after_add: ->(object, association) { add_association association_type, object, association },
|
23
|
+
before_remove: ->(object, association) { remove_association association_type, object, association }
|
24
|
+
})
|
25
|
+
end
|
26
|
+
old_has_many *args, options
|
27
|
+
end
|
28
|
+
|
29
|
+
klass.singleton_class.send :alias_method, :old_has_one, :has_one
|
30
|
+
|
31
|
+
klass.singleton_class.send :define_method, :has_one do |*args, **opts|
|
32
|
+
association_type = args.first
|
33
|
+
options = opts
|
34
|
+
if association_types.empty? || association_types.include?(association_type)
|
35
|
+
create_association_methods association_type
|
36
|
+
end
|
37
|
+
old_has_one *args, options
|
38
|
+
end
|
39
|
+
|
40
|
+
klass.singleton_class.send :alias_method, :old_belongs_to, :belongs_to
|
41
|
+
|
42
|
+
klass.singleton_class.send :define_method, :belongs_to do |*args, **opts|
|
43
|
+
association_type = args.first
|
44
|
+
options = opts
|
45
|
+
if association_types.empty? || association_types.include?(association_type)
|
46
|
+
create_association_methods association_type
|
47
|
+
end
|
48
|
+
old_belongs_to *args, options
|
49
|
+
end
|
50
|
+
|
51
|
+
klass.singleton_class.send :define_method, :belongs_to do |*args, **opts|
|
52
|
+
association_type = args.first
|
53
|
+
options = opts
|
54
|
+
if association_types.empty? || association_types.include?(association_type)
|
55
|
+
create_association_methods association_type
|
56
|
+
end
|
57
|
+
old_belongs_to *args, options
|
58
|
+
end
|
59
|
+
|
60
|
+
define_method 'dirty_associations' do
|
61
|
+
dirty_associations = dirty_assocs || {}
|
62
|
+
association_types.each do |association_type|
|
63
|
+
dirty_associations[association_type] = association_changes(association_type) if association_changes(association_type).any?
|
64
|
+
end
|
65
|
+
dirty_associations
|
66
|
+
end
|
67
|
+
|
68
|
+
define_method 'dirty_associations?' do
|
69
|
+
send(:dirty_associations).any?
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def create_association_methods association_type
|
76
|
+
define_method "#{association_type}_changes" do
|
77
|
+
association_changes(association_type)
|
78
|
+
end
|
79
|
+
|
80
|
+
define_method "#{association_type}_changed?" do
|
81
|
+
send("#{association_type}_changes".to_sym).any?
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def add_association association_type, object, association
|
86
|
+
handle_association :added, association_type, object, association
|
87
|
+
end
|
88
|
+
|
89
|
+
def remove_association association_type, object, association
|
90
|
+
handle_association :removed, association_type, object, association
|
91
|
+
end
|
92
|
+
|
93
|
+
def handle_association action, association_type, object, association
|
94
|
+
return if object.new_record?
|
95
|
+
object.dirty_assocs = {} unless object.dirty_assocs
|
96
|
+
object.dirty_assocs[association_type] = {} unless object.dirty_assocs[association_type]
|
97
|
+
(object.dirty_assocs[association_type][action] ||= []) << association
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def association_changes association_type
|
102
|
+
association_changes = dirty_assocs&.dig(association_type) || {}
|
103
|
+
changes = []
|
104
|
+
|
105
|
+
assocs = send association_type
|
106
|
+
assocs = [assocs] unless assocs.is_a? Enumerable
|
107
|
+
assocs.each do |assoc|
|
108
|
+
assoc_changes = {}
|
109
|
+
assoc_changes.merge!(assoc.changes) if assoc.respond_to?(:changes)
|
110
|
+
assoc_changes.merge!(assoc.dirty_associations) if assoc.respond_to?(:dirty_associations)
|
111
|
+
changes << assoc_changes if assoc_changes.any?
|
112
|
+
end
|
113
|
+
|
114
|
+
association_changes[:changes] = changes if changes.any?
|
115
|
+
association_changes
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
class ActiveRecord::Base
|
121
|
+
include ActiveRecord::TrackDirtyAssociations
|
122
|
+
end
|
data/lib/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-track-dirty-associations
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lynge Poulsgaard
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-03-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activerecord
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description: This gem enables tracking of dirty associations on ActiveRecord objects
|
56
|
+
email:
|
57
|
+
- lpo@webshipper.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- activerecord-track-dirty-associations.gemspec
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
70
|
+
- lib/activerecord/track_dirty_associations.rb
|
71
|
+
- lib/version.rb
|
72
|
+
homepage: https://webshipper.io
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata:
|
76
|
+
homepage_uri: https://webshipper.io
|
77
|
+
source_code_uri: https://bitbucket.org/lyngekp/activerecord-track-dirty-associations
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubygems_version: 3.0.6
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Define track_dirty_associations method
|
97
|
+
test_files: []
|