Commit 1672fd6e155776ac81083d07dd32ec7ddbf4bcba

Authored by Rodrigo Souto
1 parent fb7ddece

suggestions: send email only if user enabled the email_suggestions option

app/models/profile.rb
... ... @@ -3,7 +3,7 @@
3 3 # which by default is the one returned by Environment:default.
4 4 class Profile < ActiveRecord::Base
5 5  
6   - 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
  6 + 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
7 7  
8 8 # use for internationalizable human type names in search facets
9 9 # reimplement on subclasses
... ... @@ -163,6 +163,7 @@ class Profile &lt; ActiveRecord::Base
163 163 settings_items :public_content, :type => :boolean, :default => true
164 164 settings_items :description
165 165 settings_items :fields_privacy, :type => :hash, :default => {}
  166 + settings_items :email_suggestions, :type => :boolean, :default => false
166 167  
167 168 validates_length_of :description, :maximum => 550, :allow_nil => true
168 169  
... ...
app/views/profile_editor/edit.html.erb
... ... @@ -51,6 +51,12 @@
51 51 'profile_data[redirect_l10n]', true, @profile.redirect_l10n
52 52 )%>
53 53  
  54 + <h2><%= _('Suggestions') %></h2>
  55 + <%= labelled_check_box(
  56 + _('Send me relationship suggestions by email'),
  57 + 'profile_data[email_suggestions]', true, @profile.email_suggestions
  58 + )%>
  59 +
54 60 <%=
55 61 @plugins.dispatch(:profile_editor_extras).map do |content|
56 62 content.kind_of?(Proc) ? self.instance_exec(&content) : content
... ...
lib/profile_suggestions_job.rb
... ... @@ -13,7 +13,7 @@ class ProfileSuggestionsJob &lt; Struct.new(:person_id)
13 13 begin
14 14 person = Person.find(person_id)
15 15 ProfileSuggestion.calculate_suggestions(person)
16   - UserMailer.profiles_suggestions_email(person).deliver
  16 + UserMailer.profiles_suggestions_email(person).deliver if person.email_suggestions
17 17 rescue Exception => exception
18 18 logger.error("Error with suggestions for person ID %d: %s" % [person_id, exception.to_s])
19 19 end
... ...
test/unit/profile_suggestions_job_test.rb
... ... @@ -98,4 +98,24 @@ class ProfileSuggestionsJobTest &lt; ActiveSupport::TestCase
98 98 assert_equivalent [community], person.suggested_communities
99 99 end
100 100  
  101 + should 'send suggestion e-mail only if the user enabled it' do
  102 + person = create_user('person').person
  103 + person.email_suggestions = true
  104 + person.save!
  105 + job = ProfileSuggestionsJob.new(person.id)
  106 + assert_difference 'ActionMailer::Base.deliveries.count', 1 do
  107 + job.perform
  108 + end
  109 + end
  110 +
  111 + should 'not send suggestion e-mail if the user disabled it' do
  112 + person = create_user('person').person
  113 + person.email_suggestions = false
  114 + person.save!
  115 + job = ProfileSuggestionsJob.new(person.id)
  116 + assert_no_difference 'ActionMailer::Base.deliveries.count' do
  117 + job.perform
  118 + end
  119 + end
  120 +
101 121 end
... ...