mongoid-pending_changes 0.3.0 → 0.4.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 +10 -2
- data/lib/mongoid/pending_changes/pending_changes.rb +32 -1
- data/lib/mongoid/pending_changes/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6dce5a15518ca9176d773b7a3364788f96e4581d
|
4
|
+
data.tar.gz: 787ff3f834156ec643507b2036e5d25ecfb00e44
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e9507da094c0945f4ba81bf5a19b324810d6d6b3bea8d097acf78347ad804068b3ae1fd7893cc18e15436f792ec71fef3c9de4433f594ce42b8b10dec907e363
|
7
|
+
data.tar.gz: 4b33a0475a311829cd7389332588139151b9e5b0a13a5e0de6298fa34af5737f640d98c9e8842836ce0916fc0479e7820e0536a7cc9efbb0f38ca20efe4d062d
|
data/README.md
CHANGED
@@ -10,6 +10,9 @@ This is an initial effort to develop an approval system to control changes to co
|
|
10
10
|
|
11
11
|
## Change Log
|
12
12
|
|
13
|
+
v0.4.0:
|
14
|
+
Creating the `appendable` helper, which flags an Array field as a collection of values. When applying changes for appendable fields, instead of replacing them.
|
15
|
+
|
13
16
|
v0.3.0:
|
14
17
|
Adding methods '#apply_change' and '#reject_change'
|
15
18
|
|
@@ -80,5 +83,10 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
80
83
|
Bug reports and pull requests are welcome on GitHub at https://github.com/matheus208/mongoid-pending_changes.
|
81
84
|
|
82
85
|
## TODO list
|
83
|
-
*
|
84
|
-
|
86
|
+
* Create a class to represent a "Change" (instead of using arrays)
|
87
|
+
* Make `appendable` work for Hashes.
|
88
|
+
* Create options for `appendable`, like: whether or not it merges two arrays (or it just appends the second array to the first, creating a nested array), how to make a change remove or replace the field, instead of appending to it, etc...
|
89
|
+
* Have a conversion modifier parameter when applying changes. E.g.: when applying change, pass a block that will receive the change data as a parameter, and could modify it (useful for converting between different versions maybe?)
|
90
|
+
* Test if it works on relations. (I.e. change with a relation swap)
|
91
|
+
* Enforce type on changes.
|
92
|
+
* ... Complete the TODO list
|
@@ -4,6 +4,7 @@ module Mongoid
|
|
4
4
|
module PendingChanges
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
GLOBAL_PENDING_CHANGES_FLAG = 'mongoid_pending_changes_enabled'
|
7
|
+
|
7
8
|
included do
|
8
9
|
include Mongoid::Document
|
9
10
|
|
@@ -11,6 +12,9 @@ module Mongoid
|
|
11
12
|
field :last_version, type: Integer
|
12
13
|
field :changelist, type: Array
|
13
14
|
|
15
|
+
mattr_accessor :__appendable_fields
|
16
|
+
|
17
|
+
|
14
18
|
before_create do
|
15
19
|
self.version = 0
|
16
20
|
self.last_version = 0
|
@@ -77,6 +81,13 @@ module Mongoid
|
|
77
81
|
end
|
78
82
|
|
79
83
|
private
|
84
|
+
|
85
|
+
def self.appendable(field)
|
86
|
+
#Make it private so we can one set a field as appendable on the Class definition, not on Runtime (is this the best idea?)
|
87
|
+
self.__appendable_fields ||= []
|
88
|
+
self.__appendable_fields << field
|
89
|
+
end
|
90
|
+
|
80
91
|
def apply(data)
|
81
92
|
backup = {}
|
82
93
|
|
@@ -87,10 +98,30 @@ module Mongoid
|
|
87
98
|
backup[field] = self[field]
|
88
99
|
end
|
89
100
|
#Update
|
90
|
-
|
101
|
+
incorporate_change field, value
|
91
102
|
end
|
92
103
|
|
93
104
|
backup
|
105
|
+
end
|
106
|
+
|
107
|
+
def incorporate_change(field, value)
|
108
|
+
#Check if this field is appendable or not
|
109
|
+
|
110
|
+
if self.__appendable_fields && self.__appendable_fields.include?(field.intern)
|
111
|
+
#Append
|
112
|
+
self[field] ||= []
|
113
|
+
if value.is_a? Array
|
114
|
+
#Append all values
|
115
|
+
self.set "#{field}" => self[field] + value
|
116
|
+
else
|
117
|
+
#Append single value
|
118
|
+
self.set "#{field}" => self[field].push(value)
|
119
|
+
end
|
120
|
+
|
121
|
+
else
|
122
|
+
#Replace
|
123
|
+
self.set "#{field}" => value
|
124
|
+
end
|
94
125
|
|
95
126
|
end
|
96
127
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-pending_changes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- matheus208
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|