forked from ActsAsParanoid/acts_as_paranoid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.rb
52 lines (49 loc) · 2.63 KB
/
init.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class << ActiveRecord::Base
def belongs_to_with_deleted(association_id, options = {})
with_deleted = options.delete :with_deleted
returning belongs_to_without_deleted(association_id, options) do
if with_deleted and options[:polymorphic]
reflection = reflect_on_association(association_id)
association_accessor_methods(reflection, Caboose::Acts::BelongsToWithDeletedPolymorphicAssociation)
association_constructor_method(:build, reflection, Caboose::Acts::BelongsToWithDeletedPolymorphicAssociation)
association_constructor_method(:create, reflection, Caboose::Acts::BelongsToWithDeletedPolymorphicAssociation)
elsif with_deleted
reflection = reflect_on_association(association_id)
association_accessor_methods(reflection, Caboose::Acts::BelongsToWithDeletedAssociation)
association_constructor_method(:build, reflection, Caboose::Acts::BelongsToWithDeletedAssociation)
association_constructor_method(:create, reflection, Caboose::Acts::BelongsToWithDeletedAssociation)
end
end
end
def has_many_without_deleted(association_id, options = {}, &extension)
with_deleted = options.delete :with_deleted
returning has_many_with_deleted(association_id, options, &extension) do
if options[:through] && !with_deleted
reflection = reflect_on_association(association_id)
collection_reader_method(reflection, Caboose::Acts::HasManyThroughWithoutDeletedAssociation)
collection_accessor_methods(reflection, Caboose::Acts::HasManyThroughWithoutDeletedAssociation, false)
end
end
end
def has_one_with_deleted(association_id, options = {})
with_deleted = options.delete :with_deleted
returning has_one_without_deleted(association_id, options) do
if with_deleted
reflection = reflect_on_association(association_id)
association_accessor_methods(reflection, Caboose::Acts::HasOneWithDeletedAssociation)
association_constructor_method(:build, reflection, Caboose::Acts::HasOneWithDeletedAssociation)
association_constructor_method(:create, reflection, Caboose::Acts::HasOneWithDeletedAssociation)
end
end
end
alias_method_chain :has_one, :deleted
alias_method_chain :belongs_to, :deleted
alias_method :has_many_with_deleted, :has_many
alias_method :has_many, :has_many_without_deleted
alias_method :exists_with_deleted?, :exists?
end
ActiveRecord::Base.send :include, Caboose::Acts::Paranoid
ActiveRecord::Base.send :include, Caboose::Acts::ParanoidFindWrapper
class << ActiveRecord::Base
alias_method_chain :acts_as_paranoid, :find_wrapper
end