Module: Historiographer::Relation

Extended by:
ActiveSupport::Concern
Defined in:
lib/historiographer/relation.rb

Instance Method Summary collapse

Instance Method Details

#bulk_record_history(records, updates = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/historiographer/relation.rb', line 31

def bulk_record_history(records, updates = {})
  now = UTC.now
  history_class = self.klass.history_class

  records.new.send(:history_user_absent_action) if updates[:history_user_id].nil?
  history_user_id = updates[:history_user_id]

  new_histories = records.map do |record|
    attrs = record.history_attrs(now: now)
    record.histories.build(attrs)
  end

  current_histories = history_class.current.where("#{history_class.history_foreign_key} IN (?)", records.map(&:id))

  current_histories.update_all(history_ended_at: now)

  history_class.import new_histories
end

#delete_all(options = {}, histories = true) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/historiographer/relation.rb', line 54

def delete_all(options={}, histories=true)
  unless histories
    super()
  else
    ActiveRecord::Base.transaction do
      records = self
      history_class = records.first.class.history_class
      history_user_id = options[:history_user_id]
      records.first.send(:history_user_absent_action) if history_user_id.nil?
      now = UTC.now

      history_class.current.where("#{history_class.history_foreign_key} IN (?)", records.map(&:id)).update_all(history_ended_at: now)

      if records.first.respond_to?(:paranoia_destroy)
        new_histories = records.map do |record|
          attrs = record.history_attrs(now: now)
          attrs[:history_user_id] = history_user_id
          attrs[:deleted_at] = now
          record.histories.build(attrs)
        end
        history_class.import new_histories
      end

      super()
    end
  end
end

#delete_all_without_historyObject



50
51
52
# File 'lib/historiographer/relation.rb', line 50

def delete_all_without_history
  delete_all(nil, false)
end

#destroy_all(history_user_id: nil) ⇒ Object



86
87
88
# File 'lib/historiographer/relation.rb', line 86

def destroy_all(history_user_id: nil)
  records.each { |r| r.destroy(history_user_id: history_user_id) }.tap { reset }
end

#destroy_all_without_historyObject



82
83
84
# File 'lib/historiographer/relation.rb', line 82

def destroy_all_without_history
  records.each(&:destroy_without_history).tap { reset }
end

#has_histories?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/historiographer/relation.rb', line 5

def has_histories?
  self.klass.respond_to?(:history_class)
end

#update_all(updates, histories = true) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/historiographer/relation.rb', line 13

def update_all(updates, histories=true)
  if !histories || self.model.is_history_class?
    super(updates)
  else
    updates.symbolize_keys!
    model_changes = updates.except(:history_user_id)

    ActiveRecord::Base.transaction do
      changed_records = select do |record|
        !(record.attributes.symbolize_keys >= model_changes)
      end

      super(model_changes)
      bulk_record_history(self.reload.where(id: changed_records.pluck(:id)), updates)
    end
  end
end

#update_all_without_history(updates) ⇒ Object



9
10
11
# File 'lib/historiographer/relation.rb', line 9

def update_all_without_history(updates)
  update_all(updates, false)
end