From 1672fd6e155776ac81083d07dd32ec7ddbf4bcba Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Wed, 28 Jan 2015 14:30:30 -0300 Subject: [PATCH] suggestions: send email only if user enabled the email_suggestions option --- app/models/profile.rb | 3 ++- app/views/profile_editor/edit.html.erb | 6 ++++++ lib/profile_suggestions_job.rb | 2 +- test/unit/profile_suggestions_job_test.rb | 20 ++++++++++++++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/app/models/profile.rb b/app/models/profile.rb index fc65afa..f646e71 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -3,7 +3,7 @@ # which by default is the one returned by Environment:default. class Profile < ActiveRecord::Base - attr_accessible :name, :identifier, :public_profile, :nickname, :custom_footer, :custom_header, :address, :zip_code, :contact_phone, :image_builder, :description, :closed, :template_id, :environment, :lat, :lng, :is_template, :fields_privacy, :preferred_domain_id, :category_ids, :country, :city, :state, :national_region_code, :email, :contact_email, :redirect_l10n, :notification_time, :redirection_after_login + attr_accessible :name, :identifier, :public_profile, :nickname, :custom_footer, :custom_header, :address, :zip_code, :contact_phone, :image_builder, :description, :closed, :template_id, :environment, :lat, :lng, :is_template, :fields_privacy, :preferred_domain_id, :category_ids, :country, :city, :state, :national_region_code, :email, :contact_email, :redirect_l10n, :notification_time, :redirection_after_login, :email_suggestions # use for internationalizable human type names in search facets # reimplement on subclasses @@ -163,6 +163,7 @@ class Profile < ActiveRecord::Base settings_items :public_content, :type => :boolean, :default => true settings_items :description settings_items :fields_privacy, :type => :hash, :default => {} + settings_items :email_suggestions, :type => :boolean, :default => false validates_length_of :description, :maximum => 550, :allow_nil => true diff --git a/app/views/profile_editor/edit.html.erb b/app/views/profile_editor/edit.html.erb index abef586..309ba24 100644 --- a/app/views/profile_editor/edit.html.erb +++ b/app/views/profile_editor/edit.html.erb @@ -51,6 +51,12 @@ 'profile_data[redirect_l10n]', true, @profile.redirect_l10n )%> +

<%= _('Suggestions') %>

+ <%= labelled_check_box( + _('Send me relationship suggestions by email'), + 'profile_data[email_suggestions]', true, @profile.email_suggestions + )%> + <%= @plugins.dispatch(:profile_editor_extras).map do |content| content.kind_of?(Proc) ? self.instance_exec(&content) : content diff --git a/lib/profile_suggestions_job.rb b/lib/profile_suggestions_job.rb index 0d17c6e..f627986 100644 --- a/lib/profile_suggestions_job.rb +++ b/lib/profile_suggestions_job.rb @@ -13,7 +13,7 @@ class ProfileSuggestionsJob < Struct.new(:person_id) begin person = Person.find(person_id) ProfileSuggestion.calculate_suggestions(person) - UserMailer.profiles_suggestions_email(person).deliver + UserMailer.profiles_suggestions_email(person).deliver if person.email_suggestions rescue Exception => exception logger.error("Error with suggestions for person ID %d: %s" % [person_id, exception.to_s]) end diff --git a/test/unit/profile_suggestions_job_test.rb b/test/unit/profile_suggestions_job_test.rb index b34ae71..fffa9dc 100644 --- a/test/unit/profile_suggestions_job_test.rb +++ b/test/unit/profile_suggestions_job_test.rb @@ -98,4 +98,24 @@ class ProfileSuggestionsJobTest < ActiveSupport::TestCase assert_equivalent [community], person.suggested_communities end + should 'send suggestion e-mail only if the user enabled it' do + person = create_user('person').person + person.email_suggestions = true + person.save! + job = ProfileSuggestionsJob.new(person.id) + assert_difference 'ActionMailer::Base.deliveries.count', 1 do + job.perform + end + end + + should 'not send suggestion e-mail if the user disabled it' do + person = create_user('person').person + person.email_suggestions = false + person.save! + job = ProfileSuggestionsJob.new(person.id) + assert_no_difference 'ActionMailer::Base.deliveries.count' do + job.perform + end + end + end -- libgit2 0.21.2