Skip to content

Commit

Permalink
Toggle resource recommendations (#1738)
Browse files Browse the repository at this point in the history
* [#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 <[email protected]>
  • Loading branch information
S-Warmenhoven and AlineRibeiro authored May 11, 2020
1 parent daf3461 commit c7ddc23
Show file tree
Hide file tree
Showing 30 changed files with 132 additions and 65 deletions.
2 changes: 1 addition & 1 deletion app/controllers/moments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 16 additions & 16 deletions app/helpers/moments_form_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# frozen_string_literal: true

module MomentsFormHelper
include MoodsHelper
include CategoriesHelper
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -64,37 +61,40 @@ 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,
moment_strategy, get_viewers_input(
@viewers, 'moment', 'moments', @moment
),
moment_comment, moment_publishing,
Rails.env.development? ? moment_bookmarked : {}
Rails.env.development? ? moment_bookmarked : {},
moment_display_resources
]
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# updated_at :datetime
# user_id :integer
# slug :string
# visible :boolean
# visible :boolean default(TRUE)
#

class Category < ApplicationRecord
Expand Down
1 change: 0 additions & 1 deletion app/models/medication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 16 additions & 14 deletions app/models/moment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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') }
Expand Down
2 changes: 1 addition & 1 deletion app/models/mood.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# updated_at :datetime
# user_id :integer
# slug :string
# visible :boolean
# visible :boolean default(TRUE)
#

class Mood < ApplicationRecord
Expand Down
4 changes: 2 additions & 2 deletions app/models/strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions app/views/moments/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@
</div>
<% end %>
<% if @resources.present? %>
<div class="smallMarginTop">
<div class="label"><%= label_tag t('moments.show.resources') %></div>
<ul>
<% @resources.take(3).each do |item| %>
<li><%= link_to item['name'], item['link'] %></li>
<%end %>
<li><%= link_to "#{t('load_more')}...", "/resources?#{@resources_tags}" %></li>
</ul>
</div>
<% if @moment.resource_recommendations? && @resources.any? %>
<div class="smallMarginTop">
<div class="label"><%= label_tag t('moments.show.resources') %></div>
<ul>
<% @resources.take(3).each do |item| %>
<li><%= link_to item['name'], item['link'] %></li>
<%end %>
<li><%= link_to "#{t('load_more')}...", "/resources?#{@resources_tags}" %></li>
</ul>
</div>
<% end %>
<% if @moment.owned_by?(current_user) && @moment.shared? %>
Expand Down
1 change: 1 addition & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,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'
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,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'
Expand Down
1 change: 1 addition & 0 deletions config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,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'
Expand Down
1 change: 1 addition & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,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'
Expand Down
1 change: 1 addition & 0 deletions config/locales/hi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ hi:
tagged_moments: 'टैग किए गए लम्हें'
new: 'नया पल'
bookmark: 'बुकमार्क'
resource_recommendations: 'अनुशंसित संसाधन प्रदर्शित करें?'
secret_share:
singular: 'गुप्त हिस्सा'
expires_at: 'पर समाप्त'
Expand Down
1 change: 1 addition & 0 deletions config/locales/it.yml
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,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
Expand Down
1 change: 1 addition & 0 deletions config/locales/nb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,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
Expand Down
1 change: 1 addition & 0 deletions config/locales/nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,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'
Expand Down
1 change: 1 addition & 0 deletions config/locales/pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,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'
Expand Down
1 change: 1 addition & 0 deletions config/locales/sv.yml
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,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'
Expand Down
1 change: 1 addition & 0 deletions config/locales/vi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,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'
Expand Down
1 change: 1 addition & 0 deletions config/locales/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ zh-CN:
tagged_moments: '标记的日记'
new: '新日记分享'
bookmark: '将日记加上个书签?'
resource_recommendations: '显示资源推荐?'
secret_share:
singular: '秘密分享'
expires_at: '到期'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddResourceRecommendationsToMoments < ActiveRecord::Migration[5.2]
def change
add_column :moments, :resource_recommendations, :boolean, default: true
end
end
1 change: 1 addition & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
8 changes: 6 additions & 2 deletions spec/controllers/moments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
19 changes: 19 additions & 0 deletions spec/features/user_displays_resources_links_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit c7ddc23

Please sign in to comment.