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
49
50
51
52
53
54
# 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.attributes.clone
    foreign_key   = history_class.history_foreign_key
  
    attrs.merge!(foreign_key => attrs["id"], history_started_at: now, history_user_id: history_user_id)
  
    attrs = attrs.except("id")

    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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/historiographer/relation.rb', line 60

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.attributes.clone
          foreign_key   = history_class.history_foreign_key
    
          now = UTC.now
          attrs.merge!(foreign_key => attrs["id"], history_started_at: now, history_user_id: history_user_id, deleted_at: now)
    
          attrs = attrs.except("id")

          record.histories.build(attrs)
        end
        history_class.import new_histories
      end

      super()
    end
  end
end

#delete_all_without_historyObject



56
57
58
# File 'lib/historiographer/relation.rb', line 56

def delete_all_without_history
  delete_all(nil, false)
end

#destroy_all(history_user_id: nil) ⇒ Object



97
98
99
# File 'lib/historiographer/relation.rb', line 97

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



93
94
95
# File 'lib/historiographer/relation.rb', line 93

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)
  unless histories
    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