Merge pull request #22 from sgmap/delete_duplicate_mail_received

Delete duplicate mail received
This commit is contained in:
Mathieu Magnin 2017-02-15 17:15:00 +01:00 committed by GitHub
commit 6a8c9c67de
5 changed files with 50 additions and 6 deletions

View file

@ -30,7 +30,7 @@ class Procedure < ActiveRecord::Base
validates :libelle, presence: true, allow_blank: false, allow_nil: false
validates :description, presence: true, allow_blank: false, allow_nil: false
after_save :build_default_mails, if: Proc.new { id_changed? }
after_create :build_default_mails
def build_default_mails
MailReceived.create(procedure: self) unless mail_received

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

@ -0,0 +1,6 @@
class AddTimestampsToMailTemplate < ActiveRecord::Migration[5.0]
def change
add_column :mail_templates, :created_at, :datetime
add_column :mail_templates, :updated_at, :datetime
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: 20170215142944) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -234,10 +234,12 @@ ActiveRecord::Schema.define(version: 20170125152856) do
end
create_table "mail_templates", force: :cascade do |t|
t.string "object"
t.text "body"
t.string "type"
t.integer "procedure_id"
t.string "object"
t.text "body"
t.string "type"
t.integer "procedure_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "module_api_cartos", force: :cascade do |t|

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