From ded22d4c451aa0920aab2552cc78ba8ead761918 Mon Sep 17 00:00:00 2001 From: Stephanie Warmenhoven <57923250+S-Warmenhoven@users.noreply.github.com> Date: Mon, 11 May 2020 13:41:13 -0700 Subject: [PATCH] Toggle resource recommendations (#1738) * [#1737] Display new toggle button on Moment edit page * Add resource_recommendation column on moments table * [#1737] Toggle works for moment new and edit * [#1737] Refactored and rubocop * [#1737] Translation for Hindi * [#1737] Translate Norwegian * [#1737] Translate Dutch * [#1737] Translate Swedish * [#1737] Translate Vietnamese * [#1737] Add resource_recommendation column on moments table * [#1737] Toggle works for moment new and edit * [#1737] Refactored and rubocop * [#1737] Transalte resource_recommendations into Brazilian Portuguese * [#1737] Transalte resource_recommendations into Spanish * [#1737] Translate German * [#1737] Transalte resource_recommendations into French * [#1737] Transalte resource_recommendations into Italian * [#1737] Transalte resource_recommendations into Chinese * [#1737] Fix French translation * [#1737] Test if MomentsFormHelper #edit and #create returns correct props for resource recommendations toggle * [#1737] Test if toggle updates moment.resource_recommendations in db * [#1737] Test if correct links are displayed on moment show * [#1737] Test if resource links are not displayed when user chooses so * [#1737] Updated schema and seed file * [#1737] Refactored code to be dry Co-authored-by: Aline Ribeiro --- app/controllers/moments_controller.rb | 2 +- app/helpers/moments_form_helper.rb | 32 +++++++++---------- app/models/category.rb | 2 +- app/models/medication.rb | 1 - app/models/moment.rb | 30 +++++++++-------- app/models/mood.rb | 2 +- app/models/strategy.rb | 4 +-- app/views/moments/show.html.erb | 20 ++++++------ config/locales/de.yml | 1 + config/locales/en.yml | 1 + config/locales/es.yml | 1 + config/locales/fr.yml | 1 + config/locales/hi.yml | 1 + config/locales/it.yml | 1 + config/locales/nb.yml | 1 + config/locales/nl.yml | 1 + config/locales/pt-BR.yml | 1 + config/locales/sv.yml | 1 + config/locales/vi.yml | 1 + config/locales/zh-CN.yml | 1 + ...add_resource_recommendations_to_moments.rb | 5 +++ db/schema.rb | 1 + db/seeds.rb | 4 +-- spec/controllers/moments_controller_spec.rb | 8 +++-- .../user_displays_resources_links_spec.rb | 19 +++++++++++ spec/helpers/moments_form_helper_spec.rb | 22 ++++++++++++- spec/models/category_spec.rb | 1 + spec/models/moment_spec.rb | 28 ++++++++-------- spec/models/mood_spec.rb | 1 + spec/models/strategy_spec.rb | 3 +- 30 files changed, 132 insertions(+), 65 deletions(-) create mode 100644 db/migrate/20200506222107_add_resource_recommendations_to_moments.rb create mode 100644 spec/features/user_displays_resources_links_spec.rb diff --git a/app/controllers/moments_controller.rb b/app/controllers/moments_controller.rb index 8ded8b5592..72bdd22aea 100644 --- a/app/controllers/moments_controller.rb +++ b/app/controllers/moments_controller.rb @@ -99,7 +99,7 @@ def set_moment def moment_params params.require(:moment).permit(:name, :why, :fix, :comment, :draft, - :bookmarked, + :bookmarked, :resource_recommendations, category: [], mood: [], viewers: [], strategy: []) end diff --git a/app/helpers/moments_form_helper.rb b/app/helpers/moments_form_helper.rb index 0bb8fe75cf..007a907cd9 100644 --- a/app/helpers/moments_form_helper.rb +++ b/app/helpers/moments_form_helper.rb @@ -1,5 +1,4 @@ # frozen_string_literal: true - module MomentsFormHelper include MoodsHelper include CategoriesHelper @@ -17,8 +16,7 @@ def edit_moment_props private def moment_input_props(field, type, label, group = false) - { id: "moment_#{field}", - type: type, + { id: "moment_#{field}", type: type, name: "moment[#{field}]#{group ? '[]' : ''}", label: t(label) } end @@ -46,8 +44,7 @@ def quick_create_props(model_relation, form_props) model_name, 'quickCreate', "#{model_name.pluralize}.plural", true ) .merge(placeholder: t('common.form.search_by_keywords'), - checkboxes: checkboxes_for(model_relation), - formProps: form_props) + checkboxes: checkboxes_for(model_relation), formProps: form_props) end def moment_category @@ -64,29 +61,31 @@ def moment_strategy def moment_comment moment_input_props('comment', 'switch', 'comment.allow_comments') - .merge(value: true, - uncheckedValue: false, checked: @moment.comment, + .merge(value: true, uncheckedValue: false, checked: @moment.comment, info: t('comment.hint'), dark: true) end def moment_publishing { id: 'moment_publishing', type: 'switch', - label: t('moments.form.draft_question'), - dark: true, name: 'publishing', - value: '0', uncheckedValue: '1', - checked: !@moment.published? } + label: t('moments.form.draft_question'), dark: true, name: 'publishing', + value: '0', uncheckedValue: '1', checked: !@moment.published? } end def moment_bookmarked moment_input_props('bookmarked', 'switch', 'moments.bookmark') .merge( - value: true, - uncheckedValue: false, - checked: @moment.bookmarked, - dark: true + value: true, uncheckedValue: false, + checked: @moment.bookmarked, dark: true ) end + def moment_display_resources + moment_input_props('resource_recommendations', 'switch', + 'moments.resource_recommendations') + .merge(value: true, uncheckedValue: false, + checked: @moment.resource_recommendations, dark: true) + end + def moment_form_inputs [ moment_name, moment_why, moment_fix, moment_category, moment_mood, @@ -94,7 +93,8 @@ def moment_form_inputs @viewers, 'moment', 'moments', @moment ), moment_comment, moment_publishing, - Rails.env.development? ? moment_bookmarked : {} + Rails.env.development? ? moment_bookmarked : {}, + moment_display_resources ] end diff --git a/app/models/category.rb b/app/models/category.rb index f3de60c5aa..02a520ba33 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -10,7 +10,7 @@ # updated_at :datetime # user_id :integer # slug :string -# visible :boolean +# visible :boolean default(TRUE) # class Category < ApplicationRecord diff --git a/app/models/medication.rb b/app/models/medication.rb index 2c4055081d..161e26f632 100644 --- a/app/models/medication.rb +++ b/app/models/medication.rb @@ -20,7 +20,6 @@ # add_to_google_cal :boolean default(FALSE) # weekly_dosage :integer # default(["0", "1", "2", "3", "4", "5", "6"]), is an Array -# class Medication < ApplicationRecord # dosage: amount of medication taken at one time diff --git a/app/models/moment.rb b/app/models/moment.rb index 5e22e5ffbb..f630c67490 100644 --- a/app/models/moment.rb +++ b/app/models/moment.rb @@ -3,20 +3,21 @@ # # Table name: moments # -# id :bigint not null, primary key -# name :string -# why :text -# fix :text -# created_at :datetime -# updated_at :datetime -# user_id :integer -# viewers :text -# comment :boolean -# slug :string -# secret_share_identifier :uuid -# secret_share_expires_at :datetime -# published_at :datetime -# bookmarked :boolean +# id :bigint not null, primary key +# name :string +# why :text +# fix :text +# created_at :datetime +# updated_at :datetime +# user_id :integer +# viewers :text +# comment :boolean +# slug :string +# secret_share_identifier :uuid +# secret_share_expires_at :datetime +# published_at :datetime +# bookmarked :boolean default(FALSE) +# resource_recommendations :boolean default(TRUE) # class Moment < ApplicationRecord @@ -48,6 +49,7 @@ class Moment < ApplicationRecord validates :why, length: { minimum: 1 } validates :secret_share_expires_at, presence: true, if: :secret_share_identifier? + validates :resource_recommendations, inclusion: [true, false] scope :published, -> { where.not(published_at: nil) } scope :recent, -> { order('created_at DESC') } diff --git a/app/models/mood.rb b/app/models/mood.rb index 38e0f3d3d7..5ffb829025 100644 --- a/app/models/mood.rb +++ b/app/models/mood.rb @@ -10,7 +10,7 @@ # updated_at :datetime # user_id :integer # slug :string -# visible :boolean +# visible :boolean default(TRUE) # class Mood < ApplicationRecord diff --git a/app/models/strategy.rb b/app/models/strategy.rb index 5d03249adb..03d35ab023 100644 --- a/app/models/strategy.rb +++ b/app/models/strategy.rb @@ -13,8 +13,8 @@ # name :string # slug :string # published_at :datetime -# visible :boolean -# bookmarked :boolean +# visible :boolean default(TRUE) +# bookmarked :boolean default(FALSE) # class Strategy < ApplicationRecord diff --git a/app/views/moments/show.html.erb b/app/views/moments/show.html.erb index 5e3709fac8..4a06811dac 100644 --- a/app/views/moments/show.html.erb +++ b/app/views/moments/show.html.erb @@ -43,16 +43,16 @@ <% end %> -<% if @resources.present? %> -
-
<%= label_tag t('moments.show.resources') %>
- -
+<% if @moment.resource_recommendations? && @resources.any? %> +
+
<%= label_tag t('moments.show.resources') %>
+ +
<% end %> <% if @moment.owned_by?(current_user) && @moment.shared? %> diff --git a/config/locales/de.yml b/config/locales/de.yml index 491a5aca9d..e3b101e980 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -341,6 +341,7 @@ de: tagged_moments: 'Getaggte Momente' new: 'Neuer Moment' bookmark: 'Lesezeichen' + resource_recommendations: 'Empfohlene Ressourcen anzeigen?' secret_share: singular: 'Geheimnis teilen' expires_at: 'Läuft ab am' diff --git a/config/locales/en.yml b/config/locales/en.yml index 1d64b3c23c..f571e2786d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -329,6 +329,7 @@ en: tagged_moments: 'Tagged Moments' new: 'New Moment' bookmark: 'Bookmark this moment?' + resource_recommendations: 'Display Resource Recommendations?' secret_share: singular: 'Secret Share' expires_at: 'Expire at' diff --git a/config/locales/es.yml b/config/locales/es.yml index 90e9025553..7d17feefea 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -307,6 +307,7 @@ es: tagged_moments: 'Etiquetar Momento' new: 'Nuevo Momento' bookmark: 'Marcador' + resource_recommendations: '¿Mostrar recomendación de recursos?' secret_share: singular: 'Compartir en Secreto' expires_at: 'Caduca en' diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 74bbfead5d..57d3d9f560 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -338,6 +338,7 @@ fr: tagged_moments: 'Moments étiquetés' new: 'Nouveau moment' bookmark: 'Signet' + resource_recommendations: 'Voulez-vous afficher la recommandation de ressource?' secret_share: singular: 'Partage de secret' expires_at: 'Expire le' diff --git a/config/locales/hi.yml b/config/locales/hi.yml index 5ab7c266ed..1568170acb 100644 --- a/config/locales/hi.yml +++ b/config/locales/hi.yml @@ -295,6 +295,7 @@ hi: tagged_moments: 'टैग किए गए लम्हें' new: 'नया पल' bookmark: 'बुकमार्क' + resource_recommendations: 'अनुशंसित संसाधन प्रदर्शित करें?' secret_share: singular: 'गुप्त हिस्सा' expires_at: 'पर समाप्त' diff --git a/config/locales/it.yml b/config/locales/it.yml index 349fe9df80..8314f8edf0 100644 --- a/config/locales/it.yml +++ b/config/locales/it.yml @@ -311,6 +311,7 @@ it: tagged_moments: 'Momenti taggati' new: 'Nuovo Momento' bookmark: 'Segnalibro' + resource_recommendations: 'Mostra raccomandazione sulle risorse?' secret_share: singular: 'Condivisione Segreta' expires_at: Scadenza diff --git a/config/locales/nb.yml b/config/locales/nb.yml index bc2404a269..c653e538ce 100644 --- a/config/locales/nb.yml +++ b/config/locales/nb.yml @@ -308,6 +308,7 @@ nb: tagged_moments: 'tag øyeblikk' new: 'Nytt øyeblikk' bookmark: 'Bookmark' + resource_recommendations: 'Vise anbefalte ressurser?' secret_share: singular: 'Hemmelig deling' expires_at: Utløper diff --git a/config/locales/nl.yml b/config/locales/nl.yml index 7d3d73aff7..17d73a182e 100644 --- a/config/locales/nl.yml +++ b/config/locales/nl.yml @@ -311,6 +311,7 @@ nl: tagged_moments: 'Getagde Momenten' new: 'Nieuw Moment' bookmark: 'Bladwijzer' + resource_recommendations: 'Aanbevolen bronnen weergeven?' secret_share: singular: 'Geheim Delen' expires_at: 'Verloopt op' diff --git a/config/locales/pt-BR.yml b/config/locales/pt-BR.yml index e3d2d360ba..ea6c19dbc8 100644 --- a/config/locales/pt-BR.yml +++ b/config/locales/pt-BR.yml @@ -315,6 +315,7 @@ pt-BR: tagged_moments: 'Momentos com tags' new: 'Novo momento' bookmark: 'Marca páginas' + resource_recommendations: 'Mostrar recomendação de recursos?' secret_share: singular: 'Compartilhar segredo' expires_at: 'Expirar em' diff --git a/config/locales/sv.yml b/config/locales/sv.yml index 086934d074..9ab54c7f94 100644 --- a/config/locales/sv.yml +++ b/config/locales/sv.yml @@ -307,6 +307,7 @@ sv: tagged_moments: 'Taggade Ögonblick' new: 'Nya Ögonblick' bookmark: 'Bokmärke' + resource_recommendations: 'Visa rekommenderade resurser?' secret_share: singular: 'Hemlig Andel' expires_at: 'Förfaller vid' diff --git a/config/locales/vi.yml b/config/locales/vi.yml index a41e51a511..82488705b2 100644 --- a/config/locales/vi.yml +++ b/config/locales/vi.yml @@ -306,6 +306,7 @@ vi: tagged_moments: 'Những khoản khắc đã được ghi' new: 'Khoản khắc mới' bookmark: 'Đánh dấu trang' + resource_recommendations: 'Hiển thị tài nguyên được đề xuất?' secret_share: singular: 'Chia sẻ bí mật' expires_at: 'Hết hạn lúc' diff --git a/config/locales/zh-CN.yml b/config/locales/zh-CN.yml index 828711c244..c9fe80f9cf 100644 --- a/config/locales/zh-CN.yml +++ b/config/locales/zh-CN.yml @@ -290,6 +290,7 @@ zh-CN: tagged_moments: '标记的日记' new: '新日记分享' bookmark: '将日记加上个书签?' + resource_recommendations: '显示资源推荐?' secret_share: singular: '秘密分享' expires_at: '到期' diff --git a/db/migrate/20200506222107_add_resource_recommendations_to_moments.rb b/db/migrate/20200506222107_add_resource_recommendations_to_moments.rb new file mode 100644 index 0000000000..668cd2bb1f --- /dev/null +++ b/db/migrate/20200506222107_add_resource_recommendations_to_moments.rb @@ -0,0 +1,5 @@ +class AddResourceRecommendationsToMoments < ActiveRecord::Migration[5.2] + def change + add_column :moments, :resource_recommendations, :boolean, default: true + end +end diff --git a/db/schema.rb b/db/schema.rb index b86aca3452..88dffa9260 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -145,6 +145,7 @@ t.datetime "secret_share_expires_at" t.datetime "published_at" t.boolean "bookmarked", default: false + t.boolean "resource_recommendations", default: true t.index ["secret_share_identifier"], name: "index_moments_on_secret_share_identifier", unique: true t.index ["slug"], name: "index_moments_on_slug", unique: true end diff --git a/db/seeds.rb b/db/seeds.rb index 309256db68..93a128592c 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -23,7 +23,7 @@ user1_category1 = Category.create(user_id: user1.id, name: 'Public Speaking', description: 'Speaking in front of an audience at school') user1_mood1 = Mood.create(user_id: user1.id, name: 'Anxious', description: 'Sweaty palms, increased heart rate') user1_mood2 = Mood.create(user_id: user1.id, name: 'Shy', description: 'I swallow my words and start speaking fast') -user1_moment1 = Moment.create(user_id: user1.id, category: Array.new(1, user1_category1.id), mood: [user1_mood1.id, user1_mood2.id], name: 'Presentation for ENGL 101', why: 'I am presenting in front of my classmates and I am worried I will make a fool out of myself', viewers: [user2.id, user3.id], comment: true) +user1_moment1 = Moment.create(user_id: user1.id, category: Array.new(1, user1_category1.id), mood: [user1_mood1.id, user1_mood2.id], name: 'Presentation for ENGL 101', why: 'I am presenting in front of my classmates and I am worried I will make a fool out of myself', viewers: [user2.id, user3.id], comment: true, resource_reccomedations: true) user1_moment1_comment = Comment.create(commentable_type: 'moment', commentable_id: user1_moment1.id, comment_by: user2.id, comment: "Good luck on the presentation! Just pretend everyone is in underpants :)", visibility: 'private') user1_group1 = Group.create(name: 'Students with Anxiety', description: 'A support group for students to discuss anxiety weekly') user1_group1_member1 = GroupMember.create(group_id: user1_group1.id, user_id: user1.id, leader: true) @@ -38,6 +38,6 @@ user2_category1 = Category.create(user_id: user2.id, name: 'Brother', description: 'We have a strained relationship') user2_mood1 = Mood.create(user_id: user2.id, name: 'Angry', description: 'I become violent and act irrationally') user2_mood2 = Mood.create(user_id: user2.id, name: 'Exhausted', description: 'No motivation to do anything') -user2_moment1 = Moment.create(user_id: user2.id, category: Array.new(1, user2_category1.id), mood: Array.new(1, user2_mood1.id), name: 'Thanksgiving Dinner', why: 'He kept asserting to everyone that I was immature and he always did everything for me.', viewers: Array.new(1, user1.id), comment: false) +user2_moment1 = Moment.create(user_id: user2.id, category: Array.new(1, user2_category1.id), mood: Array.new(1, user2_mood1.id), name: 'Thanksgiving Dinner', why: 'He kept asserting to everyone that I was immature and he always did everything for me.', viewers: Array.new(1, user1.id), comment: false, resource_reccomedations: true) user2_moment1_comment = Comment.create(commentable_type: 'moment', commentable_id: user2_moment1.id, comment_by: user1.id, comment: "You should talk to him one-on-one and tell him how you feel!", visibility: 'all') user1_group1_member2 = GroupMember.create(group_id: user1_group1.id, user_id: user2.id, leader: true) diff --git a/spec/controllers/moments_controller_spec.rb b/spec/controllers/moments_controller_spec.rb index e3adcd0f39..342d927015 100644 --- a/spec/controllers/moments_controller_spec.rb +++ b/spec/controllers/moments_controller_spec.rb @@ -131,8 +131,8 @@ def post_create(moment_params) end describe '#update' do - let!(:moment) { create(:moment, user: user) } - let(:valid_moment_params) { { why: 'updated why' } } + let!(:moment) { create(:moment, user: user, resource_recommendations: true) } + let(:valid_moment_params) { { why: 'updated why', resource_recommendations: false } } let(:invalid_moment_params) { { why: nil } } context 'when the user is logged in' do @@ -147,6 +147,10 @@ def post_create(moment_params) expect(moment.reload.why).to eq('updated why') end + it 'updates the resource_recommendations toggle' do + expect(moment.reload.resource_recommendations).to eq(false) + end + it 'redirects to the show page' do expect(response).to redirect_to(moment_path(moment)) end diff --git a/spec/features/user_displays_resources_links_spec.rb b/spec/features/user_displays_resources_links_spec.rb new file mode 100644 index 0000000000..334ec29476 --- /dev/null +++ b/spec/features/user_displays_resources_links_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +describe 'Display of recommended resource links', js: true do + let(:user) { create :user } + + scenario 'User allows' do + login_as user + moment = create :moment, user_id: user.id, name: 'Teachers', resource_recommendations: 'true' + visit moment_path(moment) + expect(page).to have_content 'Insight Timer' + end + + scenario 'User does not allow' do + login_as user + moment = create :moment, user_id: user.id, name: 'Teachers', resource_recommendations: 'false' + visit moment_path(moment) + expect(page).not_to have_content 'Insight Timer' + end +end diff --git a/spec/helpers/moments_form_helper_spec.rb b/spec/helpers/moments_form_helper_spec.rb index 2bf7543060..19bf4aaeb2 100644 --- a/spec/helpers/moments_form_helper_spec.rb +++ b/spec/helpers/moments_form_helper_spec.rb @@ -236,6 +236,16 @@ uncheckedValue: '1', value: '0' }, + { + id: 'moment_resource_recommendations', + type: 'switch', + name: 'moment[resource_recommendations]', + label: 'Display Resource Recommendations?', + value: true, + uncheckedValue: false, + checked: true, + dark: true + }, { dark: true, id: 'submit', @@ -249,7 +259,7 @@ end describe '#edit_moment_props' do - let(:moment) { FactoryBot.create(:moment, user: user, category: [category.id], mood: [mood.id], strategy: [strategy.id]) } + let(:moment) { FactoryBot.create(:moment, user: user, category: [category.id], mood: [mood.id], strategy: [strategy.id], resource_recommendations: false) } before do @category = category @@ -493,6 +503,16 @@ uncheckedValue: '1', checked: true }, + { + id: 'moment_resource_recommendations', + type: 'switch', + name: 'moment[resource_recommendations]', + label: 'Display Resource Recommendations?', + value: true, + uncheckedValue: false, + checked: false, + dark: true + }, { id: '_method', name: '_method', diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb index 135043fdb5..ad9744b13a 100644 --- a/spec/models/category_spec.rb +++ b/spec/models/category_spec.rb @@ -10,6 +10,7 @@ # updated_at :datetime # user_id :integer # slug :string +# visible :boolean default(TRUE) # describe Category do diff --git a/spec/models/moment_spec.rb b/spec/models/moment_spec.rb index c8ab6f88b2..20fc19d1fd 100644 --- a/spec/models/moment_spec.rb +++ b/spec/models/moment_spec.rb @@ -3,19 +3,21 @@ # # Table name: moments # -# id :bigint not null, primary key -# name :string -# why :text -# fix :text -# created_at :datetime -# updated_at :datetime -# user_id :integer -# viewers :text -# comment :boolean -# slug :string -# secret_share_identifier :uuid -# secret_share_expires_at :datetime -# published_at :datetime +# id :bigint not null, primary key +# name :string +# why :text +# fix :text +# created_at :datetime +# updated_at :datetime +# user_id :integer +# viewers :text +# comment :boolean +# slug :string +# secret_share_identifier :uuid +# secret_share_expires_at :datetime +# published_at :datetime +# bookmarked :boolean default(FALSE) +# resource_recommendations :boolean default(TRUE) # describe Moment do diff --git a/spec/models/mood_spec.rb b/spec/models/mood_spec.rb index 3d50335d03..568a4d3fc3 100644 --- a/spec/models/mood_spec.rb +++ b/spec/models/mood_spec.rb @@ -10,6 +10,7 @@ # updated_at :datetime # user_id :integer # slug :string +# visible :boolean default(TRUE) # describe Mood do diff --git a/spec/models/strategy_spec.rb b/spec/models/strategy_spec.rb index 6918f14f2b..6b55d43bd1 100644 --- a/spec/models/strategy_spec.rb +++ b/spec/models/strategy_spec.rb @@ -5,7 +5,6 @@ # # id :bigint not null, primary key # user_id :integer -# category :text # description :text # viewers :text # comment :boolean @@ -14,6 +13,8 @@ # name :string # slug :string # published_at :datetime +# visible :boolean default(TRUE) +# bookmarked :boolean default(FALSE) # describe Strategy do