kakurenbo-puti 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +22 -0
- data/.travis.yml +11 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +110 -0
- data/Rakefile +2 -0
- data/kakurenbo-puti.gemspec +36 -0
- data/lib/kakurenbo_puti.rb +5 -0
- data/lib/kakurenbo_puti/active_record_base.rb +69 -0
- data/lib/kakurenbo_puti/version.rb +3 -0
- data/spec/kakurenbo_puti/active_record_base_spec.rb +330 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/supports/active_record_model.rb +35 -0
- data/spec/supports/active_record_model_spec.rb +17 -0
- metadata +166 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: db851cf209d31a80217b782ac9996290d3f72de5
|
4
|
+
data.tar.gz: 6e263c0a7727470e81c790e6d6cf07d1622a63ca
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b9a1cd55cf877d33bf7a5d558e800c2a68d97369fc315388f63289d3e832beef1908ecf7d195b5d1fb2dd84280c4f3379c615bb730b944d6d3a866a39aab73cd
|
7
|
+
data.tar.gz: d62a5e2a8705d012079ca2bf54c6e69830116f37cea3372b060fbe01b140a552632ab742a2ad0b8efa3ed0640c0e73ff07e3cb9b6f61ab2b553b84e3b393d2fc
|
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# Test report files.
|
2
|
+
/spec/reports/
|
3
|
+
/coverage/
|
4
|
+
|
5
|
+
# Documentation files.
|
6
|
+
/doc/
|
7
|
+
/_yardoc/
|
8
|
+
/.yardoc
|
9
|
+
|
10
|
+
# Temporary files.
|
11
|
+
/.bundle/
|
12
|
+
/Gemfile.lock
|
13
|
+
/pkg/
|
14
|
+
/tmp/
|
15
|
+
*.bundle
|
16
|
+
*.so
|
17
|
+
*.o
|
18
|
+
*.a
|
19
|
+
mkmf.log
|
20
|
+
|
21
|
+
# IDE files.
|
22
|
+
.idea/
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 alfa-jpn
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
# Kakurenbo\-Puti
|
2
|
+
kakurenbo-puti provides an ActiveRecord-friendly soft-delete gem.
|
3
|
+
This gem does not override methods of ActiveRecord.
|
4
|
+
|
5
|
+
I think that kakurenbo-puti is cho-iketeru! (very cool!)
|
6
|
+
|
7
|
+
[![Build Status](https://travis-ci.org/alfa-jpn/kakurenbo-puti.svg?branch=master)](https://travis-ci.org/alfa-jpn/kakurenbo-puti)
|
8
|
+
[![Coverage Status](https://coveralls.io/repos/alfa-jpn/kakurenbo-puti/badge.svg)](https://coveralls.io/r/alfa-jpn/kakurenbo-puti)
|
9
|
+
[![Code Climate](https://codeclimate.com/github/alfa-jpn/kakurenbo-puti/badges/gpa.svg)](https://codeclimate.com/github/alfa-jpn/kakurenbo-puti)
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'kakurenbo-puti'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install kakurenbo-puti
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
At first, add `soft_destroyed_at` column to your model.
|
29
|
+
|
30
|
+
```shell
|
31
|
+
rails g migration AddSoftDestroyedAtYourModel soft_destroyed_at:datetime:index
|
32
|
+
```
|
33
|
+
|
34
|
+
Next, call `soft_deletable` in model.
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
|
38
|
+
class Member < ActiveRecord::Base
|
39
|
+
soft_deletable
|
40
|
+
end
|
41
|
+
|
42
|
+
```
|
43
|
+
|
44
|
+
|
45
|
+
### Scopes
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
# Get models without soft-deleted
|
49
|
+
Model.without_soft_destroyed
|
50
|
+
|
51
|
+
# Get models only soft-deleted
|
52
|
+
Model.only_soft_destroyed
|
53
|
+
```
|
54
|
+
|
55
|
+
### Soft-delete record
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
model.soft_destroy
|
59
|
+
|
60
|
+
# or
|
61
|
+
model.soft_destroy!
|
62
|
+
|
63
|
+
# check soft_destroyed
|
64
|
+
model.soft_destroyed? # => true
|
65
|
+
```
|
66
|
+
|
67
|
+
# Restore record
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
model.restore
|
71
|
+
|
72
|
+
# or
|
73
|
+
model.restore!
|
74
|
+
```
|
75
|
+
|
76
|
+
## Advanced
|
77
|
+
|
78
|
+
### Definition of the dependency
|
79
|
+
Use dependent_associations option of `soft-deletable`.
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
|
83
|
+
class Parent < ActiveRecord::Base
|
84
|
+
soft_deletable
|
85
|
+
has_many :children
|
86
|
+
end
|
87
|
+
|
88
|
+
class Child < ActiveRecord::Base
|
89
|
+
soft_deletable dependent_associations: [:soft_delete_model, :normal_model]
|
90
|
+
belongs_to :parent
|
91
|
+
end
|
92
|
+
|
93
|
+
# create instance
|
94
|
+
parent = Parent.create!
|
95
|
+
child = Child.create!
|
96
|
+
|
97
|
+
# add child
|
98
|
+
parent.children << child
|
99
|
+
|
100
|
+
child.destroyed? # false
|
101
|
+
|
102
|
+
# soft-destroy parent
|
103
|
+
parent.soft_destroy
|
104
|
+
|
105
|
+
child.destroyed? # true
|
106
|
+
|
107
|
+
```
|
108
|
+
|
109
|
+
# License
|
110
|
+
This gem is released under the MIT license.
|
data/Rakefile
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'kakurenbo_puti/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'kakurenbo-puti'
|
8
|
+
spec.version = KakurenboPuti::VERSION
|
9
|
+
spec.authors = ['alfa-jpn']
|
10
|
+
spec.email = ['a.nkmr.ja@gmail.com']
|
11
|
+
spec.homepage = 'https://github.com/alfa-jpn/kakurenbo-puti'
|
12
|
+
spec.license = 'MIT'
|
13
|
+
spec.summary = <<-EOF
|
14
|
+
Lightweight soft-delete gem.
|
15
|
+
EOF
|
16
|
+
spec.description = <<-EOF
|
17
|
+
kakurenbo-puti provides an ActiveRecord-friendly soft-delete gem.
|
18
|
+
This gem does not override methods of ActiveRecord.
|
19
|
+
|
20
|
+
I think that kakurenbo-puti is cho-iketeru! (very cool!)
|
21
|
+
EOF
|
22
|
+
|
23
|
+
spec.files = `git ls-files -z`.split("\x0")
|
24
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
25
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
26
|
+
spec.require_paths = ['lib']
|
27
|
+
|
28
|
+
spec.add_dependency 'activerecord', '>= 4.1.0'
|
29
|
+
|
30
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
31
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
32
|
+
spec.add_development_dependency 'rspec', '~> 3.0.0'
|
33
|
+
spec.add_development_dependency 'yard', '~> 0.8.7.6'
|
34
|
+
spec.add_development_dependency 'sqlite3', '~> 1.3.10'
|
35
|
+
spec.add_development_dependency 'coveralls', '~> 0.7.8'
|
36
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module KakurenboPuti
|
2
|
+
# Extension module of ActiveRecord::Base
|
3
|
+
module ActiveRecordBase
|
4
|
+
# Enable soft-delete.
|
5
|
+
# @raise [StandardException] if Not found soft-deleted date column.
|
6
|
+
#
|
7
|
+
# @param [Symbol] column name of soft-deleted date column.
|
8
|
+
# @param [Array<Symbol>] dependent_associations names of dependency association.
|
9
|
+
def soft_deletable(column: :soft_destroyed_at, dependent_associations: [])
|
10
|
+
raise "`#{column}` is not defined in #{self.class.name}." unless column_names.include?(column.to_s)
|
11
|
+
|
12
|
+
define_singleton_method(:soft_delete_column) { column }
|
13
|
+
delegate :soft_delete_column, to: :class
|
14
|
+
|
15
|
+
define_model_callbacks :restore
|
16
|
+
define_model_callbacks :soft_destroy
|
17
|
+
|
18
|
+
scope :only_soft_destroyed, -> { where.not(id: without_soft_destroyed.select(:id)) }
|
19
|
+
scope :without_soft_destroyed, (lambda do
|
20
|
+
dependent_associations.inject(where(soft_delete_column => nil)) do |relation, name|
|
21
|
+
association = relation.klass.reflect_on_all_associations.find{|a| a.name == name }
|
22
|
+
if association.klass.method_defined?(:soft_delete_column)
|
23
|
+
relation.joins(name).merge(association.klass.without_soft_destroyed).references(name)
|
24
|
+
else
|
25
|
+
relation.joins(name).references(name)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end)
|
29
|
+
|
30
|
+
include InstanceMethods
|
31
|
+
end
|
32
|
+
|
33
|
+
module InstanceMethods
|
34
|
+
# Restore model.
|
35
|
+
# @return [Boolean] Return true if it is success.
|
36
|
+
def restore
|
37
|
+
true.tap { restore! }
|
38
|
+
rescue
|
39
|
+
false
|
40
|
+
end
|
41
|
+
|
42
|
+
# Restore model.
|
43
|
+
# @raise [ActiveRecordError]
|
44
|
+
def restore!
|
45
|
+
run_callbacks(:restore) { update_column soft_delete_column, nil; self }
|
46
|
+
end
|
47
|
+
|
48
|
+
# Soft-Delete model.
|
49
|
+
# @return [Boolean] Return true if it is success.
|
50
|
+
def soft_destroy
|
51
|
+
true.tap { soft_destroy! }
|
52
|
+
rescue
|
53
|
+
false
|
54
|
+
end
|
55
|
+
|
56
|
+
# Soft-Delete model.
|
57
|
+
# @raise [ActiveRecordError]
|
58
|
+
def soft_destroy!
|
59
|
+
run_callbacks(:soft_destroy) { touch soft_delete_column; self }
|
60
|
+
end
|
61
|
+
|
62
|
+
# Check if model is soft-deleted.
|
63
|
+
# @return [Boolean] Return true if model is soft-deleted.
|
64
|
+
def soft_destroyed?
|
65
|
+
self.class.only_soft_destroyed.where(id: id).exists?
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,330 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe KakurenboPuti::ActiveRecordBase do
|
4
|
+
define_active_record_model :NormalModel do; end
|
5
|
+
|
6
|
+
define_active_record_model :SoftDeleteModel do |t|
|
7
|
+
t.datetime :soft_destroyed_at
|
8
|
+
t.datetime :deleted_at
|
9
|
+
end
|
10
|
+
|
11
|
+
define_active_record_model :SoftDeleteChild do |t|
|
12
|
+
t.integer :soft_delete_model_id
|
13
|
+
t.integer :normal_model_id
|
14
|
+
t.datetime :soft_destroyed_at
|
15
|
+
end
|
16
|
+
|
17
|
+
let :model_class do
|
18
|
+
options_cache = model_class_options
|
19
|
+
SoftDeleteModel.tap do |klass|
|
20
|
+
klass.class_eval do
|
21
|
+
soft_deletable options_cache
|
22
|
+
|
23
|
+
before_soft_destroy :cb_mock
|
24
|
+
after_soft_destroy :cb_mock
|
25
|
+
|
26
|
+
before_restore :cb_mock
|
27
|
+
after_restore :cb_mock
|
28
|
+
|
29
|
+
define_method(:cb_mock) { true }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
let :child_class do
|
35
|
+
options_cache = child_class_options
|
36
|
+
SoftDeleteChild.tap do |klass|
|
37
|
+
klass.class_eval do
|
38
|
+
soft_deletable options_cache
|
39
|
+
belongs_to :soft_delete_model
|
40
|
+
belongs_to :normal_model
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
let :model_class_options do
|
46
|
+
{}
|
47
|
+
end
|
48
|
+
|
49
|
+
let :child_class_options do
|
50
|
+
{ dependent_associations: [:soft_delete_model, :normal_model] }
|
51
|
+
end
|
52
|
+
|
53
|
+
let! :normal_model_instance do
|
54
|
+
NormalModel.create!
|
55
|
+
end
|
56
|
+
|
57
|
+
let! :model_instance do
|
58
|
+
model_class.create!
|
59
|
+
end
|
60
|
+
|
61
|
+
let! :child_instance do
|
62
|
+
child_class.create!(soft_delete_model: model_instance, normal_model: normal_model_instance)
|
63
|
+
end
|
64
|
+
|
65
|
+
describe '.soft_delete_column' do
|
66
|
+
it 'Return column name of soft-delete' do
|
67
|
+
expect(model_class.soft_delete_column).to eq(:soft_destroyed_at)
|
68
|
+
end
|
69
|
+
|
70
|
+
context 'When with column option' do
|
71
|
+
let :model_class_options do
|
72
|
+
{ column: :deleted_at }
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'Return column name of option' do
|
76
|
+
expect(model_class.soft_delete_column).to eq(model_class_options[:column])
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe '.only_soft_destroyed' do
|
82
|
+
subject do
|
83
|
+
child_class.only_soft_destroyed
|
84
|
+
end
|
85
|
+
|
86
|
+
context 'When soft-deleted' do
|
87
|
+
it 'Return a relation without soft-deleted model.' do
|
88
|
+
expect {
|
89
|
+
child_instance.soft_destroy!
|
90
|
+
}.to change {
|
91
|
+
subject.count
|
92
|
+
}.by(1)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context 'When parent is soft-deleted' do
|
97
|
+
it 'Return a relation without parent soft-deleted model.' do
|
98
|
+
expect {
|
99
|
+
child_instance.soft_delete_model.soft_destroy!
|
100
|
+
}.to change {
|
101
|
+
subject.count
|
102
|
+
}.by(1)
|
103
|
+
end
|
104
|
+
|
105
|
+
context 'When dependent association is nothing' do
|
106
|
+
let :child_class_options do
|
107
|
+
{ dependent_associations: [] }
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'Return a relation with parent soft-deleted model.' do
|
111
|
+
expect {
|
112
|
+
child_instance.soft_delete_model.soft_destroy!
|
113
|
+
}.not_to change {
|
114
|
+
subject.count
|
115
|
+
}
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
context 'When parent is hard-deleted' do
|
121
|
+
it 'Return a relation without parent soft-deleted model.' do
|
122
|
+
expect {
|
123
|
+
child_instance.normal_model.destroy!
|
124
|
+
}.to change {
|
125
|
+
subject.count
|
126
|
+
}.by(1)
|
127
|
+
end
|
128
|
+
|
129
|
+
context 'When dependent association is nothing' do
|
130
|
+
let :child_class_options do
|
131
|
+
{ dependent_associations: [] }
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'Return a relation with parent soft-deleted model.' do
|
135
|
+
expect {
|
136
|
+
child_instance.normal_model.destroy!
|
137
|
+
}.not_to change {
|
138
|
+
subject.count
|
139
|
+
}
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
describe '.without_soft_destroyed' do
|
146
|
+
subject do
|
147
|
+
child_class.without_soft_destroyed
|
148
|
+
end
|
149
|
+
|
150
|
+
context 'When soft-deleted' do
|
151
|
+
it 'Return a relation without soft-deleted model.' do
|
152
|
+
expect {
|
153
|
+
child_instance.soft_destroy!
|
154
|
+
}.to change {
|
155
|
+
subject.count
|
156
|
+
}.by(-1)
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
context 'When parent is soft-deleted' do
|
161
|
+
it 'Return a relation without parent soft-deleted model.' do
|
162
|
+
expect {
|
163
|
+
child_instance.soft_delete_model.soft_destroy!
|
164
|
+
}.to change {
|
165
|
+
subject.count
|
166
|
+
}.by(-1)
|
167
|
+
end
|
168
|
+
|
169
|
+
context 'When dependent association is nothing' do
|
170
|
+
let :child_class_options do
|
171
|
+
{ dependent_associations: [] }
|
172
|
+
end
|
173
|
+
|
174
|
+
it 'Return a relation with parent soft-deleted model.' do
|
175
|
+
expect {
|
176
|
+
child_instance.soft_delete_model.soft_destroy!
|
177
|
+
}.not_to change {
|
178
|
+
subject.count
|
179
|
+
}
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
context 'When parent is hard-deleted' do
|
185
|
+
it 'Return a relation without parent soft-deleted model.' do
|
186
|
+
expect {
|
187
|
+
child_instance.normal_model.destroy!
|
188
|
+
}.to change {
|
189
|
+
subject.count
|
190
|
+
}.by(-1)
|
191
|
+
end
|
192
|
+
|
193
|
+
context 'When dependent association is nothing' do
|
194
|
+
let :child_class_options do
|
195
|
+
{ dependent_associations: [] }
|
196
|
+
end
|
197
|
+
|
198
|
+
it 'Return a relation with parent soft-deleted model.' do
|
199
|
+
expect {
|
200
|
+
child_instance.normal_model.destroy!
|
201
|
+
}.not_to change {
|
202
|
+
subject.count
|
203
|
+
}
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
describe '#restore' do
|
210
|
+
before :each do
|
211
|
+
model_instance.soft_destroy
|
212
|
+
end
|
213
|
+
|
214
|
+
subject do
|
215
|
+
model_instance.restore
|
216
|
+
end
|
217
|
+
|
218
|
+
it 'Restore soft-deleted model.' do
|
219
|
+
expect {
|
220
|
+
subject
|
221
|
+
}.to change {
|
222
|
+
model_class.without_soft_destroyed.count
|
223
|
+
}.by(1)
|
224
|
+
end
|
225
|
+
|
226
|
+
it 'Return truethy value.' do
|
227
|
+
expect(subject).to be_truthy
|
228
|
+
end
|
229
|
+
|
230
|
+
it 'Run callbacks.' do
|
231
|
+
expect(model_instance).to receive(:cb_mock).twice
|
232
|
+
subject
|
233
|
+
end
|
234
|
+
|
235
|
+
context 'When raise exception.' do
|
236
|
+
before :each do
|
237
|
+
allow_any_instance_of(model_class).to receive(:update_column) { raise }
|
238
|
+
end
|
239
|
+
|
240
|
+
it 'Return falsey value.' do
|
241
|
+
expect(subject).to be_falsey
|
242
|
+
end
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
describe '#restore!' do
|
247
|
+
subject do
|
248
|
+
model_instance.restore!
|
249
|
+
end
|
250
|
+
|
251
|
+
context 'When raise exception.' do
|
252
|
+
before :each do
|
253
|
+
allow_any_instance_of(model_class).to receive(:update_column) { raise }
|
254
|
+
end
|
255
|
+
|
256
|
+
it 'Raise Error.' do
|
257
|
+
expect{ subject }.to raise_error
|
258
|
+
end
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
describe '#soft_delete' do
|
263
|
+
subject do
|
264
|
+
model_instance.soft_destroy
|
265
|
+
end
|
266
|
+
|
267
|
+
it 'Soft-delete model.' do
|
268
|
+
expect {
|
269
|
+
subject
|
270
|
+
}.to change {
|
271
|
+
model_class.without_soft_destroyed.count
|
272
|
+
}.by(-1)
|
273
|
+
end
|
274
|
+
|
275
|
+
it 'Return truethy value.' do
|
276
|
+
expect(subject).to be_truthy
|
277
|
+
end
|
278
|
+
|
279
|
+
it 'Run callbacks.' do
|
280
|
+
expect(model_instance).to receive(:cb_mock).twice
|
281
|
+
subject
|
282
|
+
end
|
283
|
+
|
284
|
+
context 'When raise exception.' do
|
285
|
+
before :each do
|
286
|
+
allow_any_instance_of(model_class).to receive(:touch) { raise }
|
287
|
+
end
|
288
|
+
|
289
|
+
it 'Return falsey value.' do
|
290
|
+
expect(subject).to be_falsey
|
291
|
+
end
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
describe '#soft_delete!' do
|
296
|
+
subject do
|
297
|
+
model_instance.soft_delete!
|
298
|
+
end
|
299
|
+
|
300
|
+
context 'When raise exception.' do
|
301
|
+
before :each do
|
302
|
+
allow_any_instance_of(model_class).to receive(:update_column) { raise }
|
303
|
+
end
|
304
|
+
|
305
|
+
it 'Raise Error.' do
|
306
|
+
expect{ subject }.to raise_error
|
307
|
+
end
|
308
|
+
end
|
309
|
+
end
|
310
|
+
|
311
|
+
describe '#soft_destroyed?' do
|
312
|
+
subject do
|
313
|
+
model_instance.soft_destroyed?
|
314
|
+
end
|
315
|
+
|
316
|
+
it 'Return falsey' do
|
317
|
+
expect(subject).to be_falsey
|
318
|
+
end
|
319
|
+
|
320
|
+
context 'When model is soft-deleted' do
|
321
|
+
before :each do
|
322
|
+
model_instance.soft_destroy!
|
323
|
+
end
|
324
|
+
|
325
|
+
it 'Return truthy' do
|
326
|
+
expect(subject).to be_truthy
|
327
|
+
end
|
328
|
+
end
|
329
|
+
end
|
330
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'supports/active_record_model'
|
4
|
+
require 'kakurenbo_puti'
|
5
|
+
require 'coveralls'
|
6
|
+
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.color = true
|
9
|
+
config.mock_framework = :rspec
|
10
|
+
config.before :all do
|
11
|
+
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
Coveralls.wear!
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
|
3
|
+
# Use ActiveSupport::Inflector#classify
|
4
|
+
Inflector = Class.new.extend(ActiveSupport::Inflector)
|
5
|
+
|
6
|
+
# Define model of ActiveRecord.
|
7
|
+
#
|
8
|
+
# @param [Symbol] Model name.
|
9
|
+
# @yield [table] Table column definition.
|
10
|
+
def define_active_record_model(model_name, &block)
|
11
|
+
raise 'column definition block is nothing!' unless block_given?
|
12
|
+
|
13
|
+
tableize_name = Inflector.tableize(model_name)
|
14
|
+
|
15
|
+
before :all do
|
16
|
+
migration = ActiveRecord::Migration.new
|
17
|
+
migration.verbose = false
|
18
|
+
migration.create_table tableize_name, &block
|
19
|
+
|
20
|
+
mock_class = Class.new(ActiveRecord::Base) do
|
21
|
+
define_singleton_method(:name) { model_name.to_s }
|
22
|
+
reset_table_name
|
23
|
+
end
|
24
|
+
|
25
|
+
Object.const_set model_name, mock_class
|
26
|
+
end
|
27
|
+
|
28
|
+
after :all do
|
29
|
+
migration = ActiveRecord::Migration.new
|
30
|
+
migration.verbose = false
|
31
|
+
migration.drop_table tableize_name
|
32
|
+
|
33
|
+
Object.class_eval { remove_const model_name }
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe '#define_active_record_model' do
|
4
|
+
define_active_record_model :Drink do |t|
|
5
|
+
t.integer :price
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'created Drink class.' do
|
9
|
+
expect(Object.const_defined? :Drink).to be_truthy
|
10
|
+
end
|
11
|
+
|
12
|
+
describe 'created class' do
|
13
|
+
it 'has methods of ActiveRecord.' do
|
14
|
+
expect(Drink).to respond_to(:create, :find, :where, :update_all)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kakurenbo-puti
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- alfa-jpn
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.1.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.0.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.0.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: yard
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.8.7.6
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.8.7.6
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: sqlite3
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 1.3.10
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.3.10
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: coveralls
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 0.7.8
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 0.7.8
|
111
|
+
description: |2
|
112
|
+
kakurenbo-puti provides an ActiveRecord-friendly soft-delete gem.
|
113
|
+
This gem does not override methods of ActiveRecord.
|
114
|
+
|
115
|
+
I think that kakurenbo-puti is cho-iketeru! (very cool!)
|
116
|
+
email:
|
117
|
+
- a.nkmr.ja@gmail.com
|
118
|
+
executables: []
|
119
|
+
extensions: []
|
120
|
+
extra_rdoc_files: []
|
121
|
+
files:
|
122
|
+
- ".coveralls.yml"
|
123
|
+
- ".gitignore"
|
124
|
+
- ".travis.yml"
|
125
|
+
- Gemfile
|
126
|
+
- LICENSE.txt
|
127
|
+
- README.md
|
128
|
+
- Rakefile
|
129
|
+
- kakurenbo-puti.gemspec
|
130
|
+
- lib/kakurenbo_puti.rb
|
131
|
+
- lib/kakurenbo_puti/active_record_base.rb
|
132
|
+
- lib/kakurenbo_puti/version.rb
|
133
|
+
- spec/kakurenbo_puti/active_record_base_spec.rb
|
134
|
+
- spec/spec_helper.rb
|
135
|
+
- spec/supports/active_record_model.rb
|
136
|
+
- spec/supports/active_record_model_spec.rb
|
137
|
+
homepage: https://github.com/alfa-jpn/kakurenbo-puti
|
138
|
+
licenses:
|
139
|
+
- MIT
|
140
|
+
metadata: {}
|
141
|
+
post_install_message:
|
142
|
+
rdoc_options: []
|
143
|
+
require_paths:
|
144
|
+
- lib
|
145
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
146
|
+
requirements:
|
147
|
+
- - ">="
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0'
|
150
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
151
|
+
requirements:
|
152
|
+
- - ">="
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
version: '0'
|
155
|
+
requirements: []
|
156
|
+
rubyforge_project:
|
157
|
+
rubygems_version: 2.4.5
|
158
|
+
signing_key:
|
159
|
+
specification_version: 4
|
160
|
+
summary: Lightweight soft-delete gem.
|
161
|
+
test_files:
|
162
|
+
- spec/kakurenbo_puti/active_record_base_spec.rb
|
163
|
+
- spec/spec_helper.rb
|
164
|
+
- spec/supports/active_record_model.rb
|
165
|
+
- spec/supports/active_record_model_spec.rb
|
166
|
+
has_rdoc:
|