mongoid-undo 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,25 @@
1
+ # Because this is a gem, ignore Gemfile.lock:
2
+
3
+ Gemfile.lock
4
+
5
+ # And because this is Ruby, ignore the following
6
+ # (source: https://github.com/github/gitignore/blob/master/Ruby.gitignore):
7
+
8
+ *.gem
9
+ *.rbc
10
+ .bundle
11
+ .config
12
+ coverage
13
+ InstalledFiles
14
+ lib/bundler/man
15
+ pkg
16
+ rdoc
17
+ spec/reports
18
+ test/tmp
19
+ test/version_tmp
20
+ tmp
21
+
22
+ # YARD artifacts
23
+ .yardoc
24
+ _yardoc
25
+ doc/
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # 0.1.0
2
+
3
+ * First public version.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # mongoid-undo
2
+
3
+ Super simple versioning for your Mongoid app.
4
+
5
+ TODO: Add usage and installation instructions.
6
+
7
+ ## Contributing
8
+
9
+ 1. Fork it
10
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
11
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
12
+ 4. Push to the branch (`git push origin my-new-feature`)
13
+ 5. Create new Pull Request
14
+
15
+ ## Copyright
16
+
17
+ (The MIT license)
18
+
19
+ Copyright (c) 2012 Mario Uher
20
+
21
+ Permission is hereby granted, free of charge, to any person obtaining
22
+ a copy of this software and associated documentation files (the
23
+ "Software"), to deal in the Software without restriction, including
24
+ without limitation the rights to use, copy, modify, merge, publish,
25
+ distribute, sublicense, and/or sell copies of the Software, and to
26
+ permit persons to whom the Software is furnished to do so, subject to
27
+ the following conditions:
28
+
29
+ The above copyright notice and this permission notice shall be
30
+ included in all copies or substantial portions of the Software.
31
+
32
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
34
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
36
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
37
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
38
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.test_files = Dir.glob('spec/**/*_spec.rb')
6
+ end
7
+
8
+ task(default: :test)
@@ -0,0 +1,51 @@
1
+ require 'active_support/concern'
2
+ require 'mongoid'
3
+
4
+ module Mongoid::Fields
5
+ class Localized
6
+ def mongoize(object)
7
+ if object.is_a? ::Hash
8
+ object
9
+ else
10
+ { ::I18n.locale.to_s => type.mongoize(object) }
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+
17
+ module Mongoid
18
+ module Undo
19
+ extend ActiveSupport::Concern
20
+
21
+ include Mongoid::Paranoia
22
+ include Mongoid::Versioning
23
+
24
+ included do
25
+ field :action, type: Symbol, versioned: false
26
+ index deleted_at: 1
27
+
28
+ [:create, :update, :destroy].each do |action|
29
+ set_callback action, :after do
30
+ collection.find(atomic_selector).update '$set' => { action: action }
31
+ reload
32
+ end
33
+ end
34
+ end
35
+
36
+ def undo
37
+ case action
38
+ when :create, :destroy
39
+ deleted_at.present? ? restore : delete
40
+ when :update
41
+ retrieve
42
+ end
43
+ end
44
+ alias_method :redo, :undo
45
+
46
+ private
47
+ def retrieve
48
+ update_attributes versions.last.versioned_attributes.except('version')
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ module Undo
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,18 @@
1
+ require './lib/mongoid/undo/version'
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'mongoid-undo'
5
+ gem.version = Mongoid::Undo::VERSION
6
+ gem.authors = 'Mario Uher'
7
+ gem.email = 'uher.mario@gmail.com'
8
+ gem.homepage = 'https://github.com/haihappen/mongoid-undo'
9
+ gem.summary = 'Super simple versioning for your Mongoid app.'
10
+ gem.description = 'mongoid-undo provides a super simple and easy to use versioning system for Mongo apps.'
11
+
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.require_path = 'lib'
14
+
15
+ gem.add_dependency 'mongoid'
16
+
17
+ gem.add_development_dependency 'minitest'
18
+ end
data/spec/mongoid.yml ADDED
@@ -0,0 +1,8 @@
1
+ test:
2
+ sessions:
3
+ default:
4
+ database: mongoid_undo_test
5
+ hosts:
6
+ - localhost:27017
7
+ options:
8
+ consistency: :strong
@@ -0,0 +1,9 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require 'minitest/spec'
4
+
5
+ require 'active_support/testing/assertions'
6
+
7
+ require './lib/mongoid/undo'
8
+
9
+ Mongoid.load!(File.expand_path('../mongoid.yml', __FILE__), 'test')
data/spec/undo_spec.rb ADDED
@@ -0,0 +1,172 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ module Mongoid
4
+ module Undo
5
+ describe self do
6
+ include ActiveSupport::Testing::Assertions
7
+
8
+ class Document
9
+ include Mongoid::Document
10
+ include Mongoid::Undo
11
+
12
+ field :name, type: String
13
+ end
14
+
15
+
16
+ subject { Document.create(name: 'Version 1') }
17
+
18
+
19
+ describe 'undoing create' do
20
+ before { subject.undo }
21
+
22
+
23
+ it 'deletes' do
24
+ subject.persisted?.wont_equal true
25
+ subject.deleted_at.wont_be_nil
26
+ end
27
+
28
+
29
+ describe 'and then redoing' do
30
+ it 'redoes' do
31
+ subject.redo
32
+
33
+ subject.persisted?.must_equal true
34
+ subject.deleted_at.must_be_nil
35
+ end
36
+ end
37
+
38
+
39
+ it 'returns proper state' do
40
+ 3.times do
41
+ subject.undo
42
+ subject.action.must_equal :create
43
+ end
44
+ end
45
+ end
46
+
47
+
48
+ describe 'undoing update' do
49
+ before { subject.update_attributes(name: subject.name.next) }
50
+
51
+
52
+ it 'creates a new version' do
53
+ assert_difference 'subject.version', +1 do
54
+ subject.undo
55
+ end
56
+
57
+ # Ensure the new version is saved to the db
58
+ subject.persisted?.must_equal true
59
+ subject.name.must_equal 'Version 1'
60
+ end
61
+
62
+
63
+ it 'returns proper state' do
64
+ 3.times do
65
+ subject.undo
66
+ subject.action.must_equal :update
67
+ end
68
+ end
69
+ end
70
+
71
+
72
+ describe 'undoing destroy' do
73
+ describe 'having one version' do
74
+ before { subject.destroy }
75
+
76
+
77
+ it 'restores' do
78
+ subject.undo
79
+
80
+ subject.persisted?.must_equal true
81
+ subject.deleted_at.must_be_nil
82
+ end
83
+
84
+
85
+ it 'returns proper state' do
86
+ 3.times do
87
+ subject.undo
88
+ subject.action.must_equal :destroy
89
+ end
90
+ end
91
+ end
92
+
93
+
94
+ describe 'having multiple versions' do
95
+ before do
96
+ 3.times { subject.update_attributes(name: subject.name.next) }
97
+ subject.destroy
98
+ end
99
+
100
+
101
+ it 'restores' do
102
+ assert_difference 'subject.version', 0 do
103
+ subject.undo
104
+ end
105
+
106
+ subject.persisted?.must_equal true
107
+ subject.deleted_at.must_be_nil
108
+ end
109
+
110
+
111
+ it 'returns proper state' do
112
+ 3.times do
113
+ subject.undo
114
+ subject.action.must_equal :destroy
115
+ end
116
+ end
117
+ end
118
+ end
119
+
120
+
121
+ describe :redo do
122
+ it 'is a convenient alias for undo' do
123
+ subject.method(:redo).must_equal subject.method(:undo)
124
+ end
125
+ end
126
+
127
+
128
+ describe :state do
129
+ it 'is a symbol' do
130
+ subject.fields['action'].options[:type].must_equal Symbol
131
+ end
132
+
133
+
134
+ it 'is versionless' do
135
+ subject.fields['action'].options[:versioned].must_equal false
136
+ end
137
+ end
138
+
139
+
140
+ describe :retrieve do
141
+ class Localized
142
+ include Mongoid::Document
143
+ include Mongoid::Undo
144
+
145
+ field :language, localize: true
146
+ end
147
+
148
+
149
+ subject { Localized.new }
150
+
151
+
152
+ it 'works too with localized fields' do
153
+ subject.update_attributes language: 'English'
154
+ subject.update_attributes language: 'English Updated'
155
+
156
+ assert_difference 'subject.version', +1 do
157
+ subject.send(:retrieve)
158
+ end
159
+ subject.language.must_equal 'English'
160
+
161
+ assert_difference 'subject.version', +1 do
162
+ subject.send(:retrieve)
163
+ end
164
+ subject.language.must_equal 'English Updated'
165
+ end
166
+
167
+
168
+ after { I18n.locale = I18n.default_locale }
169
+ end
170
+ end
171
+ end
172
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-undo
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Mario Uher
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ version_requirements: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ none: false
21
+ name: mongoid
22
+ type: :runtime
23
+ prerelease: false
24
+ requirement: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ! '>='
27
+ - !ruby/object:Gem::Version
28
+ version: '0'
29
+ none: false
30
+ - !ruby/object:Gem::Dependency
31
+ version_requirements: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ none: false
37
+ name: minitest
38
+ type: :development
39
+ prerelease: false
40
+ requirement: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ none: false
46
+ description: mongoid-undo provides a super simple and easy to use versioning system
47
+ for Mongo apps.
48
+ email: uher.mario@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - CHANGELOG.md
55
+ - Gemfile
56
+ - README.md
57
+ - Rakefile
58
+ - lib/mongoid/undo.rb
59
+ - lib/mongoid/undo/version.rb
60
+ - mongoid-undo.gemspec
61
+ - spec/mongoid.yml
62
+ - spec/spec_helper.rb
63
+ - spec/undo_spec.rb
64
+ homepage: https://github.com/haihappen/mongoid-undo
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ none: false
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ none: false
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.23
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Super simple versioning for your Mongoid app.
88
+ test_files: []