closet 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +196 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/closet.gemspec +41 -0
- data/lib/closet.rb +25 -0
- data/lib/closet/.DS_Store +0 -0
- data/lib/closet/bury/class_methods.rb +51 -0
- data/lib/closet/bury/instance_methods.rb +37 -0
- data/lib/closet/callbacks.rb +10 -0
- data/lib/closet/query/class_methods.rb +15 -0
- data/lib/closet/reflection_handler.rb +79 -0
- data/lib/closet/restore/class_methods.rb +47 -0
- data/lib/closet/restore/instance_methods.rb +33 -0
- data/lib/closet/version.rb +3 -0
- metadata +181 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 34749701bcf85b1fd7ef10b013dbd4cacf67020d
|
4
|
+
data.tar.gz: 04f61f1d9ab80aa578ceda837de0f5446912526f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 43ed97741098893c2245ea7f0ecf5e640ba08c4ea74a76c7373c178b095a901af69b21bf846a15fdd46bc954e05b481b9d3f484046f2ca723c4387cf0d8e696b
|
7
|
+
data.tar.gz: 0fe291158ff4bb1c3483384b0786a7c8de2797f24bd3c5b769963f566b5ecb5c82109af0cec84ca2e47345998f45f4f01014f1d9950c509c92c4849473e899ed
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.2.3
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 TODO: Write your name
|
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,196 @@
|
|
1
|
+
# Closet
|
2
|
+
Closet let you bury your records instead of killing(destroy) them.
|
3
|
+
|
4
|
+
Data is valuable even those you think worthless.
|
5
|
+
Closet helps you bury/hide your records in the closet, and restore them whenever you want.
|
6
|
+
|
7
|
+
Closet only works with ActiveRecord(Mongoid will support in near future) now.
|
8
|
+
|
9
|
+
There is one main difference between closet and other similar packages, Closet didn't change default behaviour of `ActiveRecord`, instead brings new functionality on ActiveRecord.
|
10
|
+
|
11
|
+
## Requirements
|
12
|
+
"activerecord", "~> 4.0"
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'closet'
|
20
|
+
```
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
|
24
|
+
$ bundle
|
25
|
+
|
26
|
+
Or install it yourself as:
|
27
|
+
|
28
|
+
$ gem install closet
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
Before do anything, Please add `buried_at:datetime` into your desired model.
|
33
|
+
Run:
|
34
|
+
|
35
|
+
$ rails g migration AddBuriedAtToUsers buried_at:datetime:index
|
36
|
+
|
37
|
+
and now you have following migration
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
class AddBuriedAtToUserss < ActiveRecord::Migration
|
41
|
+
def change
|
42
|
+
add_column :users, :buried_at, :datetime
|
43
|
+
add_index :users, :buried_at
|
44
|
+
end
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
repeat above process for every desired model.
|
49
|
+
|
50
|
+
And now include `Closet` into the model:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
class User < ActiveRecord::Base
|
54
|
+
# define associations ...
|
55
|
+
has_many :articles, dependent: :destroy # Closet will be automatically included to class of this association
|
56
|
+
# After define associations
|
57
|
+
include Closet
|
58
|
+
# ....
|
59
|
+
end
|
60
|
+
```
|
61
|
+
|
62
|
+
### Associations
|
63
|
+
|
64
|
+
Closet is smart, after including `Closet` into your model, closet will include on every association with `dependent` option automatically. So there is no need to include `Closet` into model's associations with `dependent` option.
|
65
|
+
|
66
|
+
Closet only works with following associations: `has_many`, `has_one`, `belongs_to`.
|
67
|
+
It means you must run the migration for every association with `dependent` option.
|
68
|
+
|
69
|
+
It worth to mention that `dependent` option works exactly like ActiveRecord:
|
70
|
+
```ruby
|
71
|
+
has_many dependent: # Acceptable values: [:destroy, :delete_all]
|
72
|
+
has_one dependent: # Acceptable values: [:destroy, :delete]
|
73
|
+
belongs_to dependent: # Acceptable values: [:destroy, :delete]
|
74
|
+
```
|
75
|
+
|
76
|
+
### Instance Methods
|
77
|
+
|
78
|
+
All of instance methods are using ActiveRecord Transactions.
|
79
|
+
|
80
|
+
Calling `#bury` method on `User` instance will update the `buried_at` column:
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
user.buried? # Always return in boolean
|
84
|
+
# => false
|
85
|
+
user.bury # Always return in boolean
|
86
|
+
# => true
|
87
|
+
user.buried?
|
88
|
+
# => true
|
89
|
+
user.bury( dependent: false ) # Call bury without dependent callbacks
|
90
|
+
# => true
|
91
|
+
```
|
92
|
+
`#bury!` is similar to `#bury` method except it raise `Exception` on failure.
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
is_going_to_failure.bury!
|
96
|
+
# => Exception
|
97
|
+
```
|
98
|
+
|
99
|
+
Also if your model have an association with `dependent` option, `#bury` effects on the association too.
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
user.articles.map do |article|
|
103
|
+
article.buried?
|
104
|
+
end
|
105
|
+
# => [false, false, ... ]
|
106
|
+
user.bury! # on succeed returns `true`, on failure raise Exception
|
107
|
+
# => true
|
108
|
+
user.buried?
|
109
|
+
# => true
|
110
|
+
user.articles.map do |article|
|
111
|
+
article.buried?
|
112
|
+
end
|
113
|
+
# => [true, true, ... ]
|
114
|
+
user.bury!( dependent: false ) # Call `#bury!` without any effect on associations
|
115
|
+
# => true
|
116
|
+
```
|
117
|
+
|
118
|
+
`#restore` and `#restore!` are inverse of `#bury` and `#bury!`.
|
119
|
+
|
120
|
+
### Class Methods
|
121
|
+
|
122
|
+
All of class methods are using ActiveRecord Transactions.
|
123
|
+
|
124
|
+
If you want to bury all of the records in a table:
|
125
|
+
```ruby
|
126
|
+
User.bury_all
|
127
|
+
```
|
128
|
+
`#bury_all` have a dependent argument like `#bury` method.
|
129
|
+
`#bury_all!` is similar to `#bury_all` method except it raise `Exception` on failure.
|
130
|
+
`#restore_all` and `#restore_all!` are inverse of `#bury_all` and `#bury_all!`.
|
131
|
+
|
132
|
+
### Query Methods
|
133
|
+
|
134
|
+
If you're looking for buried records:
|
135
|
+
```ruby
|
136
|
+
User.where_buried
|
137
|
+
```
|
138
|
+
If you're looking for normal records:
|
139
|
+
```ruby
|
140
|
+
User.where_not_buried
|
141
|
+
```
|
142
|
+
If you're looking for all records:
|
143
|
+
```ruby
|
144
|
+
User.all
|
145
|
+
```
|
146
|
+
If you're looking for buried/normal/all records on an association:
|
147
|
+
```ruby
|
148
|
+
user.articles.where_buried
|
149
|
+
user.articles.where_not_buried
|
150
|
+
user.articles
|
151
|
+
```
|
152
|
+
as you see Closet didn't change activerecord default behaviour.
|
153
|
+
|
154
|
+
### Validations
|
155
|
+
#### Uniqueness
|
156
|
+
If you want to use uniqueness validation for records where not buried:
|
157
|
+
```ruby
|
158
|
+
validates :property, uniqueness: { conditions: -> { where_not_buried } }
|
159
|
+
```
|
160
|
+
If you want to use uniqueness validation for records where buried:
|
161
|
+
```ruby
|
162
|
+
validates :property, uniqueness: { conditions: -> { where_buried } }
|
163
|
+
```
|
164
|
+
If you want to use the uniqueness for all of the records:
|
165
|
+
```ruby
|
166
|
+
validates :property, uniqueness: true
|
167
|
+
```
|
168
|
+
|
169
|
+
### Callbacks
|
170
|
+
Closet provides a few callbacks,`bury` callback triggered after/around/before a record gets buried, `restore` callback triggered after/around/before a record gets restored.
|
171
|
+
```ruby
|
172
|
+
class User < ActiveRecord::Base
|
173
|
+
# define associations
|
174
|
+
include Closet
|
175
|
+
|
176
|
+
before_bury :do_something
|
177
|
+
around_bury :do_something
|
178
|
+
after_bury :do_something
|
179
|
+
|
180
|
+
before_restore :do_something
|
181
|
+
around_restore :do_something
|
182
|
+
after_restore :do_something
|
183
|
+
|
184
|
+
end
|
185
|
+
```
|
186
|
+
|
187
|
+
|
188
|
+
## Contributing
|
189
|
+
|
190
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/closet. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
191
|
+
|
192
|
+
|
193
|
+
## License
|
194
|
+
|
195
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
196
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "closet"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/closet.gemspec
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'closet/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "closet"
|
8
|
+
spec.version = Closet::VERSION
|
9
|
+
spec.authors = ["Ehsan Yousefi"]
|
10
|
+
spec.email = ["ehsan.yousefi@live.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Closet let you bury your records instead of killing them.}
|
13
|
+
spec.description = %q{Closet let you bury your records instead of killing them. Data is valuable even thoes you think worthless. Closet helps you bury/hide your records in closet, and restore them whenever you want. Colest only works with ActiveRecord(Mongoid will support in near future) }
|
14
|
+
spec.homepage = "https://github.com/EhsanYousefi/closet"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
# if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
# else
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
# end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
33
|
+
spec.add_development_dependency "rspec-mocks", "~> 3.0"
|
34
|
+
spec.add_development_dependency "pry"
|
35
|
+
spec.add_development_dependency "factory_girl", "~> 4.0"
|
36
|
+
spec.add_development_dependency "database_cleaner", "~> 1.0"
|
37
|
+
|
38
|
+
|
39
|
+
spec.add_dependency "activerecord", "~> 4.0"
|
40
|
+
|
41
|
+
end
|
data/lib/closet.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'active_record' unless defined? ActiveRecord
|
2
|
+
require 'closet/version'
|
3
|
+
require 'closet/reflection_handler'
|
4
|
+
require 'closet/callbacks'
|
5
|
+
require 'closet/bury/class_methods'
|
6
|
+
require 'closet/bury/instance_methods'
|
7
|
+
require 'closet/restore/class_methods'
|
8
|
+
require 'closet/restore/instance_methods'
|
9
|
+
require 'closet/query/class_methods'
|
10
|
+
|
11
|
+
module Closet
|
12
|
+
def self.included(klass)
|
13
|
+
# Includes
|
14
|
+
klass.include Bury::InstanceMethods
|
15
|
+
klass.include Restore::InstanceMethods
|
16
|
+
# Extends
|
17
|
+
klass.extend Bury::ClassMethods
|
18
|
+
klass.extend Restore::ClassMethods
|
19
|
+
klass.extend Query::ClassMethods
|
20
|
+
# Define necceary callbacks like: before_bury, before_vivify and so on...
|
21
|
+
Callbacks.define( klass )
|
22
|
+
# Handle reflection dependets
|
23
|
+
ReflectionHandler.new( klass ).handle_dependencies
|
24
|
+
end
|
25
|
+
end
|
Binary file
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Closet
|
2
|
+
module Bury
|
3
|
+
module ClassMethods
|
4
|
+
|
5
|
+
def bury!( id, options= { dependent: true } )
|
6
|
+
if options.with_indifferent_access[:dependent]
|
7
|
+
self.find(id).bury!
|
8
|
+
else
|
9
|
+
self.find(id).bury!( dependent: false )
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def bury( id, options= { dependent: true } )
|
14
|
+
begin
|
15
|
+
self.bury!( id, dependent: options.with_indifferent_access[:dependent] )
|
16
|
+
rescue
|
17
|
+
false
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def buried?(id)
|
22
|
+
self.find(id).buried_at != nil
|
23
|
+
end
|
24
|
+
|
25
|
+
def bury_all!( options = { dependent: true } )
|
26
|
+
if options.with_indifferent_access[:dependent]
|
27
|
+
ActiveRecord::Base.transaction do
|
28
|
+
self.all.each do |record|
|
29
|
+
record.bury!
|
30
|
+
end
|
31
|
+
end
|
32
|
+
else
|
33
|
+
ActiveRecord::Base.transaction do
|
34
|
+
self.all.each do |record|
|
35
|
+
record.bury!( dependent: false )
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def bury_all( options = { dependent: true } )
|
42
|
+
begin
|
43
|
+
self.bury_all!( dependent: options.with_indifferent_access[:dependent] )
|
44
|
+
rescue
|
45
|
+
false
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Closet
|
2
|
+
module Bury
|
3
|
+
module InstanceMethods
|
4
|
+
|
5
|
+
def bury( options = { dependent: true } )
|
6
|
+
begin
|
7
|
+
self.bury!( dependent: options.with_indifferent_access[:dependent] )
|
8
|
+
rescue
|
9
|
+
false
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def bury!( options= { dependent: true } )
|
14
|
+
if options.with_indifferent_access[:dependent]
|
15
|
+
ActiveRecord::Base.transaction do
|
16
|
+
run_callbacks( :bury ) do
|
17
|
+
flag_as_buried
|
18
|
+
end
|
19
|
+
end
|
20
|
+
else
|
21
|
+
flag_as_buried
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def buried?
|
26
|
+
self.reload && self.buried_at != nil
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def flag_as_buried
|
32
|
+
update_columns(buried_at: Time.now)
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module Closet
|
2
|
+
class ReflectionHandler
|
3
|
+
|
4
|
+
def initialize( klass )
|
5
|
+
@klass = klass
|
6
|
+
end
|
7
|
+
|
8
|
+
def handle_dependencies
|
9
|
+
@klass.reflections.each do |k,v|
|
10
|
+
handle_dependency( k,v )
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def handle_dependency( name , metadata )
|
15
|
+
include_closet( metadata.klass )
|
16
|
+
dependent = metadata.options.with_indifferent_access[:dependent]
|
17
|
+
send( metadata.macro, name, metadata, metadata.klass, dependent ) if dependent
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def has_many( name, metadata, klazz, dependent )
|
23
|
+
return if metadata.through_reflection
|
24
|
+
if dependent == :destroy
|
25
|
+
send( :destroy_all, name )
|
26
|
+
else
|
27
|
+
send( dependent, name )
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def belongs_to( name, metadata, klazz, dependent)
|
32
|
+
send( dependent, name )
|
33
|
+
end
|
34
|
+
|
35
|
+
def has_one( name, metadata, klazz, dependent )
|
36
|
+
return if metadata.through_reflection
|
37
|
+
if dependent == :destroy
|
38
|
+
send( :destroy_before_bury, name )
|
39
|
+
else
|
40
|
+
send( :delete_before_bury, name )
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def destroy( reflection_name )
|
45
|
+
@klass.after_bury lambda { |r| r.send( reflection_name ).bury! }
|
46
|
+
@klass.after_restore lambda { |r| r.send( reflection_name ).restore! }
|
47
|
+
end
|
48
|
+
|
49
|
+
def destroy_before_bury ( reflection_name )
|
50
|
+
@klass.before_bury lambda { |r| r.send( reflection_name ).bury! if r.send( reflection_name ) }
|
51
|
+
@klass.before_restore lambda { |r| r.send( reflection_name ).restore! if r.send( reflection_name ) }
|
52
|
+
end
|
53
|
+
|
54
|
+
def destroy_all( reflection_name )
|
55
|
+
@klass.before_bury lambda { |r| r.send( reflection_name ).where_not_buried.bury_all! }
|
56
|
+
@klass.before_restore lambda { |r| r.send( reflection_name ).where_buried.restore_all! }
|
57
|
+
end
|
58
|
+
|
59
|
+
def delete_all( reflection_name )
|
60
|
+
@klass.before_bury lambda { |r| r.send( reflection_name ).bury_all!( dependent: false )}
|
61
|
+
@klass.before_restore lambda { |r| r.send( reflection_name ).restore_all!( dependent: false )}
|
62
|
+
end
|
63
|
+
|
64
|
+
def delete( reflection_name )
|
65
|
+
@klass.after_bury lambda { |r| r.send( reflection_name ).bury!( dependent: false )}
|
66
|
+
@klass.after_restore lambda { |r| r.send( reflection_name ).restore!( dependent: false )}
|
67
|
+
end
|
68
|
+
|
69
|
+
def delete_before_bury ( reflection_name )
|
70
|
+
@klass.before_bury lambda { |r| r.send( reflection_name ).bury!( dependent: false ) if r.send( reflection_name ) }
|
71
|
+
@klass.before_restore lambda { |r| r.send( reflection_name ).restore!( dependent: false ) if r.send( reflection_name ) }
|
72
|
+
end
|
73
|
+
|
74
|
+
def include_closet( klazz )
|
75
|
+
klazz.include(Closet) unless klazz.include?( Closet )
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Closet
|
2
|
+
module Restore
|
3
|
+
module ClassMethods
|
4
|
+
|
5
|
+
def restore!( id, options= { dependent: true } )
|
6
|
+
if options.with_indifferent_access[:dependent]
|
7
|
+
self.find(id).restore!
|
8
|
+
else
|
9
|
+
self.find(id).restore!( dependent: false )
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def restore( id, options= { dependent: true } )
|
14
|
+
begin
|
15
|
+
self.restore!( id, dependent: options.with_indifferent_access[:dependent] )
|
16
|
+
rescue
|
17
|
+
false
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def restore_all!( options = { dependent: true } )
|
22
|
+
if options.with_indifferent_access[:dependent]
|
23
|
+
ActiveRecord::Base.transaction do
|
24
|
+
self.all.lazy.each do |record|
|
25
|
+
record.restore!
|
26
|
+
end
|
27
|
+
end
|
28
|
+
else
|
29
|
+
ActiveRecord::Base.transaction do
|
30
|
+
self.all.lazy.each do |record|
|
31
|
+
record.restore!( dependent: false )
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def restore_all( options = { dependent: true } )
|
38
|
+
begin
|
39
|
+
self.restore_all!( dependent: options.with_indifferent_access[:dependent] )
|
40
|
+
rescue
|
41
|
+
false
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Closet
|
2
|
+
module Restore
|
3
|
+
module InstanceMethods
|
4
|
+
|
5
|
+
def restore!( options = { dependent: true } )
|
6
|
+
if options.with_indifferent_access[:dependent]
|
7
|
+
ActiveRecord::Base.transaction do
|
8
|
+
run_callbacks( :restore ) do
|
9
|
+
flag_as_restored
|
10
|
+
end
|
11
|
+
end
|
12
|
+
else
|
13
|
+
flag_as_restored
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def restore( options = { dependent: true } )
|
18
|
+
begin
|
19
|
+
self.restore!( dependent: options.with_indifferent_access[:dependent] )
|
20
|
+
rescue
|
21
|
+
false
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def flag_as_restored
|
28
|
+
update_columns(buried_at: nil)
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,181 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: closet
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ehsan Yousefi
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec-mocks
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.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'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: factory_girl
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '4.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '4.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: database_cleaner
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '1.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '1.0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: activerecord
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '4.0'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '4.0'
|
125
|
+
description: 'Closet let you bury your records instead of killing them. Data is valuable
|
126
|
+
even thoes you think worthless. Closet helps you bury/hide your records in closet,
|
127
|
+
and restore them whenever you want. Colest only works with ActiveRecord(Mongoid
|
128
|
+
will support in near future) '
|
129
|
+
email:
|
130
|
+
- ehsan.yousefi@live.com
|
131
|
+
executables: []
|
132
|
+
extensions: []
|
133
|
+
extra_rdoc_files: []
|
134
|
+
files:
|
135
|
+
- ".gitignore"
|
136
|
+
- ".rspec"
|
137
|
+
- ".ruby-version"
|
138
|
+
- ".travis.yml"
|
139
|
+
- CODE_OF_CONDUCT.md
|
140
|
+
- Gemfile
|
141
|
+
- LICENSE.txt
|
142
|
+
- README.md
|
143
|
+
- Rakefile
|
144
|
+
- bin/console
|
145
|
+
- bin/setup
|
146
|
+
- closet.gemspec
|
147
|
+
- lib/closet.rb
|
148
|
+
- lib/closet/.DS_Store
|
149
|
+
- lib/closet/bury/class_methods.rb
|
150
|
+
- lib/closet/bury/instance_methods.rb
|
151
|
+
- lib/closet/callbacks.rb
|
152
|
+
- lib/closet/query/class_methods.rb
|
153
|
+
- lib/closet/reflection_handler.rb
|
154
|
+
- lib/closet/restore/class_methods.rb
|
155
|
+
- lib/closet/restore/instance_methods.rb
|
156
|
+
- lib/closet/version.rb
|
157
|
+
homepage: https://github.com/EhsanYousefi/closet
|
158
|
+
licenses:
|
159
|
+
- MIT
|
160
|
+
metadata: {}
|
161
|
+
post_install_message:
|
162
|
+
rdoc_options: []
|
163
|
+
require_paths:
|
164
|
+
- lib
|
165
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
166
|
+
requirements:
|
167
|
+
- - ">="
|
168
|
+
- !ruby/object:Gem::Version
|
169
|
+
version: '0'
|
170
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
171
|
+
requirements:
|
172
|
+
- - ">="
|
173
|
+
- !ruby/object:Gem::Version
|
174
|
+
version: '0'
|
175
|
+
requirements: []
|
176
|
+
rubyforge_project:
|
177
|
+
rubygems_version: 2.4.5.1
|
178
|
+
signing_key:
|
179
|
+
specification_version: 4
|
180
|
+
summary: Closet let you bury your records instead of killing them.
|
181
|
+
test_files: []
|