mongoid_extended_dirty_trackable 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +15 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +87 -0
- data/Rakefile +6 -0
- data/TODO +9 -0
- data/bin/console +11 -0
- data/bin/setup +7 -0
- data/lib/mongoid/extended_dirty_trackable.rb +55 -0
- data/lib/mongoid/extended_dirty_trackable/version.rb +5 -0
- data/lib/mongoid_extended_dirty_trackable.rb +2 -0
- data/mongoid_extended_dirty_trackable.gemspec +31 -0
- metadata +151 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 913f8c7ecec74560dba0dd1eeabfd2e1851a742e
|
4
|
+
data.tar.gz: 8b724dca2a098edce9f7f95b5e981cc98deafb32
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: afda3e2dc4e579511ecc1a46f7f3b9f6648f5c7e3037ba88c661977c7ba5b36c32df26874becd2e564f4d8da7e50c1bc832220e7d27a7c2ed3c162dd7dcae453
|
7
|
+
data.tar.gz: 1789a15154df9771ced0e97910578ec2584e27d0f83e259f8c8cca28ff7a939146f4c2c273dcf3eb9223419d29f18afe958f5cfb9cbc8a5c06cf88f4e2bc6630
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Thomas Graves
|
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,87 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/graves/mongoid_extended_dirty_trackable.svg?branch=master)](https://travis-ci.org/graves/mongoid_extended_dirty_trackable)
|
2
|
+
|
3
|
+
# Mongoid::ExtendedDirtyTrackable
|
4
|
+
|
5
|
+
A Mongoid Extension that gives you the ability to track changes to embedded and related documents through a parent.
|
6
|
+
|
7
|
+
It was born from a need in a production app I work on from which it was extracted. If you'd like a pretty detailed run down of what exactly the code is doing you can find it on [my blog.](http://blog.ooo.pm/dirty-tracking-embedded-documents-with-mongoid/)
|
8
|
+
|
9
|
+
I don't consider this Gem production ready. Mostly because I ran into some issues when writing the specs after Gemifying the original Concern. None of these problems exist in my production app so I'm lead to believe they are the result of the _hack_ job I did setting up and tearing down mongo for the tests. I plan on working these issues out in the near future and if you'd like to know more check out my [blog post that covers them.](http://blog.ooo.pm/post-im-gonna-write-tomorrow-morning)
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'mongoid_extended_dirty_trackable'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself with:
|
24
|
+
|
25
|
+
$ gem install mongoid_extended_dirty_trackable
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
I tried to make the specs work as documentation but for those of you averse to reading them I'll make it easy for you.
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
class Account
|
33
|
+
include Mongoid::Document
|
34
|
+
include Mongoid::ExtendedDirtyTrackable
|
35
|
+
|
36
|
+
field :name
|
37
|
+
|
38
|
+
embeds_one :address
|
39
|
+
embeds_many :invoices
|
40
|
+
has_many :offices
|
41
|
+
end
|
42
|
+
|
43
|
+
account = Account.create(name: "Prestige Worldwide")
|
44
|
+
account.name = "Umbrella Corp"
|
45
|
+
account.changed? #=> true
|
46
|
+
account.changes["name"] #=> ["Prestige Worldwide", "Umbrella Corp"]
|
47
|
+
|
48
|
+
account.create_address(zipcode: "90210")
|
49
|
+
account.address.zipcode = "1000 AS"
|
50
|
+
account.changed? #=> true
|
51
|
+
account.changes["zipcode"] #=> ["90210", "1000 AS"]
|
52
|
+
|
53
|
+
account.invoices.create(total: 420.00)
|
54
|
+
account.invoices.first.total = 69.69
|
55
|
+
account.changed? #=> true
|
56
|
+
account.changes["total"] #=> [420.00, 69.69]
|
57
|
+
|
58
|
+
office = account.offices.create(number: 666)
|
59
|
+
office.number = 5446
|
60
|
+
|
61
|
+
account.changed? #=> true
|
62
|
+
account.changes["number"] #=> [666, 5446]
|
63
|
+
```
|
64
|
+
|
65
|
+
## Development
|
66
|
+
|
67
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
68
|
+
|
69
|
+
## Contributing
|
70
|
+
|
71
|
+
All pull requests are met with open arms and gratitude.
|
72
|
+
|
73
|
+
Check the TODO file or the Github Issue tracker for suggestions on where to begin.
|
74
|
+
|
75
|
+
Please be sure your pull request includes descriptive commit messages and tests that cover your feature, change, or bug.
|
76
|
+
|
77
|
+
1. Fork it ( https://github.com/graves/mongoid_extended_dirty_trackable/fork )
|
78
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
79
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
80
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
81
|
+
5. Create a new Pull Request
|
82
|
+
|
83
|
+
## Similar Projects
|
84
|
+
|
85
|
+
[versative/mongoid_relations_dirty_tracking](https://github.com/versative/mongoid_relations_dirty_tracking) - This is a little more full featured and much more in the direction I plan on taking but I feel like it can be done in a more simple manner with less code.
|
86
|
+
|
87
|
+
[millisami/gist:721466](https://gist.github.com/millisami/721466) - The code in this Gist no longer works or maybe it never did but it served as the inspiration for my Gem. Thanks [millisami!](https://github.com/millisami)
|
data/Rakefile
ADDED
data/TODO
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
- lib/mongoid/extended_dirty_trackable.rb could use some refactoring, at least a few method extractions
|
2
|
+
- change @associated_changes and @embedded_changes back to being conditional assignments
|
3
|
+
- doing the above should fix the infinite recursion bug that happens when two related classes both include the mixin
|
4
|
+
- figure out why and fix #associated_changes and #embedded_changes returning only empty hashes when their instance variables set by conditional assignment
|
5
|
+
- REMEMBER: ^ empty hashes are not falsey
|
6
|
+
- setup a proper mongo instance (using the yaml config file) for tests
|
7
|
+
- use DatabaseCleaner to clear out the db before each test
|
8
|
+
- the above 2 might be the fix the empty hash on conditional assignment bug mentioned above
|
9
|
+
- maybe include object id and relationship name in the changes hash or implement in seperate methods
|
data/bin/console
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mongoid"
|
5
|
+
require "mongoid_extended_dirty_trackable"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
require "pry"
|
11
|
+
Pry.start
|
data/bin/setup
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'mongoid'
|
2
|
+
require 'active_support/concern'
|
3
|
+
|
4
|
+
module Mongoid
|
5
|
+
module ExtendedDirtyTrackable
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
attr_writer :embedded_changes, :associated_changes
|
10
|
+
end
|
11
|
+
|
12
|
+
def associated_changes
|
13
|
+
@associated_changes = begin
|
14
|
+
self.associations.keys.inject({}) do |memo, association|
|
15
|
+
_changes = msg_relative(association)
|
16
|
+
memo.merge!(_changes)
|
17
|
+
memo
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def msg_relative(relationship)
|
23
|
+
relative = self.send(relationship)
|
24
|
+
|
25
|
+
if relative && !relative.is_a?(Array) && relative.changed?
|
26
|
+
_changes = relative.changes
|
27
|
+
elsif relative && relative.is_a?(Array) && relative.any?(&:changed)
|
28
|
+
_changes = relative.inject({}) do |memo, obj|
|
29
|
+
memo.merge!(obj.changes) if obj.changed?
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
_changes || {}
|
34
|
+
end
|
35
|
+
|
36
|
+
def embedded_changes
|
37
|
+
@embedded_changes = begin
|
38
|
+
self.collect_children.inject({}) do |memo, child|
|
39
|
+
memo.merge!(child.changes) if child.changed?
|
40
|
+
memo
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def changes
|
46
|
+
from_super = super
|
47
|
+
from_super.merge!(associated_changes)
|
48
|
+
from_super.merge!(embedded_changes)
|
49
|
+
end
|
50
|
+
|
51
|
+
def changed?
|
52
|
+
super || self._children.any?(&:changed?)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mongoid/extended_dirty_trackable/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mongoid_extended_dirty_trackable"
|
8
|
+
spec.version = Mongoid::ExtendedDirtyTrackable::VERSION
|
9
|
+
spec.authors = ["Thomas Graves"]
|
10
|
+
spec.email = ["thomas@ooo.pm"]
|
11
|
+
|
12
|
+
spec.summary = "Mongoid extension for tracking changes to embedded and related documents"
|
13
|
+
spec.description = <<-EOS
|
14
|
+
An ActiveSupport::Concern that extends Mongoid to give you a mixin for tracking changes to embedded and related documents
|
15
|
+
EOS
|
16
|
+
spec.homepage = "https://github.com/graves/mongoid_extended_dirty_trackable"
|
17
|
+
spec.license = "MIT"
|
18
|
+
|
19
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
spec.bindir = "bin"
|
21
|
+
spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_runtime_dependency 'activesupport', '~> 3.0'
|
25
|
+
spec.add_runtime_dependency 'mongoid', '>= 3.1.0', '< 4.0'
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler"
|
28
|
+
spec.add_development_dependency "rake"
|
29
|
+
spec.add_development_dependency "pry"
|
30
|
+
spec.add_development_dependency "rspec"
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid_extended_dirty_trackable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Thomas Graves
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mongoid
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.1.0
|
34
|
+
- - "<"
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '4.0'
|
37
|
+
type: :runtime
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 3.1.0
|
44
|
+
- - "<"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '4.0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: bundler
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rake
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: pry
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: rspec
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
description: |2
|
104
|
+
An ActiveSupport::Concern that extends Mongoid to give you a mixin for tracking changes to embedded and related documents
|
105
|
+
email:
|
106
|
+
- thomas@ooo.pm
|
107
|
+
executables:
|
108
|
+
- console
|
109
|
+
- setup
|
110
|
+
extensions: []
|
111
|
+
extra_rdoc_files: []
|
112
|
+
files:
|
113
|
+
- ".gitignore"
|
114
|
+
- ".rspec"
|
115
|
+
- ".travis.yml"
|
116
|
+
- Gemfile
|
117
|
+
- LICENSE.txt
|
118
|
+
- README.md
|
119
|
+
- Rakefile
|
120
|
+
- TODO
|
121
|
+
- bin/console
|
122
|
+
- bin/setup
|
123
|
+
- lib/mongoid/extended_dirty_trackable.rb
|
124
|
+
- lib/mongoid/extended_dirty_trackable/version.rb
|
125
|
+
- lib/mongoid_extended_dirty_trackable.rb
|
126
|
+
- mongoid_extended_dirty_trackable.gemspec
|
127
|
+
homepage: https://github.com/graves/mongoid_extended_dirty_trackable
|
128
|
+
licenses:
|
129
|
+
- MIT
|
130
|
+
metadata: {}
|
131
|
+
post_install_message:
|
132
|
+
rdoc_options: []
|
133
|
+
require_paths:
|
134
|
+
- lib
|
135
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - ">="
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
requirements: []
|
146
|
+
rubyforge_project:
|
147
|
+
rubygems_version: 2.2.2
|
148
|
+
signing_key:
|
149
|
+
specification_version: 4
|
150
|
+
summary: Mongoid extension for tracking changes to embedded and related documents
|
151
|
+
test_files: []
|