Skip to content

Commit 979cd73

Browse files
committed
[Issue CodeandoMexico#209] Send email after status change
Send email notification when a service request status has changed. Added Sidekiq Ignoring coverage folder Added Redis dependency on README file
1 parent 50c8358 commit 979cd73

21 files changed

+209
-50
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@
2525
.ruby-version
2626
.rspec
2727
Guardfile
28+
coverage

Gemfile

+6-1
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,17 @@ gem 'figaro'
3030
gem 'active_model_serializers'
3131
gem 'coveralls', require: false
3232

33+
# Async backgroun job handling
34+
gem 'sidekiq'
35+
gem 'sinatra', require: false
36+
gem 'slim'
37+
3338
group :test do
3439
gem 'ffaker'
3540
gem 'database_cleaner'
3641
gem 'launchy'
3742
gem 'capybara'
38-
gem 'selenium-webdriver'
43+
gem 'selenium-webdriver', "~> 2.38.0"
3944
end
4045

4146
group :development,:test do

Gemfile.lock

+31-5
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ GEM
5555
activemodel (>= 3.2.0)
5656
activesupport (>= 3.2.0)
5757
json (>= 1.7)
58-
childprocess (0.3.9)
58+
celluloid (0.15.2)
59+
timers (~> 1.1.0)
60+
childprocess (0.5.1)
5961
ffi (~> 1.0, >= 1.0.11)
6062
chunky_png (1.2.8)
6163
coderay (1.1.0)
@@ -73,6 +75,7 @@ GEM
7375
sass (~> 3.1)
7476
compass-rails (1.0.3)
7577
compass (>= 0.12.2, < 0.14)
78+
connection_pool (1.2.0)
7679
coveralls (0.7.0)
7780
multi_json (~> 1.3)
7881
rest-client
@@ -223,6 +226,8 @@ GEM
223226
rack (1.4.5)
224227
rack-cache (1.2)
225228
rack (>= 0.4)
229+
rack-protection (1.5.2)
230+
rack
226231
rack-ssl (1.3.3)
227232
rack
228233
rack-test (0.6.2)
@@ -255,6 +260,9 @@ GEM
255260
ffi (>= 0.5.0)
256261
rdoc (3.12.2)
257262
json (~> 1.4)
263+
redis (3.0.7)
264+
redis-namespace (1.4.1)
265+
redis (~> 3.0.4)
258266
rest-client (1.6.7)
259267
mime-types (>= 1.16)
260268
rspec-core (2.14.5)
@@ -269,37 +277,51 @@ GEM
269277
rspec-expectations (~> 2.14.0)
270278
rspec-mocks (~> 2.14.0)
271279
ruby-hmac (0.4.0)
272-
rubyzip (0.9.9)
280+
rubyzip (1.1.0)
273281
sass (3.2.10)
274282
sass-rails (3.2.6)
275283
railties (~> 3.2.0)
276284
sass (>= 3.1.10)
277285
tilt (~> 1.3)
278286
sassy-math (1.5)
279287
compass (~> 0.11)
280-
selenium-webdriver (2.35.1)
288+
selenium-webdriver (2.38.0)
281289
childprocess (>= 0.2.5)
282290
multi_json (~> 1.0)
283-
rubyzip (< 1.0.0)
291+
rubyzip (~> 1.0)
284292
websocket (~> 1.0.4)
285293
sendgrid (1.2.0)
286294
json
287295
json
288296
shoulda-matchers (2.3.0)
289297
activesupport (>= 3.0.0)
298+
sidekiq (2.17.5)
299+
celluloid (>= 0.15.2)
300+
connection_pool (>= 1.0.0)
301+
json
302+
redis (>= 3.0.6)
303+
redis-namespace (>= 1.3.1)
290304
simple_oauth (0.2.0)
291305
simplecov (0.8.2)
292306
docile (~> 1.1.0)
293307
multi_json
294308
simplecov-html (~> 0.8.0)
295309
simplecov-html (0.8.0)
310+
sinatra (1.4.4)
311+
rack (~> 1.4)
312+
rack-protection (~> 1.4)
313+
tilt (~> 1.3, >= 1.3.4)
314+
slim (2.0.2)
315+
temple (~> 0.6.6)
316+
tilt (>= 1.3.3, < 2.1)
296317
sprockets (2.2.2)
297318
hike (~> 1.2)
298319
multi_json (~> 1.0)
299320
rack (~> 1.0)
300321
tilt (~> 1.1, != 1.3.0)
301322
statistics2 (0.54)
302323
subexec (0.2.3)
324+
temple (0.6.7)
303325
term-ansicolor (1.2.2)
304326
tins (~> 0.8)
305327
thin (1.5.1)
@@ -311,6 +333,7 @@ GEM
311333
activerecord
312334
statistics2
313335
tilt (1.4.1)
336+
timers (1.1.0)
314337
tins (0.13.1)
315338
treetop (1.4.15)
316339
polyglot
@@ -381,9 +404,12 @@ DEPENDENCIES
381404
ransack
382405
rspec-rails
383406
sass-rails (~> 3.2.3)
384-
selenium-webdriver
407+
selenium-webdriver (~> 2.38.0)
385408
sendgrid
386409
shoulda-matchers
410+
sidekiq
411+
sinatra
412+
slim
387413
thin
388414
thumbs_up
389415
twitter

Procfile

-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
2-

README.en.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ based on the [Open 311](http://open311.org) standard.
1818
- Rails 3.2
1919
- Bootstrap 3.0
2020
- Rspec 2.14
21+
- Redis 2.8.6
2122

2223
### Questions?
2324

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ de la comunicación con la ciudadanía y los reportes de cuestiones en sus servi
2121
- Rails 3.2
2222
- Bootstrap 3.0
2323
- Rspec 2.14
24+
- Redis 2.8.6
2425

2526
### ¿Dudas?
2627

app/mailers/user_mailer.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class UserMailer < ActionMailer::Base
2+
default from: ENV['MAILER_FROM'] || 'Urbem <no-responder@urbem.org>'
3+
4+
def notify_service_request_status_change(service_request_id, previous_status_id)
5+
@service_request = ServiceRequest.find(service_request_id)
6+
@user = @service_request.requester
7+
@previous_status = Status.find(previous_status_id)
8+
mail(subject: I18n.t('mailer.subject.status_change_notification'), to: @user.email)
9+
end
10+
11+
end
12+

app/models/service_request.rb

+12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class ServiceRequest < ActiveRecord::Base
1010
validate :service_extra_fields
1111

1212
before_validation :assign_default_status, on: :create
13+
after_update :send_notification_for_status_update
1314

1415
belongs_to :service
1516
belongs_to :requester, polymorphic: true
@@ -106,6 +107,11 @@ def service_fields_hash
106107
end
107108
end
108109

110+
def requested_by_user?
111+
self.requester_type == 'User'
112+
end
113+
114+
109115
ransacker :date do |parent|
110116
Arel::Nodes::InfixOperation.new('||',
111117
Arel::Nodes::InfixOperation.new('||', parent.table[:created_at], ' '), parent.table[:created_at])
@@ -123,4 +129,10 @@ def assign_default_status
123129
self.status = Status.where(is_default: true).first
124130
end
125131

132+
def send_notification_for_status_update
133+
if self.requested_by_user? && self.status_id_changed?
134+
UserMailer.delay.notify_service_request_status_change(self.id, self.status_id_was)
135+
end
136+
end
137+
126138
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<p>Hola <b><%= @user.name || @user.email %></b>, </p>
2+
3+
<p>Tu reporte con folio <b>#<%= @service_request.id %></b> ha cambiado de estatus <%= @previous_status.name %> a <%= @service_request.status.name %></p>
4+
5+
<p>Puedes ver más sobre este reporte y el mensaje dejado por el administrador ingresando a la siguiente página: <%= link_to nil, service_request_url(@service_request) %></p>
6+
7+
<p>Saludos</p>

config/environments/development.rb

+3-7
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,10 @@
3535
# Expands the lines which load the assets
3636
config.assets.debug = true
3737

38+
# Action Mailer
3839
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
39-
4040
config.action_mailer.delivery_method = :letter_opener
41+
config.action_mailer.perform_deliveries = true
42+
config.action_mailer.raise_delivery_errors = true
4143

42-
#Fallback for development as it uses open letter
43-
SENDGRID = {
44-
username: '',
45-
password: '',
46-
domain: ''
47-
}
4844
end

config/environments/production.rb

+15-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@
6666
# with SQLite, MySQL, and PostgreSQL)
6767
# config.active_record.auto_explain_threshold_in_seconds = 0.5
6868
#
69-
config.action_mailer.default_url_options = { :host => ENV['ACTION_MAILER_DEFAULT_URL'] }
69+
70+
# Action mailer
71+
config.action_mailer.raise_delivery_errors = false
72+
config.action_mailer.default_url_options = { :host => ENV['HOST'] }
73+
config.action_mailer.smtp_settings = {
74+
:user_name => ENV['SENDGRID_USERNAME'],
75+
:password => ENV['SENDGRID_PASSWORD'],
76+
:domain => 'heroku.com',
77+
:address => 'smtp.sendgrid.net',
78+
:port => '587',
79+
:authentication => :plain,
80+
:enable_starttls_auto => true
81+
}
82+
83+
7084

7185
end

config/environments/test.rb

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
# The :test delivery method accumulates sent emails in the
2929
# ActionMailer::Base.deliveries array.
3030
config.action_mailer.delivery_method = :test
31+
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
3132

3233
# Raise exception on mass assignment protection for Active Record models
3334
config.active_record.mass_assignment_sanitizer = :strict

config/initializers/setup_mail.rb

-9
This file was deleted.

config/initializers/sidekiq.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
if Rails.env.production?
2+
Sidekiq.configure_server do |config|
3+
config.redis = { url: ENV["REDISCLOUD_URL"], size: 3 }
4+
5+
database_url = ENV['DATABASE_URL']
6+
if database_url
7+
ENV['DATABASE_URL'] = "#{database_url}?pool=20"
8+
ActiveRecord::Base.establish_connection
9+
end
10+
end
11+
12+
Sidekiq.configure_client do |config|
13+
config.redis = {size: 1}
14+
end
15+
end
16+

config/locales/mailers.es.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
es:
2+
mailer:
3+
subject:
4+
status_change_notification: 'Cambio de estatus en reporte'

config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'api_constraints'
2+
23
ReporteCiudadano::Application.routes.draw do
34

45
devise_for :admins, controllers: { sessions: 'admins/sessions' }

config/unicorn.rb

+24-26
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
1-
# config/unicorn.rb
2-
3-
worker_processes 3 #Integer(ENV["WEB_CONCURRENCY"] || 3)
4-
timeout 30 #Integer(ENV['WEB_TIMEOUT'] || 30)
5-
#preload_app true
6-
7-
#before_fork do |server, worker|
8-
9-
#Signal.trap 'TERM' do
10-
#puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
11-
#Process.kill 'QUIT', Process.pid
12-
#end
13-
14-
#defined?(ActiveRecord::Base) and
15-
#ActiveRecord::Base.connection.disconnect!
16-
#end
17-
18-
#after_fork do |server, worker|
19-
20-
#Signal.trap 'TERM' do
21-
#puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to sent QUIT'
22-
#end
23-
24-
#defined?(ActiveRecord::Base) and
25-
#ActiveRecord::Base.establish_connection
26-
#end
1+
worker_processes 3
2+
timeout 30
3+
preload_app true
4+
5+
before_fork do |server, worker|
6+
Signal.trap 'TERM' do
7+
puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
8+
Process.kill 'QUIT', Process.pid
9+
end
10+
11+
@sidekiq_pid ||= spawn("bundle exec sidekiq -c 2")
12+
13+
defined?(ActiveRecord::Base) and
14+
ActiveRecord::Base.connection.disconnect!
15+
end
16+
17+
after_fork do |server, worker|
18+
Signal.trap 'TERM' do
19+
puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to sent QUIT'
20+
end
21+
22+
defined?(ActiveRecord::Base) and
23+
ActiveRecord::Base.establish_connection
24+
end
2725

spec/mailers/user_mailer_spec.rb

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'spec_helper'
2+
3+
describe UserMailer do
4+
5+
describe ".notify_service_request_status_change" do
6+
7+
let!(:service_request) { FactoryGirl.create(:service_request) }
8+
let!(:status) { FactoryGirl.create(:status) }
9+
let!(:mail) { UserMailer.notify_service_request_status_change(service_request.id, status.id) }
10+
11+
12+
it "sends the email" do
13+
reset_email
14+
mail.deliver!
15+
ActionMailer::Base.deliveries.size.should be 1
16+
end
17+
18+
it "sends an email to the requester" do
19+
expect(mail.to).to eq( [service_request.requester.email] )
20+
end
21+
22+
it "has a translated subject" do
23+
expect( mail.subject ).to eq(I18n.t('mailer.subject.status_change_notification'))
24+
end
25+
end
26+
27+
end
28+

0 commit comments

Comments
 (0)