lib/active_archive/base.rb in active_archive-2.4.0 vs lib/active_archive/base.rb in active_archive-3.0.0

- old
+ new

@@ -33,11 +33,11 @@ I18n.t("active_archive.archival.#{archived? ? :archived : :unarchived}") end def unarchive(options=nil) with_transaction_returning_status do - (should_unarchive_parent_first?(options) ? unarchival.reverse : unarchival).lazy.each { |r| r.call(options) } + (should_unarchive_parent_first?(options) ? unarchival.reverse : unarchival).each { |r| r.call(options) } self end end alias_method(:unarchive!, :unarchive) @@ -51,15 +51,13 @@ end private def attempt_notifying_observers(callback) - begin - notify_observers(callback) - rescue NoMethodError => e - # RETURN - end + notify_observers(callback) + rescue NoMethodError + # RETURN end def destroy_with_active_archive(force=nil) run_callbacks(:destroy) do (archived? || new_record?) ? save : set_archived_at(Time.now, force) @@ -68,73 +66,77 @@ archived? ? self : false end def get_archived_record - record_id = (self.respond_to?(:parent_id) && self.parent_id.present?) ? parent_id : id - self.class.unscoped.find(record_id) + self.class.unscoped.find(id) end def get_dependent_records dependent_records = {} - self.class.reflections.lazy.each do |key, reflection| - if reflection.options[:dependent] == :destroy - next unless records = self.send(key) - if records.respond_to?(:size) - next unless records.size > 0 - else - records = [] << records - end - dependent_record = records.first - next if dependent_record.nil? - dependent_records[dependent_record.class] = records.map(&:id) + + self.class.reflections.each do |key, reflection| + next unless reflection.options[:dependent] == :destroy + next unless records = send(key) + + if records.respond_to? :size + next unless records.size > 0 + else + records = [] << records end + + dependent_record = records.first + next if dependent_record.nil? + dependent_records[dependent_record.class] = records.map(&:id) end - return(dependent_records) + + dependent_records end def permanently_delete_records(dependent_records) - dependent_records.lazy.each do |klass, ids| - ids.lazy.each do |id| - record = begin - klass.unscoped.find(id) - rescue ::ActiveRecord::RecordNotFound - next - end + 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 def permanently_delete_records_after(&block) dependent_records = get_dependent_records - dependent_results = block.call + dependent_results = yield permanently_delete_records(dependent_records) if dependent_results return(dependent_results) end def unarchival [ - ->(_validate) { unarchive_destroyed_dependent_records(_validate) }, - ->(_validate) { run_callbacks(:unarchive) { set_archived_at(nil, _validate) } } + ->(validate) { unarchive_destroyed_dependent_records(validate) }, + ->(validate) do + run_callbacks(:unarchive) do + set_archived_at(nil, validate) + return(true) + end + end ] end def unarchive_destroyed_dependent_records(force = nil) self.class.reflections.select do |name, reflection| - 'destroy'.freeze == reflection.options[:dependent].to_s && reflection.klass.archivable? + "destroy" == reflection.options[:dependent].to_s && reflection.klass.archivable? end.each do |name, reflection| - cardinality = reflection.macro.to_s.gsub('has_'.freeze, ''.freeze).to_sym + cardinality = reflection.macro.to_s.gsub("has_", "").to_sym case cardinality when :many records = (archived_at ? set_record_window(send(name), name, reflection) : send(name)) when :one, :belongs_to self.class.unscoped { records = [] << send(name) } end - [records].flatten.compact.lazy.each { |d| d.unarchive(force) } + [records].flatten.compact.each { |d| d.unarchive(force) } send(name, :reload) end end def set_archived_at(value, force=nil) @@ -142,12 +144,12 @@ record = get_archived_record record.archived_at = value begin should_ignore_validations?(force) ? record.save(validate: false) : record.save! - @attributes = record.instance_variable_get('@attributes'.freeze) - rescue Exception => e + @attributes = record.instance_variable_get("@attributes") + rescue => e record.destroy raise(e) end end @@ -159,18 +161,18 @@ archived_at + ActiveArchive.configuration.dependent_record_window ]) end def should_force_destroy?(force) - (Hash === force) ? force[:force] : (:force == force) + force.is_a?(Hash) ? force[:force] : (:force == force) end def should_ignore_validations?(force) - (Hash === force) && (false == force[:validate]) + force.is_a?(Hash) && (false == force[:validate]) end def should_unarchive_parent_first?(order) - (Hash === order) && (true == order[:reverse]) + order.is_a?(Hash) && (true == order[:reverse]) end end -end \ No newline at end of file +end