acts_as_removable 0.9.4 → 0.9.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/acts_as_removable/version.rb +1 -1
- data/lib/acts_as_removable.rb +30 -34
- data/spec/acts_as_removable_spec.rb +4 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b2e52df51683c7203a4a78ed2f72ce9b5c72d464
|
4
|
+
data.tar.gz: 9a39dbee521544bdcdae76250996c6dcb12e1fbf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6f16a0de9785d79ffd75790c7b4872e6d7119fc9afc474547fa3aaaa6815e95e2de6e0bc5dbe12384d43864c29e44d6c064649c2fe6cc3ebac76358f103cc9a7
|
7
|
+
data.tar.gz: 4e0651c43989baa13f8624f58513ce2df8adbd8c2b55a75e99f983006c6669a9e9448168e5570cea926c2e131ad4d32ecd835fa6b4b7eef8f24647a6122bf1ed
|
data/lib/acts_as_removable.rb
CHANGED
@@ -5,12 +5,7 @@ require "acts_as_removable/version"
|
|
5
5
|
module ActsAsRemovable
|
6
6
|
extend ActiveSupport::Concern
|
7
7
|
|
8
|
-
included do
|
9
|
-
extend(ClassMethods)
|
10
|
-
end
|
11
|
-
|
12
8
|
module ClassMethods
|
13
|
-
|
14
9
|
# Add ability to remove ActiveRecord instances
|
15
10
|
#
|
16
11
|
# acts_as_removable
|
@@ -21,29 +16,26 @@ module ActsAsRemovable
|
|
21
16
|
#
|
22
17
|
# * <tt>:column_name</tt> - A symbol or string with the column to use for removal timestamp.
|
23
18
|
# * <tt>:without_default_scope</tt> - A boolean indicating to not set a default scope.
|
24
|
-
def acts_as_removable(options={})
|
25
|
-
|
26
|
-
@_acts_as_removable_column_name = (options[:column_name] || 'removed_at').to_s
|
27
|
-
def self._acts_as_removable_column_name
|
28
|
-
@_acts_as_removable_column_name
|
29
|
-
end
|
19
|
+
def acts_as_removable(options = {})
|
20
|
+
_acts_as_removable_options.merge!(options)
|
30
21
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
22
|
+
scope :removed, -> {
|
23
|
+
removed_at_column_name = _acts_as_removable_options[:column_name]
|
24
|
+
query = where(all.table[removed_at_column_name].not_eq(nil).to_sql)
|
25
|
+
_removable_where_values(query, removed_at_column_name, all.table[removed_at_column_name].eq(nil).to_sql)
|
26
|
+
}
|
36
27
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
28
|
+
scope :actives, -> {
|
29
|
+
removed_at_column_name = _acts_as_removable_options[:column_name]
|
30
|
+
query = where(all.table[removed_at_column_name].eq(nil).to_sql)
|
31
|
+
_removable_where_values(query, removed_at_column_name, all.table[removed_at_column_name].not_eq(nil).to_sql)
|
32
|
+
}
|
42
33
|
|
43
|
-
|
34
|
+
default_scope -> {where(all.table[_acts_as_removable_options[:column_name]].eq(nil).to_sql)} unless _acts_as_removable_options[:without_default_scope]
|
44
35
|
|
45
|
-
|
36
|
+
define_model_callbacks :remove, :unremove
|
46
37
|
|
38
|
+
class_eval do
|
47
39
|
def self.before_remove(*args, &block)
|
48
40
|
set_callback :remove, :before, *args, &block
|
49
41
|
end
|
@@ -61,7 +53,7 @@ module ActsAsRemovable
|
|
61
53
|
end
|
62
54
|
|
63
55
|
def removed?
|
64
|
-
|
56
|
+
send(self.class._acts_as_removable_options[:column_name]).present?
|
65
57
|
end
|
66
58
|
|
67
59
|
def remove(options={})
|
@@ -80,23 +72,27 @@ module ActsAsRemovable
|
|
80
72
|
_update_remove_attribute(:unremove, nil, true, options)
|
81
73
|
end
|
82
74
|
|
83
|
-
private
|
84
|
-
|
85
75
|
def _update_remove_attribute(callback, value, with_bang=false, options={})
|
86
76
|
run_callbacks callback.to_sym do
|
87
|
-
|
88
|
-
with_bang ?
|
77
|
+
send("#{self.class._acts_as_removable_options[:column_name]}=", value)
|
78
|
+
with_bang ? save!(options) : save(options)
|
89
79
|
end
|
90
80
|
end
|
91
81
|
end
|
92
82
|
end
|
93
|
-
end
|
94
83
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
84
|
+
def _acts_as_removable_options
|
85
|
+
@_acts_as_removable_options ||= {
|
86
|
+
column_name: 'removed_at'
|
87
|
+
}
|
88
|
+
end
|
89
|
+
|
90
|
+
# Delete where statements from query
|
91
|
+
def _removable_where_values(query, column_name, remove_where)
|
92
|
+
query = query.with_default_scope if query.respond_to?(:with_default_scope)
|
93
|
+
query.where_values.delete(remove_where)
|
94
|
+
query
|
95
|
+
end
|
100
96
|
end
|
101
97
|
end
|
102
98
|
|
@@ -28,7 +28,7 @@ describe 'acts_as_removable' do
|
|
28
28
|
db_file = File.expand_path(File.join(File.dirname(__FILE__), '..', 'tmp', 'acts_as_removable.db'))
|
29
29
|
Dir::mkdir(File.dirname(db_file)) unless File.exists?(File.dirname(db_file))
|
30
30
|
ActiveRecord::Base.establish_connection(
|
31
|
-
:adapter =>
|
31
|
+
:adapter => 'sqlite3',
|
32
32
|
:database => "#{File.expand_path(File.join(File.dirname(__FILE__), '..'))}/tmp/acts_as_removable.db"
|
33
33
|
)
|
34
34
|
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS 'my_models'")
|
@@ -42,7 +42,7 @@ describe 'acts_as_removable' do
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
-
it
|
45
|
+
it 'test column and check method' do
|
46
46
|
[[MyModel.create!, :removed_at], [MySecondModel.create!, :use_this_column]].each do |r, column_name|
|
47
47
|
r.removed?.should be_false
|
48
48
|
r.send(column_name).should be_nil
|
@@ -53,7 +53,7 @@ describe 'acts_as_removable' do
|
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
|
-
it
|
56
|
+
it 'test scopes' do
|
57
57
|
MyModel.delete_all
|
58
58
|
MySecondModel.delete_all
|
59
59
|
|
@@ -73,7 +73,7 @@ describe 'acts_as_removable' do
|
|
73
73
|
MySecondModel.unscoped.count.should be(2)
|
74
74
|
end
|
75
75
|
|
76
|
-
it
|
76
|
+
it 'test callbacks' do
|
77
77
|
r = MyModel.create!
|
78
78
|
r.callback_before_remove.should be_false
|
79
79
|
r.callback_after_remove.should be_false
|
@@ -93,6 +93,5 @@ describe 'acts_as_removable' do
|
|
93
93
|
r.callback_after_remove.should be_true
|
94
94
|
r.callback_before_unremove.should be_true
|
95
95
|
r.callback_after_unremove.should be_true
|
96
|
-
|
97
96
|
end
|
98
97
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Florian Schwab
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-07-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|