Module: Journaled::RelationChangeProtection

Defined in:
lib/journaled/relation_change_protection.rb

Instance Method Summary collapse

Instance Method Details

#delete_all(force: false) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/journaled/relation_change_protection.rb', line 25

def delete_all(force: false)
  if force || !@klass.respond_to?(:journaled_attribute_names) || @klass.journaled_attribute_names.empty?
    super()
  else
    raise(<<~ERROR)
      #delete_all aborted by Journaled::Changes.

      Call .destroy_all instead to ensure journaling or invoke .delete_all(force: true)
      to override and skip journaling.
    ERROR
  end
end

#update_all(updates, force: false) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/PerceivedComplexity



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/journaled/relation_change_protection.rb', line 2

def update_all(updates, force: false) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/PerceivedComplexity
  unless force || !@klass.respond_to?(:journaled_attribute_names) || @klass.journaled_attribute_names.empty?
    conflicting_journaled_attribute_names = if updates.is_a?(Hash)
                                              @klass.journaled_attribute_names & updates.keys.map(&:to_sym)
                                            elsif updates.is_a?(String)
                                              @klass.journaled_attribute_names.select do |a|
                                                updates.match?(/\b(?<!')#{a}(?!')\b/)
                                              end
                                            else
                                              raise "unsupported type '#{updates.class}' for 'updates'"
                                            end
    raise(<<~ERROR) if conflicting_journaled_attribute_names.present?
      .update_all aborted by Journaled::Changes due to journaled attributes:

        #{conflicting_journaled_attribute_names.join(', ')}

      Consider using .all(lock: true) or .find_each with #update to ensure journaling or invoke
       .update_all with additional arg `{ force: true }` to override and skip journaling.
    ERROR
  end
  super(updates)
end