Skip to content

Commit

Permalink
Improved drain deletion.
Browse files Browse the repository at this point in the history
* Send email on deleted drain
* Soft-delete the removed rains
  • Loading branch information
squidarth committed Dec 5, 2016
1 parent 544c8f0 commit 7aa3911
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ gem 'rails_admin'
gem 'validates_formatting_of'

gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw]
gem 'paranoia', '~> 2.2'

group :assets do
gem 'sass-rails', '>= 4.0.3'
Expand Down
5 changes: 4 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ GEM
pkg-config (~> 1.1.7)
obscenity (1.0.2)
orm_adapter (0.5.0)
paranoia (2.2.0)
activerecord (>= 4.0, < 5.1)
parser (2.3.0.7)
ast (~> 2.2)
pg (0.18.4)
Expand Down Expand Up @@ -236,6 +238,7 @@ DEPENDENCIES
haml
http_accept_language
obscenity (~> 1.0, >= 1.0.2)
paranoia (~> 2.2)
pg
puma
rails (~> 4.2.4)
Expand All @@ -255,4 +258,4 @@ RUBY VERSION
ruby 2.2.3p173

BUNDLED WITH
1.13.1
1.13.6
5 changes: 5 additions & 0 deletions app/mailers/thing_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ def reminder(thing)
@user = thing.user
mail(to: @user.email, subject: ['Remember to clear your adopted drain'])
end

def drain_deleted_notification(thing)
@thing = thing
mail(to: User.where(admin: true).pluck(:email), subject: 'A drain has been removed.')
end
end
7 changes: 6 additions & 1 deletion app/models/thing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'csv'

class Thing < ActiveRecord::Base
acts_as_paranoid
extend Forwardable
include ActiveModel::ForbiddenAttributesProtection

Expand All @@ -21,6 +22,7 @@ def self.find_closest(lat, lng, limit = 10)
query = <<-SQL
SELECT *, (3959 * ACOS(COS(RADIANS(?)) * COS(RADIANS(lat)) * COS(RADIANS(lng) - RADIANS(?)) + SIN(RADIANS(?)) * SIN(RADIANS(lat)))) AS distance
FROM things
WHERE deleted_at is NULL
ORDER BY distance
LIMIT ?
SQL
Expand Down Expand Up @@ -55,7 +57,10 @@ def self._delete_non_existing_drains(drains_from_source)
drain['PUC_Maximo_Asset_ID'].gsub('N-', '')
end

Thing.where.not(city_id: city_ids).delete_all
Thing.where.not(city_id: city_ids).find_each do |thing|
thing.destroy!
ThingMailer.drain_deleted_notification(thing).deliver_now
end
end

def self._drain_params(drain)
Expand Down
3 changes: 3 additions & 0 deletions app/views/thing_mailer/drain_deleted_notification.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hi Adopt a drain admin!

Just notifying you that drain with PUC_Maximo_Asset_ID: <%= @thing.city_id %>, and rails id <% @thing.id %>, has been removed from the database.
6 changes: 6 additions & 0 deletions db/migrate/20161205030306_add_deleted_at_to_things.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddDeletedAtToThings < ActiveRecord::Migration
def change
add_column :things, :deleted_at, :datetime
add_index :things, :deleted_at
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160326200455) do
ActiveRecord::Schema.define(version: 20161205030306) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -52,9 +52,11 @@
t.integer "city_id"
t.integer "user_id"
t.string "system_use_code"
t.datetime "deleted_at"
end

add_index "things", ["city_id"], name: "index_things_on_city_id", unique: true, using: :btree
add_index "things", ["deleted_at"], name: "index_things_on_deleted_at", using: :btree

create_table "users", force: :cascade do |t|
t.datetime "created_at"
Expand Down
17 changes: 17 additions & 0 deletions test/mailers/thing_mailer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,21 @@ class ThingMailerTest < ActionMailer::TestCase
assert_equal ['[email protected]'], email.to
assert_equal 'We really do love you, Erik!', email.subject
end

test 'drain_deleted_notification' do
admin_1 = users(:admin)
admin_2 = users(:admin)
admin_2.update(email: '[email protected]')
thing = things(:thing_1)

email = nil
assert_emails(1) do
email = ThingMailer.drain_deleted_notification(thing).deliver_now
end

assert_includes email.to, admin_1.email
assert_includes email.to, admin_2.email

assert_equal email.subject, 'A drain has been removed.'
end
end
5 changes: 5 additions & 0 deletions test/models/thing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class ThingTest < ActiveSupport::TestCase
end

test 'loading drains, deletes existing drains not in data set, updates properties on rest' do
admin = users(:admin)
thing_1 = things(:thing_1)
thing_11 = things(:thing_11)

Expand All @@ -29,6 +30,10 @@ class ThingTest < ActiveSupport::TestCase
stub_request(:get, fake_url).to_return(body: fake_response)

Thing.load_drains(fake_url)

email = ActionMailer::Base.deliveries.last
assert_equal email.to, [admin.email]
assert_equal email.subject, 'A drain has been removed.'
thing_11.reload

# Asserts thing_1 is deleted
Expand Down

0 comments on commit 7aa3911

Please sign in to comment.