lib/active_archive/base.rb in active_archive-5.2.2 vs lib/active_archive/base.rb in active_archive-5.3.0

- old
+ new

@@ -43,10 +43,16 @@ with_transaction_returning_status do if unarchivable? || should_force_destroy?(force) permanently_delete_records_after { super() } else destroy_with_active_archive(force) + + if ::ActiveRecord::VERSION::MAJOR >= 5 + archived_at_will_change! + elsif ::ActiveRecord::VERSION::MAJOR >= 4 + attribute_will_change!('archived_at') + end end end end alias_method :archive, :destroy @@ -78,13 +84,22 @@ def get_archived_record self.class.unscoped.find(id) end + # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity def set_archived_at(value, force = nil) return self unless archivable? + record = get_archived_record + + if ::ActiveRecord::VERSION::MAJOR >= 5 + record.archived_at_will_change! + elsif ::ActiveRecord::VERSION::MAJOR >= 4 + record.attribute_will_change!('archived_at') + end + record.archived_at = value begin should_ignore_validations?(force) ? record.save(validate: false) : record.save! @@ -102,10 +117,11 @@ rescue => error record.destroy raise error end end + # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity def each_counter_cache _reflections.each do |name, reflection| next unless respond_to?(name.to_sym) @@ -180,10 +196,11 @@ def dependent_record_ids dependent_reflections(self.class).reduce({}) do |records, (key, _)| found = Array(send(key)).compact next records if found.empty? + records.update(found.first.class => found.map(&:id)) end end def permanently_delete_records_after(&block) @@ -196,10 +213,11 @@ def permanently_delete_records(dependent_records) dependent_records.each do |klass, ids| ids.each do |id| record = klass.unscoped.where(klass.primary_key => id).first next unless record + record.archived_at = nil record.destroy(:force) end end end @@ -230,7 +248,7 @@ end end ActiveSupport.on_load(:active_record) do - ActiveRecord::Base.send :include, ActiveArchive::Base + ActiveRecord::Base.include(ActiveArchive::Base) end