Module: Millstone::ActiveRecord::RelationMethods

Defined in:
lib/millstone/active_record/relation_methods.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



4
5
6
7
8
# File 'lib/millstone/active_record/relation_methods.rb', line 4

def self.extended(base)
  base.class_eval do
    attr_accessor :with_deleted_value, :only_deleted_value
  end
end

Instance Method Details

#apply_finder_options(options) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/millstone/active_record/relation_methods.rb', line 99

def apply_finder_options(options)
  return clone unless options

  finders = options.dup
  with_deleted = finders.delete(:with_deleted)
  only_deleted = finders.delete(:only_deleted)

  relation = super(finders)
  relation = relation.with_deleted(with_deleted) unless with_deleted.nil?
  relation = relation.only_deleted(only_deleted) unless only_deleted.nil?

  relation
end

#delete(id_or_array) ⇒ Object



44
45
46
# File 'lib/millstone/active_record/relation_methods.rb', line 44

def delete(id_or_array)
  where(@klass.primary_key => id_or_array).update_all(@klass.millstone_column => @klass.millstone_generate_column_value)
end

#delete!(id_or_array) ⇒ Object



48
49
50
# File 'lib/millstone/active_record/relation_methods.rb', line 48

def delete!(id_or_array)
  where(@klass.primary_key => id_or_array).delete_all!
end

#delete_all(conditions = nil) ⇒ Object



30
31
32
# File 'lib/millstone/active_record/relation_methods.rb', line 30

def delete_all(conditions = nil)
  conditions ? where(conditions).delete_all : update_all(@klass.millstone_column => @klass.millstone_generate_column_value)
end

#delete_all!(conditions = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/millstone/active_record/relation_methods.rb', line 34

def delete_all!(conditions = nil)
  if with_deleted_value.nil? and only_deleted_value.nil?
    with_deleted.delete_all!(conditions)
  elsif conditions
    where(conditions).delete_all!
  else
    arel.delete.tap { reset }
  end
end

#destroy!(id) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/millstone/active_record/relation_methods.rb', line 20

def destroy!(id)
  if with_deleted_value.nil? and only_deleted_value.nil?
    with_deleted.destroy!(id)
  elsif id.is_a?(Array)
    id.map {|one_id| destroy!(one_id) }
  else
    find(id).destroy!
  end
end

#destroy_all!(conditions = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/millstone/active_record/relation_methods.rb', line 10

def destroy_all!(conditions = nil)
  if with_deleted_value.nil? and only_deleted_value.nil?
    with_deleted.destroy_all!(conditions)
  elsif conditions
    where(conditions).destroy_all!
  else
    to_a.each {|object| object.destroy! }.tap { reset }
  end
end

#except(*skips) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/millstone/active_record/relation_methods.rb', line 73

def except(*skips)
  result = super

  if ::ActiveRecord::VERSION::TINY < 6
    result.send :apply_modules, extensions
  end

  ([:with_deleted, :only_deleted] - skips).each do |method|
    result.send(:"#{method}_value=", send(:"#{method}_value"))
  end
  result
end

#merge(r) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/millstone/active_record/relation_methods.rb', line 64

def merge(r)
  merged_relation = super
  [:with_deleted, :only_deleted].each do |method|
    value = r.send(:"#{method}_value")
    merged_relation.send(:"#{method}_value=", value) unless value.nil?
  end
  merged_relation
end

#only(*onlies) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/millstone/active_record/relation_methods.rb', line 86

def only(*onlies)
  result = super

  if ::ActiveRecord::VERSION::TINY < 6
    result.send :apply_modules, extensions
  end

  ([:with_deleted, :only_deleted] & onlies).each do |method|
    result.send(:"#{method}_value=", send(:"#{method}_value"))
  end
  result
end

#only_deleted(only = true) ⇒ Object



58
59
60
61
62
# File 'lib/millstone/active_record/relation_methods.rb', line 58

def only_deleted(only = true)
  relation = clone
  relation.only_deleted_value = only
  relation
end

#with_deleted(with = true) ⇒ Object



52
53
54
55
56
# File 'lib/millstone/active_record/relation_methods.rb', line 52

def with_deleted(with = true)
  relation = clone
  relation.with_deleted_value = with
  relation
end