Remove duplicate email received
This commit is contained in:
parent
a7b7c8b63f
commit
58d5e40130
3 changed files with 37 additions and 1 deletions
11
db/migrate/20170215102943_remove_duplicate_email_received.rb
Normal file
11
db/migrate/20170215102943_remove_duplicate_email_received.rb
Normal 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
|
|
@ -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"
|
||||
|
|
25
spec/migrations/remove_duplicate_email_received_spec.rb
Normal file
25
spec/migrations/remove_duplicate_email_received_spec.rb
Normal 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
|
Loading…
Reference in a new issue