Skip to content

Commit

Permalink
Merge pull request JackDanger#103 from emjot/update-to-rails-5
Browse files Browse the repository at this point in the history
Update to rails 5
  • Loading branch information
JackDanger authored Oct 16, 2019
2 parents 7594eea + 3ca3fc5 commit 4ae341d
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 77 deletions.
4 changes: 3 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
require: rubocop-performance

inherit_from: .rubocop_todo.yml

AllCops:
Exclude:
- bin/*
TargetRubyVersion: 2.0
TargetRubyVersion: 2.2

Metrics/BlockLength:
Enabled: false
Expand Down
7 changes: 1 addition & 6 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2019-10-09 11:48:30 +0200 using RuboCop version 0.50.0.
# on 2019-10-15 13:32:11 +0200 using RuboCop version 0.68.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 15
Lint/AmbiguousBlockAssociation:
Exclude:
- 'spec/permanent_records_spec.rb'
9 changes: 6 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
language: ruby
rvm:
- 2.0.0-p648
- 2.2.10
- 2.3.8
- 2.4.9
- 2.5.7
- 2.6.5
env:
- AR_TEST_VERSION: 4.2.0
- AR_TEST_VERSION: 4.2.11.1
- AR_TEST_VERSION: 5.0.7.2
- AR_TEST_VERSION: 5.1.7
- AR_TEST_VERSION: 5.2.3
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# PermanentRecords (Rails 4.2)
# PermanentRecords (Rails 5)

[http://github.com/JackDanger/permanent_records/](http://github.com/JackDanger/permanent_records/)

Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PermanentRecords (Rails 4.2)
PermanentRecords (Rails 5)
=============================

http://github.com/JackDanger/permanent_records/
Expand Down
39 changes: 20 additions & 19 deletions lib/permanent_records.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def destroy(force = nil)
if !is_permanent? || PermanentRecords.should_force_destroy?(force)
permanently_delete_records_after { super() }
else
destroy_with_permanent_records force
destroy_with_permanent_records(force)
end
end
end
Expand All @@ -68,7 +68,7 @@ def revival # rubocop:disable Metrics/MethodLength
set_deleted_at(nil, validate)
# increment all associated counters for counter cache
each_counter_cache do |assoc_class, counter_cache_column, assoc_id|
assoc_class.increment_counter counter_cache_column, assoc_id
assoc_class.increment_counter(counter_cache_column, assoc_id)
end
true
end
Expand All @@ -81,9 +81,9 @@ def get_deleted_record # rubocop:disable Naming/AccessorMethodName
end

# rubocop:disable Metrics/MethodLength
# rubocop:disable Lint/RescueWithoutErrorClass
def set_deleted_at(value, force = nil)
return self unless is_permanent?

record = get_deleted_record
record.deleted_at = value
begin
Expand All @@ -98,14 +98,14 @@ def set_deleted_at(value, force = nil)
end

@attributes = record.instance_variable_get('@attributes')
rescue => e
rescue StandardError => e
# trigger dependent record destruction (they were revived before this
# record, which cannot be revived due to validations)
record.destroy
raise e
end
end
# rubocop:enable Lint/RescueWithoutErrorClass

# rubocop:enable Metrics/MethodLength

def each_counter_cache
Expand All @@ -116,9 +116,7 @@ def each_counter_cache

associated_class = association.class

yield(associated_class,
reflection.counter_cache_column,
send(reflection.foreign_key))
yield(associated_class, reflection.counter_cache_column, send(reflection.foreign_key))
end
end

Expand All @@ -131,7 +129,7 @@ def destroy_with_permanent_records(force = nil)
set_deleted_at(Time.now, force)
# decrement all associated counters for counter cache
each_counter_cache do |assoc_class, counter_cache_column, assoc_id|
assoc_class.decrement_counter counter_cache_column, assoc_id
assoc_class.decrement_counter(counter_cache_column, assoc_id)
end
end
true
Expand All @@ -143,18 +141,16 @@ def destroy_with_permanent_records(force = nil)
def add_record_window(_request, name, reflection)
send(name).unscope(where: :deleted_at).where(
[
"#{reflection.quoted_table_name}.deleted_at > ?" \
"#{reflection.klass.quoted_table_name}.deleted_at > ?" \
' AND ' \
"#{reflection.quoted_table_name}.deleted_at < ?",
"#{reflection.klass.quoted_table_name}.deleted_at < ?",
deleted_at - PermanentRecords.dependent_record_window,
deleted_at + PermanentRecords.dependent_record_window
]
)
end

# TODO: Feel free to refactor this without polluting the ActiveRecord
# namespace.
# rubocop:disable Metrics/AbcSize
# TODO: Feel free to refactor this without polluting the ActiveRecord namespace.
def revive_destroyed_dependent_records(force = nil)
destroyed_dependent_relations.each do |relation|
relation.to_a.each { |destroyed_dependent_record| destroyed_dependent_record.try(:revive, force) }
Expand All @@ -165,15 +161,14 @@ def revive_destroyed_dependent_records(force = nil)
# rubocop:disable Metrics/MethodLength
def destroyed_dependent_relations
PermanentRecords.dependent_permanent_reflections(self.class).map do |name, relation|
cardinality = relation.macro.to_s.gsub('has_', '').to_sym
case cardinality
when :many
case relation.macro.to_sym
when :has_many
if deleted_at
add_record_window(send(name), name, relation)
else
send(name).unscope(where: :deleted_at)
end
when :one, :belongs_to
when :has_one, :belongs_to
self.class.unscoped { Array(send(name)) }
end
end
Expand All @@ -194,6 +189,7 @@ def dependent_record_ids
.reduce({}) do |records, (key, _)|
found = Array(send(key)).compact
next records if found.empty?

records.update found.first.class => found.map(&:id)
end
end
Expand All @@ -218,6 +214,7 @@ def permanently_delete_records(dependent_records)
ids.each do |id|
record = klass.unscoped.where(klass.primary_key => id).first
next unless record

record.deleted_at = nil
record.destroy(:force)
end
Expand Down Expand Up @@ -282,5 +279,9 @@ def self.dependent_permanent_reflections(klass)
end

ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.send :include, PermanentRecords::ActiveRecord
ActiveRecord::Base.send(:include, PermanentRecords::ActiveRecord)

if [ActiveRecord::VERSION::MAJOR, ActiveRecord::VERSION::MINOR] == [5, 2] || ActiveRecord::VERSION::MAJOR > 5
require 'permanent_records/active_record_5_2'
end
end
40 changes: 40 additions & 0 deletions lib/permanent_records/active_record_5_2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# rubocop:disable Metrics/AbcSize
# Support destroy for rails belongs_to assocations.
module HandlePermanentRecordsDestroyedInBelongsToAssociation
def handle_dependency
return unless load_target

case options[:dependent]
when :destroy
target.destroy
raise ActiveRecord::Rollback if target.respond_to?(:deleted?) && !target.deleted?
else
target.send(options[:dependent])
end
end
end

# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/CyclomaticComplexity
# Support destroy for rails 5.2. has_on associations.
module HandlePermanentRecordsDestroyedInHasOneAssociation
def delete(method = options[:dependent])
return unless load_target

case method
when :delete
target.delete
when :destroy
target.destroyed_by_association = reflection
target.destroy
throw(:abort) if target.respond_to?(:deleted?) && !target.deleted?
when :nullify
target.update_columns(reflection.foreign_key => nil) if target.persisted?
end
end
end
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/AbcSize
ActiveRecord::Associations::BelongsToAssociation.prepend(HandlePermanentRecordsDestroyedInBelongsToAssociation)
ActiveRecord::Associations::HasOneAssociation.prepend(HandlePermanentRecordsDestroyedInHasOneAssociation)
11 changes: 6 additions & 5 deletions permanent_records.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Gem::Specification.new do |s|
s.description = <<-DESCRIPTION
Never Lose Data. Rather than deleting rows this sets Record#deleted_at and
gives you all the scopes you need to work with your data.
DESCRIPTION
DESCRIPTION
s.email = '[email protected]'
s.extra_rdoc_files = [
'LICENSE',
Expand All @@ -26,12 +26,13 @@ DESCRIPTION
ver = ENV['AR_TEST_VERSION']
ver = ver.dup.chomp if ver

s.add_runtime_dependency 'activerecord', ver || '>= 4.2.0'
s.add_runtime_dependency 'activesupport', ver || '>= 4.2.0'
s.add_runtime_dependency 'activerecord', ver || '>= 5.0.0'
s.add_runtime_dependency 'activesupport', ver || '>= 5.0.0'
s.add_development_dependency 'database_cleaner', '>= 1.5.1'
s.add_development_dependency 'pry-byebug'
s.add_development_dependency 'rake' # For Travis-ci
s.add_development_dependency 'rspec', '>= 3.5.0'
s.add_development_dependency 'rubocop', '~> 0.50.0' # freeze to ensure ruby 2.0 compatibility
s.add_development_dependency 'sqlite3', '~> 1.3.6' # freeze to ensure specs are working
s.add_development_dependency 'rubocop', '~> 0.68.0' # freeze to ensure ruby 2.2 compatibility
s.add_development_dependency 'rubocop-performance'
s.add_development_dependency 'sqlite3', '~> 1.3.13' # freeze to ensure specs are working
end
2 changes: 1 addition & 1 deletion spec/permanent_records/circular_sti_dependency_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
let(:dirt) { Dirt.create(hole: hole) }
let(:location) { Location.create(name: 'location', hole: hole) }
let!(:zone) do
location.zones.create name: 'zone', parent_id: location.id
location.zones.create(name: 'zone', parent_id: location.id)
end

describe '#revive' do
Expand Down
10 changes: 5 additions & 5 deletions spec/permanent_records/counter_cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
end

it 'decrements counter_cache after destroying ant' do
expect(hole.reload.ants_count).to eq 0
expect(hole.reload.ants_count).to eq(0)
end

context 'revive' do
Expand All @@ -24,7 +24,7 @@
end

it 'increment counter_cache after reviving ant' do
expect(hole.reload.ants_count).to eq 1
expect(hole.reload.ants_count).to eq(1)
end
end
end
Expand All @@ -40,7 +40,7 @@
end

it 'increments counter_cache after creating new ant' do
expect(hole.ants_count).to eq 2
expect(hole.ants_count).to eq(2)
end
end

Expand All @@ -50,7 +50,7 @@
end

it 'decrements counter_cache after destroying ant' do
expect(hole.reload.ants_count).to eq 0
expect(hole.reload.ants_count).to eq(0)
end

context 'revive' do
Expand All @@ -59,7 +59,7 @@
end

it 'increment counter_cache after reviving ant' do
expect(hole.reload.ants_count).to eq 1
expect(hole.reload.ants_count).to eq(1)
end
end
end
Expand Down
Loading

0 comments on commit 4ae341d

Please sign in to comment.