lib/active_archive/base.rb in active_archive-4.0.0 vs lib/active_archive/base.rb in active_archive-4.0.1

- old
+ new

@@ -33,11 +33,13 @@ I18n.t("active_archive.archival.#{archived? ? :archived : :unarchived}") end def unarchive(opts = nil) with_transaction_returning_status do - (should_unarchive_parent_first?(opts) ? unarchival.reverse : unarchival).each { |record| record.call(opts) } + records = should_unarchive_parent_first?(opts) ? unarchival.reverse : unarchival + records.each { |record| record.call(opts) } + self end end alias_method(:unarchive!, :unarchive) @@ -67,10 +69,11 @@ def retrieve_archived_record self.class.unscoped.find(id) end + # rubocop:disable Metrics/AbcSize def retrieve_dependent_records dependent_records = {} self.class.reflections.each do |key, reflection| next unless reflection.options[:dependent] == :destroy @@ -83,10 +86,11 @@ dependent_record.nil? ? next : dependent_records[dependent_record.class] = records.map(&:id) end dependent_records end + # rubocop:enable Metrics/AbcSize 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 @@ -129,13 +133,13 @@ [records].flatten.compact end def unarchive_destroyed_dependent_records(force = nil) self.class.reflections - .select { |_, reflection| 'destroy' == reflection.options[:dependent].to_s && reflection.klass.archivable? } - .each do |name, reflection| - dependent_records_for_unarchival(name, reflection).each { |record| record.unarchive(force) } + .select { |_, ref| 'destroy' == ref.options[:dependent].to_s && ref.klass.archivable? } + .each do |name, ref| + dependent_records_for_unarchival(name, ref).each { |record| record.unarchive(force) } end end def set_archived_at(value, force = nil) return self unless archivable? @@ -153,15 +157,13 @@ def set_record_window(_, name, reflection) quoted_table_name = reflection.quoted_table_name window = ActiveArchive::Settings.config.dependent_record_window + query = "#{quoted_table_name}.archived_at > ? AND #{quoted_table_name}.archived_at < ?" + send(name).unscope(where: :archived_at) - .where([ - "#{quoted_table_name}.archived_at > ? AND #{quoted_table_name}.archived_at < ?", - archived_at - window, - archived_at + window - ]) + .where([query, archived_at - window, archived_at + window]) end def should_force_destroy?(force) force.is_a?(Hash) ? force[:force] : (:force == force) end