Remove duplicate email received

This commit is contained in:
Simon Lehericey 2017-02-15 15:26:24 +01:00
parent a7b7c8b63f
commit 58d5e40130
3 changed files with 37 additions and 1 deletions

View file

@ -0,0 +1,11 @@
class RemoveDuplicateEmailReceived < ActiveRecord::Migration[5.0]
def change
all_mails = MailReceived.all
groupped = all_mails.group_by { |m| m.procedure_id }
filtered = groupped.reject { |k, v| v.length < 2 }
filtered.each do |k, duplicate_mails|
duplicate_mails.pop
duplicate_mails.each(&:destroy)
end
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170125152856) do
ActiveRecord::Schema.define(version: 20170215102943) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

View file

@ -0,0 +1,25 @@
load 'spec/spec_helper.rb'
load 'db/migrate/20170215102943_remove_duplicate_email_received.rb'
describe RemoveDuplicateEmailReceived do
context 'with one procedure and one associated mail_received' do
let!(:procedure) { create(:procedure) }
it 'keeps the procedure mails' do
RemoveDuplicateEmailReceived.new.change
expect(MailReceived.count).to eq(1)
end
context 'and another mail_received' do
before :each do
MailReceived.create!(procedure: procedure)
end
it 'destroys the unecessary maiL_received' do
RemoveDuplicateEmailReceived.new.change
expect(MailReceived.count).to eq(1)
expect(procedure.mail_received).not_to be_nil
end
end
end
end