From e51503f6e677305e68ef9c55b060caff8f903872 Mon Sep 17 00:00:00 2001 From: marisa Date: Fri, 28 Feb 2020 12:36:07 +0100 Subject: [PATCH 1/8] Remove offers controller specs which were already on offers view specs --- spec/controllers/offers_controller_spec.rb | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/spec/controllers/offers_controller_spec.rb b/spec/controllers/offers_controller_spec.rb index 7d0619b89..5cb61af8d 100644 --- a/spec/controllers/offers_controller_spec.rb +++ b/spec/controllers/offers_controller_spec.rb @@ -100,11 +100,6 @@ get :show, id: offer.id expect(assigns(:destination_account)).to eq(member.account) end - - it 'displays the offer\'s user details' do - get :show, id: offer.id - expect(response.body).to include(offer.user.email) - end end end end @@ -118,11 +113,6 @@ allow(controller).to receive(:@current_organization).and_return(another_organization) end - it 'displays the offer\'s user details' do - get :show, id: offer.id - expect(response.body).to include(offer.user.email) - end - it 'sets the offer\'s organization as user\'s current organization' do get :show, id: offer.id expect(session[:current_organization_id]).to eq(offer.organization_id) @@ -136,11 +126,6 @@ let(:another_user) { Fabricate(:user) } before { login(another_user) } - - it 'doesn\'t display the offer\'s user details' do - get :show, id: offer.id - expect(response.body).to_not include(offer.user.email) - end end context 'when the user is not logged in' do @@ -148,11 +133,7 @@ get :show, id: offer.id expect(assigns(:offer)).to eq(offer) end - - it 'doesn\'t display the offer\'s user details' do - get :show, id: offer.id - expect(response.body).to_not include(offer.user.email) - end + end end From a9f101809bc1ddefa3cfc3aa4ea8d0d0baf783fd Mon Sep 17 00:00:00 2001 From: marisa Date: Fri, 28 Feb 2020 13:34:33 +0100 Subject: [PATCH 2/8] Move spec from controller to view --- spec/controllers/organizations_controller_spec.rb | 10 ---------- spec/views/organizations/show.html.erb_spec.rb | 4 ++++ 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/spec/controllers/organizations_controller_spec.rb b/spec/controllers/organizations_controller_spec.rb index 8bf15c25b..cd0ba5a55 100644 --- a/spec/controllers/organizations_controller_spec.rb +++ b/spec/controllers/organizations_controller_spec.rb @@ -5,16 +5,6 @@ let(:member) { Fabricate(:member, organization: organization) } let(:user) { member.user } - describe 'GET #show' do - it 'displays the organization page' do - get 'show', id: organization.id - - expect(assigns(:organization)).to eq(organization) - expect(response.status).to eq(200) - expect(response.body).to include(organization.name) - end - end - describe 'GET #index' do it 'populates and array of organizations' do get :index diff --git a/spec/views/organizations/show.html.erb_spec.rb b/spec/views/organizations/show.html.erb_spec.rb index 785cb9ca2..e2ccb6310 100644 --- a/spec/views/organizations/show.html.erb_spec.rb +++ b/spec/views/organizations/show.html.erb_spec.rb @@ -57,5 +57,9 @@ it 'diplays the movements section' do expect(rendered).to match t('shared.movements.movements') end + + it 'displays the organization page' do + expect(rendered).to match(organization.name) + end end end From 1fa4e033716bfd86778bd5fd2ce9d612b6cc8f74 Mon Sep 17 00:00:00 2001 From: marisa Date: Tue, 3 Mar 2020 10:55:30 +0100 Subject: [PATCH 3/8] Return describe 'GET #show' where it was --- spec/controllers/organizations_controller_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spec/controllers/organizations_controller_spec.rb b/spec/controllers/organizations_controller_spec.rb index cd0ba5a55..fc4144bf6 100644 --- a/spec/controllers/organizations_controller_spec.rb +++ b/spec/controllers/organizations_controller_spec.rb @@ -13,6 +13,15 @@ end end + describe 'GET #show' do + it 'displays the organization page' do + get 'show', id: organization.id + + expect(assigns(:organization)).to eq(organization) + expect(response.status).to eq(200) + end + end + describe 'POST #create' do it 'only superdamins are authorized create to new organizations' do login(member.user) From 82de815a2b01218d3596cc3f05376decd16d8ee9 Mon Sep 17 00:00:00 2001 From: Marc Anguera Insa Date: Sun, 1 Mar 2020 22:32:58 +0100 Subject: [PATCH 4/8] send OrganizationNotifier.recent_posts in user's locale --- app/jobs/organization_notifier_job.rb | 14 ++++++- app/mailers/organization_notifier.rb | 13 ++---- spec/mailers/organization_notifier_spec.rb | 48 ++++------------------ 3 files changed, 25 insertions(+), 50 deletions(-) diff --git a/app/jobs/organization_notifier_job.rb b/app/jobs/organization_notifier_job.rb index 1bd46729c..b4e0e5db4 100644 --- a/app/jobs/organization_notifier_job.rb +++ b/app/jobs/organization_notifier_job.rb @@ -2,7 +2,7 @@ # # Strategy: go throught all organizations and take latest active posts from last week # posted by active members. Send an email to all active and online members -# with the email notifications enabled with those posts. +# with the email notifications enabled with those posts. Group emails by user's locale. # Schedule defined in config/schedule.yml file. @@ -12,9 +12,19 @@ class OrganizationNotifierJob < ActiveJob::Base def perform Organization.all.find_each do |org| posts = org.posts.active.of_active_members.from_last_week + if posts.present? - OrganizationNotifier.recent_posts(posts).deliver_now + users_by_locale(org).each do |locale, users| + OrganizationNotifier.recent_posts(posts, locale, users).deliver_now + end end end end + + private + + def users_by_locale(organization) + organization.users.online_active.actives. + notifications.group_by { |u| u.locale || I18n.default_locale } + end end diff --git a/app/mailers/organization_notifier.rb b/app/mailers/organization_notifier.rb index 8b355a003..87f3119a3 100644 --- a/app/mailers/organization_notifier.rb +++ b/app/mailers/organization_notifier.rb @@ -6,20 +6,15 @@ class OrganizationNotifier < ActionMailer::Base # # en.organization_notifier.recent_posts.subject # - def recent_posts(posts) + def recent_posts(posts, locale, users) # last 10 posts of offers and inquiries @offers = posts.where(type: "Offer").take(10) @inquiries = posts.where(type: "Inquiry").take(10) @organization_name = posts.take.organization.name - mail(bcc: emails_newsletter(posts)) - end - - private - - def emails_newsletter(posts) - posts.take.organization.users.online_active.actives. - notifications.pluck(:email) + I18n.with_locale(locale) do + mail(bcc: users.map(&:email)) + end end end diff --git a/spec/mailers/organization_notifier_spec.rb b/spec/mailers/organization_notifier_spec.rb index 52bfec384..4b0be3816 100644 --- a/spec/mailers/organization_notifier_spec.rb +++ b/spec/mailers/organization_notifier_spec.rb @@ -1,52 +1,22 @@ require "spec_helper" RSpec.describe OrganizationNotifier do - let (:test_organization) { Fabricate(:organization) } - let! (:offer) { Fabricate(:offer, organization: test_organization) } - let! (:inquiry) { Fabricate(:inquiry, organization: test_organization) } - let (:user) do - Fabricate(:user, sign_in_count: 2, email: "user@example.com") - end - let (:another_user) { Fabricate(:user, sign_in_count: 1) } - let (:yet_another_user) { Fabricate(:user, sign_in_count: 0) } - let! (:member) do - Fabricate(:member, - organization: test_organization, - user: user, - active: true) - end - let! (:another_member) do - Fabricate(:member, - organization: test_organization, - user: another_user, - active: false) - end - let! (:yet_another_member) do - Fabricate(:member, - organization: test_organization, - user: yet_another_user, - active: true) - end - - before(:each) do - ActionMailer::Base.delivery_method = :test - ActionMailer::Base.perform_deliveries = true - ActionMailer::Base.deliveries = [] - OrganizationNotifier.recent_posts(test_organization.posts).deliver_now - end - - after(:each) do - ActionMailer::Base.deliveries.clear - end + let(:test_organization) { Fabricate(:organization) } + let!(:offer) { Fabricate(:offer, organization: test_organization) } + let!(:inquiry) { Fabricate(:inquiry, organization: test_organization) } + let(:user) { Fabricate(:user, email: "user@example.com", locale: :en) } + let(:member) { Fabricate(:member, organization: test_organization, user: user) } describe "send an email" do it "should send an email" do - expect(ActionMailer::Base.deliveries.count).to eq(1) + expect { + OrganizationNotifier.recent_posts(test_organization.posts, :en, [user]).deliver_now + }.to change { ActionMailer::Base.deliveries.count }.by(1) end end describe "recent posts" do - let(:mail) { OrganizationNotifier.recent_posts(test_organization.posts) } + let(:mail) { OrganizationNotifier.recent_posts(test_organization.posts, :en, [user]) } it "receive email only active and online users" do expect(mail.bcc).to eql(["user@example.com"]) From d5bce3d8439b64d3f24ddeeba567739347ea499c Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Mon, 9 Mar 2020 15:53:36 +0100 Subject: [PATCH 5/8] Upgrade rspec-rails from 3.8 to 3.9 --- Gemfile | 2 +- Gemfile.lock | 26 +++++++++++++------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Gemfile b/Gemfile index 45b563721..545020c95 100644 --- a/Gemfile +++ b/Gemfile @@ -51,7 +51,7 @@ group :development, :test do end group :test do - gem "rspec-rails", '~> 3.8.2' + gem "rspec-rails", '~> 3.9' gem "database_cleaner", '1.6.2' gem 'shoulda-matchers', '~> 3.1.2' gem 'fabrication', '~> 2.20' diff --git a/Gemfile.lock b/Gemfile.lock index b6c18bef4..4b8cb62b9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -288,23 +288,23 @@ GEM rexml (3.2.4) rollbar (2.8.3) multi_json - rspec-core (3.8.0) - rspec-support (~> 3.8.0) - rspec-expectations (3.8.2) + rspec-core (3.9.1) + rspec-support (~> 3.9.1) + rspec-expectations (3.9.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.8.0) - rspec-mocks (3.8.0) + rspec-support (~> 3.9.0) + rspec-mocks (3.9.1) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.8.0) - rspec-rails (3.8.2) + rspec-support (~> 3.9.0) + rspec-rails (3.9.0) actionpack (>= 3.0) activesupport (>= 3.0) railties (>= 3.0) - rspec-core (~> 3.8.0) - rspec-expectations (~> 3.8.0) - rspec-mocks (~> 3.8.0) - rspec-support (~> 3.8.0) - rspec-support (3.8.0) + rspec-core (~> 3.9.0) + rspec-expectations (~> 3.9.0) + rspec-mocks (~> 3.9.0) + rspec-support (~> 3.9.0) + rspec-support (3.9.2) rubocop (0.80.0) jaro_winkler (~> 1.5.1) parallel (~> 1.10) @@ -433,7 +433,7 @@ DEPENDENCIES rails-i18n rdiscount rollbar (= 2.8.3) - rspec-rails (~> 3.8.2) + rspec-rails (~> 3.9) rubocop (~> 0.80.0) sass-rails (~> 5.0.7) select2-rails From 8f62ec69bd9f56b597936338ee052bf56e000c19 Mon Sep 17 00:00:00 2001 From: Marc Anguera Insa Date: Mon, 16 Mar 2020 19:36:33 +0100 Subject: [PATCH 6/8] OrganizationNotifierJob: change strategy from group_by to 1 query per org locale --- app/jobs/organization_notifier_job.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/jobs/organization_notifier_job.rb b/app/jobs/organization_notifier_job.rb index b4e0e5db4..e8949bba6 100644 --- a/app/jobs/organization_notifier_job.rb +++ b/app/jobs/organization_notifier_job.rb @@ -24,7 +24,11 @@ def perform private def users_by_locale(organization) - organization.users.online_active.actives. - notifications.group_by { |u| u.locale || I18n.default_locale } + with_notifications = organization.users.online_active.actives.notifications + org_locales = with_notifications.pluck(:locale).uniq + + org_locales.each_with_object({}) do |locale, hash| + hash[locale] = with_notifications.where(locale: locale) + end end end From 39295b8ef3ac3fcca8f12f812ea268c9edb2ce50 Mon Sep 17 00:00:00 2001 From: Marc Anguera Insa Date: Mon, 16 Mar 2020 21:13:32 +0100 Subject: [PATCH 7/8] add specs for OrganizationNotifierJob --- app/mailers/organization_notifier.rb | 5 ----- spec/jobs/organization_notifier_job_spec.rb | 19 +++++++++++++++++++ spec/mailers/organization_notifier_spec.rb | 2 +- 3 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 spec/jobs/organization_notifier_job_spec.rb diff --git a/app/mailers/organization_notifier.rb b/app/mailers/organization_notifier.rb index 87f3119a3..44117b519 100644 --- a/app/mailers/organization_notifier.rb +++ b/app/mailers/organization_notifier.rb @@ -1,11 +1,6 @@ class OrganizationNotifier < ActionMailer::Base default from: "\"TimeOverflow\" " - # Subject can be set in your I18n file at config/locales/en.yml - # with the following lookup: - # - # en.organization_notifier.recent_posts.subject - # def recent_posts(posts, locale, users) # last 10 posts of offers and inquiries @offers = posts.where(type: "Offer").take(10) diff --git a/spec/jobs/organization_notifier_job_spec.rb b/spec/jobs/organization_notifier_job_spec.rb new file mode 100644 index 000000000..ce5dec963 --- /dev/null +++ b/spec/jobs/organization_notifier_job_spec.rb @@ -0,0 +1,19 @@ +require 'spec_helper' + +RSpec.describe OrganizationNotifierJob, type: :job do + let!(:org) { Fabricate(:organization) } + let!(:user) { Fabricate(:user, locale: :en, sign_in_count: 1) } + let!(:member) { Fabricate(:member, organization: org, user: user) } + let!(:user2) { Fabricate(:user, locale: :ca, sign_in_count: 2) } + let!(:member2) { Fabricate(:member, organization: org, user: user2) } + let!(:offer) { Fabricate(:offer, organization: org, user: user) } + let!(:inquiry) { Fabricate(:inquiry, organization: org, user: user2) } + + describe '#perform' do + it "should send emails in user's locale" do + expect { + OrganizationNotifierJob.perform_now + }.to change { ActionMailer::Base.deliveries.count }.by(2) + end + end +end diff --git a/spec/mailers/organization_notifier_spec.rb b/spec/mailers/organization_notifier_spec.rb index 4b0be3816..4582c1341 100644 --- a/spec/mailers/organization_notifier_spec.rb +++ b/spec/mailers/organization_notifier_spec.rb @@ -24,7 +24,7 @@ it "to should be null" do expect(mail.to).to be_nil end - it "assigns @organization_name" do + it "body contains organization name" do expect(mail.body.encoded).to match(test_organization.name) end end From 91056c163f2ab1581ecf3221887c50cfb7f14128 Mon Sep 17 00:00:00 2001 From: sseerrggii Date: Wed, 18 Mar 2020 17:23:42 +0100 Subject: [PATCH 8/8] change en.yml mail missage --- config/locales/en.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/locales/en.yml b/config/locales/en.yml index 4772e5ebb..d7c393daa 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -347,8 +347,8 @@ en: mailers_globals: footer: text: "%{organization_name} from" - text_donation: The ADBdT association offers TimeOverflow for free to Time Banks. With a donation of %{href} you can help maintain and improve the platform. - text_donation_link: 1€ a month + text_donation: The ADBdT association offers TimeOverflow for free to Time Banks. If you want to help maintain and improve the platform %{href}. + text_donation_link: visit this website offers: edit: submit: Change offer