mongoid-pending_changes 0.2.0a → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 36663ef17779fba395f8efe76861599eb15f0bc2
4
- data.tar.gz: ad7664ad93e27aa8e481350129540ef4c51808f7
3
+ metadata.gz: 9bd82313b3257fc4ad09831d3e0eb69d3e7476fb
4
+ data.tar.gz: 3d2e1f5ae522957bcaeb3f9cf118a614fa767379
5
5
  SHA512:
6
- metadata.gz: 5604b61f2603c415ac798246c73626ff67079e9fb214d579e26d8d9e9e4a6b53ab0704c63dcc65e464f390d8796ea79fdbe1f0a06a6d3d0541423a697c2cbd16
7
- data.tar.gz: df78268e42bc04d99952b29ce9e4698d31d2d601e51ae93acf702eb556fdded41b518941076cc91ab68247b74cf33b8601e5933c44ad62c3609d8984f594c695
6
+ metadata.gz: d64d2fc2a53a055b47d45027d18611230cdae57312d4154017ca50af92d0c207d9877038e5f687432e720e3839c1e226ec74527fa8ee9ff2227161b46abc1ee0
7
+ data.tar.gz: 3aa68495c2b686e84d74d0e50680f30e3873fab655931d7c68033f76dba06d1e85222423afaea40dd720c0b88a4d95f66611507b5761ed1841528f683c9af02f
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.3.0:
14
+ Adding methods '#apply_change' and '#reject_change'
15
+
13
16
  v0.2.0a:
14
17
  Adding method `#get_change_number`, which returns the change from the changelist with the specified number
15
18
 
@@ -45,7 +48,7 @@ end
45
48
  Then, when you want to have changes that require approval, call
46
49
 
47
50
  ```
48
- model.push_for_approval({example: 'new example value'})
51
+ model.push_for_approval {example: 'new example value'}
49
52
  ```
50
53
 
51
54
  Your model will keep the original value, but a new entry will be added to the `changelist` property, with all changes and an incrementing ID.
@@ -53,10 +56,19 @@ Your model will keep the original value, but a new entry will be added to the `c
53
56
  If you need to recover a specific change from the changelist, you can use
54
57
 
55
58
  ```
56
- change = mode.get_change_number(1)
57
- change[:data][:example] == 'new example value'
59
+ change = model.get_change_number 1
58
60
  ```
59
61
 
62
+ Then you can use `change[:data][:example] == 'new example value'`, for example.
63
+
64
+ If you want to apply a change to the model, you can call
65
+
66
+ ```
67
+ model.apply_change 1
68
+ ```
69
+
70
+ Then you can use `model.example == 'new example_value'`. Also, `model.changelist[1][:backup]` will store the old value, if there was one.
71
+
60
72
  ## Development
61
73
 
62
74
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -38,8 +38,62 @@ module Mongoid
38
38
  self.changelist.each do |cl|
39
39
  return cl if cl[:number] == number
40
40
  end
41
+
42
+ #If we got here, the number does not exist, return nil.
43
+ return nil
44
+ end
45
+
46
+ def apply_change(number, meta = {})
47
+ return unless number
48
+ new_changelist = self.changelist.map do |cl|
49
+ if cl[:number] == number
50
+ #Apply the changes to the main object and return the old values
51
+ backup = apply(cl[:data])
52
+ #Merge the change with the meta
53
+ cl.merge! meta
54
+ #Set the backup and other fields
55
+ cl[:backup] = backup
56
+ cl[:updated_at] = Time.now
57
+ cl[:approved] = true
58
+ end
59
+ cl
60
+ end
61
+ self.changelist = new_changelist
62
+ self.save!
63
+ end
64
+
65
+ def reject_change(number, meta = {})
66
+ return unless number
67
+ new_changelist = self.changelist.map do |cl|
68
+ if cl[:number] == number
69
+ #Merge the change with the meta
70
+ cl.merge! meta
71
+ cl[:updated_at] = Time.now
72
+ end
73
+ cl
74
+ end
75
+ self.changelist = new_changelist
76
+ self.save!
41
77
  end
42
78
 
79
+ private
80
+ def apply(data)
81
+ backup = {}
82
+
83
+ #For each data we want to update...
84
+ data.each do |field, value|
85
+ #if it exists, back it up
86
+ if self[field]
87
+ backup[field] = self[field]
88
+ end
89
+ #Update
90
+ self[field] = value
91
+ end
92
+
93
+ backup
94
+
95
+ end
96
+
43
97
  end
44
98
  end
45
99
  end
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module PendingChanges
3
- VERSION = "0.2.0a"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-pending_changes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0a
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - matheus208
@@ -115,9 +115,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
115
115
  version: '0'
116
116
  required_rubygems_version: !ruby/object:Gem::Requirement
117
117
  requirements:
118
- - - ">"
118
+ - - ">="
119
119
  - !ruby/object:Gem::Version
120
- version: 1.3.1
120
+ version: '0'
121
121
  requirements: []
122
122
  rubyforge_project:
123
123
  rubygems_version: 2.2.2