acts_as_removable 0.9.0 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/acts_as_removable.gemspec +2 -2
- data/lib/acts_as_removable/version.rb +1 -1
- data/lib/acts_as_removable.rb +34 -9
- data/spec/acts_as_removable_spec.rb +21 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f55f6c4ecc0f0ced45e825af183aad144e21a974
|
4
|
+
data.tar.gz: 4602fa5ebca4018257d7b799ea11cb23f4837a60
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aecc89b6b88285c230e23a58db97b9c22f7291b9266f195fd93ece71cb2b68aa0cfb1e61798f532547acde46a9e9d11970c32443b844150c40d13203958030cf
|
7
|
+
data.tar.gz: 57b8ea17f54ec4d3c37f354be41b666d68d0d73521ce92d78321e707a28e377acbe06b11801ba3536d3b4fdd9f0227caddf1daefedf31cdb2e40ec5d63af9021
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# acts_as_removable
|
2
2
|
|
3
3
|
This gem allows you to easily manage ActiveRecord objects that are pseudo destroyed a.k.a. removed.
|
4
4
|
|
@@ -42,7 +42,7 @@ And you can use callbacks:
|
|
42
42
|
class MyModel < ActiveRecord::Base
|
43
43
|
acts_as_removable
|
44
44
|
|
45
|
-
|
45
|
+
before_remove do |r|
|
46
46
|
puts "Before removing record"
|
47
47
|
end
|
48
48
|
|
data/acts_as_removable.gemspec
CHANGED
@@ -18,8 +18,8 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency "activesupport", "~> 4.0.0
|
22
|
-
spec.add_dependency "activerecord", "~> 4.0.0
|
21
|
+
spec.add_dependency "activesupport", "~> 4.0.0"
|
22
|
+
spec.add_dependency "activerecord", "~> 4.0.0"
|
23
23
|
|
24
24
|
spec.add_development_dependency "bundler", "~> 1.3"
|
25
25
|
spec.add_development_dependency "rake"
|
data/lib/acts_as_removable.rb
CHANGED
@@ -10,15 +10,27 @@ module ActsAsRemovable
|
|
10
10
|
end
|
11
11
|
|
12
12
|
module ClassMethods
|
13
|
+
|
14
|
+
# Add ability to remove ActiveRecord instances
|
15
|
+
#
|
16
|
+
# acts_as_removable
|
17
|
+
# acts_as_removable column_name: 'other_column_name'
|
18
|
+
# acts_as_removable without_default_scope: true
|
19
|
+
#
|
20
|
+
# ===== Options
|
21
|
+
#
|
22
|
+
# * <tt>:column_name</tt> - A symbol or string with the column to use for removal timestamp.
|
23
|
+
# * <tt>:without_default_scope</tt> - A boolean indicating to not set a default scope.
|
13
24
|
def acts_as_removable(options={})
|
14
25
|
self.class_eval do
|
15
|
-
@_acts_as_removable_column_name = options[:column_name] || 'removed_at'
|
26
|
+
@_acts_as_removable_column_name = (options[:column_name] || 'removed_at').to_s
|
16
27
|
def self._acts_as_removable_column_name
|
17
28
|
@_acts_as_removable_column_name
|
18
29
|
end
|
19
30
|
|
20
|
-
|
21
|
-
scope :
|
31
|
+
scope :removed, -> {unscoped.where("#{self.table_name}.#{self._acts_as_removable_column_name} IS NOT NULL")}
|
32
|
+
scope :actives, -> {unscoped.where("#{self.table_name}.#{self._acts_as_removable_column_name} IS NULL")}
|
33
|
+
default_scope -> {actives} unless options[:without_default_scope]
|
22
34
|
|
23
35
|
define_callbacks :remove
|
24
36
|
|
@@ -34,15 +46,28 @@ module ActsAsRemovable
|
|
34
46
|
self.send(self.class._acts_as_removable_column_name).present?
|
35
47
|
end
|
36
48
|
|
37
|
-
def remove
|
38
|
-
|
39
|
-
|
40
|
-
|
49
|
+
def remove(options={})
|
50
|
+
_update_remove_attribute(Time.now, false, options)
|
51
|
+
end
|
52
|
+
|
53
|
+
def remove!(options={})
|
54
|
+
_update_remove_attribute(Time.now, true, options)
|
41
55
|
end
|
42
56
|
|
43
|
-
def
|
57
|
+
def unremove(options={})
|
58
|
+
_update_remove_attribute(nil, false, options)
|
59
|
+
end
|
60
|
+
|
61
|
+
def unremove!(options={})
|
62
|
+
_update_remove_attribute(nil, true, options)
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def _update_remove_attribute(value, with_bang=false, options={})
|
44
68
|
run_callbacks :remove do
|
45
|
-
self.
|
69
|
+
self.send("#{self.class._acts_as_removable_column_name}=", value)
|
70
|
+
with_bang ? self.save!(options) : self.save(options)
|
46
71
|
end
|
47
72
|
end
|
48
73
|
end
|
@@ -13,7 +13,7 @@ describe 'acts_as_removable' do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
class MySecondModel < ActiveRecord::Base
|
16
|
-
acts_as_removable column_name: :use_this_column
|
16
|
+
acts_as_removable column_name: :use_this_column, without_default_scope: true
|
17
17
|
end
|
18
18
|
|
19
19
|
before do
|
@@ -46,6 +46,26 @@ describe 'acts_as_removable' do
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
+
it "test scopes" do
|
50
|
+
MyModel.delete_all
|
51
|
+
MySecondModel.delete_all
|
52
|
+
|
53
|
+
MyModel.create!
|
54
|
+
MyModel.create!.remove!
|
55
|
+
MySecondModel.create!
|
56
|
+
MySecondModel.create!.remove!
|
57
|
+
|
58
|
+
MyModel.count.to_i.should be(1)
|
59
|
+
MyModel.actives.count.should be(1)
|
60
|
+
MyModel.removed.count.should be(1)
|
61
|
+
MyModel.unscoped.count.should be(2)
|
62
|
+
|
63
|
+
MySecondModel.count.to_i.should be(2)
|
64
|
+
MySecondModel.actives.count.should be(1)
|
65
|
+
MySecondModel.removed.count.should be(1)
|
66
|
+
MySecondModel.unscoped.count.should be(2)
|
67
|
+
end
|
68
|
+
|
49
69
|
it "test callbacks" do
|
50
70
|
r = MyModel.create!
|
51
71
|
r.callback_before.should be_false
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_removable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Florian Schwab
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-06-
|
11
|
+
date: 2013-06-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 4.0.0
|
19
|
+
version: 4.0.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 4.0.0
|
26
|
+
version: 4.0.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activerecord
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ~>
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 4.0.0
|
33
|
+
version: 4.0.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 4.0.0
|
40
|
+
version: 4.0.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: bundler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|