Module: Snaptime::ArHooks

Defined in:
lib/snaptime/ar_hooks.rb

Class Method Summary collapse

Class Method Details

.after_create(record) ⇒ Object



12
13
14
# File 'lib/snaptime/ar_hooks.rb', line 12

def self.after_create(record)
  Snaptime.record_cloned_in_current_tx(record)
end

.before_create(record) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/snaptime/ar_hooks.rb', line 3

def self.before_create(record)
  if record.natural_id.nil?
    record.valid_from = Snaptime.current_now
    ActiveRecord::Base.uncached do
      record.natural_id = record.class.connection.next_sequence_value(record.class.sequence_name)
    end
  end
end

.before_update(record) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/snaptime/ar_hooks.rb', line 16

def self.before_update(record)
  return unless Snaptime.record_cloning_enabled?

  if record.valid_to.nil? && record.changed? && !Snaptime.record_cloned_in_current_tx?(record)
    record.valid_from = Snaptime.current_now

    Snaptime::RecordCloner.clone_record!(
      record,
      override_attributes: { valid_to: record.valid_from - SMALLEST_TIME_UNIT },
      return_record: false
    )

    Snaptime.record_cloned_in_current_tx(record)
  end
end

.destroy(record) ⇒ Object



32
33
34
35
36
# File 'lib/snaptime/ar_hooks.rb', line 32

def self.destroy(record)
  record.deleted = true
  record.version_is_minor = true if record.respond_to?(:version_is_minor=)
  record.save!
end