Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Send notification to configurable email when new user registers

Bug: https://bugs.gentoo.org/show_bug.cgi?id=355725

You will have to add new entries to config/config.yml after applying
this change. Now application requires

  notifications:
    new_user:                 example@example.com
  • Loading branch information...
commit f47ebb2a1eb022725d9c322751bc16cce81760e8 1 parent 513749d
Joachim F. I. B. authored
7 app/models/user.rb
View
@@ -206,6 +206,13 @@ def progress
end
end
+ after_create do |user|
+ # Notify configured email about new user
+ to_address = APP_CONFIG['notifications']['new_user']
+ to_address_provided = !(to_address.nil? or to_address.empty?)
+ UserMailer.delay.deliver_new_answer(to_address, user) if to_address_provided
+ end
+
protected
def only_recruiter_can_be_administrator
6 app/models/user_mailer.rb
View
@@ -54,6 +54,12 @@ def new_comment(user, comment)
@body = { :question_title=> question_title(comment.answer), :id => comment.answer.id }
end
+ def new_user(to, new_user)
+ user = User.new :email_address => to
+ common(user, "New user")
+ @body = { :name => new_user.name, :id => new_user.id }
+ end
+
def unrecognized_email(user, email)
common(user, "Your email sent to #{@app_name} wasn't recognized")
3  app/views/user_mailer/new_user.erb
View
@@ -0,0 +1,3 @@
+A new user <%= @name %> just signed up, profile of new user is:
+
+<%= user_url(@id) %>
9 config/initializers/custom_config.rb
View
@@ -1,7 +1,8 @@
begin
if Rails.env.test? || Rails.env.cucumber?
# Tests must be run in known environment
- APP_CONFIG = { 'developer_data' => { 'check' => false } }
+ APP_CONFIG = { 'developer_data' => { 'check' => false },
+ 'notifications' => { 'new_user' => 'example@example.com' } }
else
APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")[RAILS_ENV]
end
@@ -64,6 +65,12 @@ def config_must_have_one_of(keys, type = Object)
checker.config_must_have_one_of(['uri', 'data'])
end
+checker = ConfigChecker.new(APP_CONFIG)
+checker.config_must_have('notifications', Hash)
+checker.config = APP_CONFIG['notifications']
+checker.key_prefix = 'notifications/'
+checker.config_must_have('new_user', String)
+
unless checker.errors.empty?
puts "Error(s) while checking configuration:"
checker.errors.each{ |err| puts " * #{err}" }
2  doc/config/config.yml
View
@@ -5,6 +5,8 @@ defaults: &defaults
min_months_mentor_is_dev: 6
seed:
users_domain: example.com
+ notifications:
+ new_user: recruiting@gentoo.org
development:
<<: *defaults
production:
11 spec/models/user_mailer_spec.rb
View
@@ -47,6 +47,17 @@
notification.should have_subject('New comment')
end
+ it "should prepare proper new user notification" do
+ user = Factory(:recruit)
+ to_address = 'example@example.com'
+ notification = UserMailer.create_new_user(to_address, user)
+ notification.should deliver_to(to_address)
+ notification.should deliver_from("no-reply@localhost")
+ notification.should have_text(/A new user #{user.name} just signed up, profile of new user is:/)
+ notification.should have_text(/http:\/\/localhost:3000\/users\/#{user.id}/)
+ notification.should have_subject('New user')
+ end
+
it "should prepare proper unrecognized message email" do
user = Factory(:recruit)
mail = TMail::Mail.new
5 spec/models/user_spec.rb
View
@@ -378,4 +378,9 @@
u4.should be_valid
u4.save!
end
+
+ it "should notify recruiters when new user registers" do
+ UserMailer.should_receive_delayed(:deliver_new_answer)
+ user = Factory(:recruit, :mentor => nil)
+ end
end

1 comment on commit f47ebb2

Petteri Räty

look good to me

Please sign in to comment.
Something went wrong with that request. Please try again.