From 349d63b0bc0c3ae769ec055d2018e22a674a615e Mon Sep 17 00:00:00 2001 From: Celia Collins Date: Thu, 19 Sep 2024 14:14:26 +0100 Subject: [PATCH] Tests working and layout changes --- app/assets/stylesheets/application.scss | 4 ++++ app/controllers/contents_controller.rb | 4 ---- app/jobs/send_message_job.rb | 9 ++++++--- app/models/content.rb | 2 +- app/models/user.rb | 6 ++---- app/views/groups/edit.html.erb | 4 ++-- app/views/groups/index.html.erb | 2 +- app/views/groups/new.html.erb | 4 ++-- app/views/groups/show.html.erb | 2 +- app/views/layouts/application.html.erb | 6 +++--- app/views/messages/new.html.erb | 2 +- app/views/users/dashboard.html.erb | 2 +- app/views/users/index.html.erb | 2 +- app/views/users/new.html.erb | 4 ++-- app/views/users/show.html.erb | 2 +- config/database.yml | 2 +- config/routes.rb | 2 +- lib/tasks/scheduler.rake | 10 ++++++++-- test/factories/content.rb | 6 ++++-- test/factories/group.rb | 6 ++++++ test/jobs/send_message_job_test.rb | 7 ++++--- test/lib/scheduler_send_message_test.rb | 3 ++- test/models/content_test.rb | 9 ++------- test/system/contents_test.rb | 16 ++++++++-------- 24 files changed, 64 insertions(+), 52 deletions(-) create mode 100644 test/factories/group.rb diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index a1dd667..936ac61 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -46,3 +46,7 @@ h1, h2 { h3, h4, h5, h6, p, a, span, div, li, td, th, label, input, textarea, select, button { font-family: 'ReithSans'; } + +body { + background-color: #fafafa; +} diff --git a/app/controllers/contents_controller.rb b/app/controllers/contents_controller.rb index d9a3e42..0f47917 100644 --- a/app/controllers/contents_controller.rb +++ b/app/controllers/contents_controller.rb @@ -1,10 +1,6 @@ class ContentsController < ApplicationController before_action :authenticate_admin! - def index - @contents = Content.all - end - def new @group = Group.find_by(id: params[:group_id]) @content = @group.contents.new diff --git a/app/jobs/send_message_job.rb b/app/jobs/send_message_job.rb index c31529e..70aa963 100644 --- a/app/jobs/send_message_job.rb +++ b/app/jobs/send_message_job.rb @@ -3,8 +3,11 @@ class SendMessageJob < ApplicationJob queue_as :default - def perform(user:, body: "") - content = user.next_content&.body || body + def perform(user:, body: "", group: "") + return unless group.present? || body.present? + + content = group.present? ? user.next_content(group)&.body : body + return unless content.present? @client = Twilio::REST::Client.new(ENV.fetch("TWILIO_ACCOUNT_SID"), ENV.fetch("TWILIO_AUTH_TOKEN")) @@ -23,7 +26,7 @@ def perform(user:, body: "") body: message.body, message_sid: message.sid, status: message.status, - content: user.next_content || nil + content: user.next_content(group) || nil ) end end diff --git a/app/models/content.rb b/app/models/content.rb index d74b90d..117ecdb 100644 --- a/app/models/content.rb +++ b/app/models/content.rb @@ -4,5 +4,5 @@ class Content < ApplicationRecord has_many :messages - validates_presence_of :body + validates_presence_of :body, :link end diff --git a/app/models/user.rb b/app/models/user.rb index 370760a..40c9997 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -18,10 +18,8 @@ def full_name "#{first_name} #{last_name}" end - def next_content - # find relevant content group - group = Group.find_by(age_in_months: child_age_in_months_today) - + def next_content(group) + return unless group.present? # find lowest ranked content minus any they have already seen (group.contents - contents).min_by(&:position) end diff --git a/app/views/groups/edit.html.erb b/app/views/groups/edit.html.erb index 53ea19c..bc05817 100644 --- a/app/views/groups/edit.html.erb +++ b/app/views/groups/edit.html.erb @@ -1,5 +1,5 @@ -
-
+
+
<%= link_to '< Back', groups_path, class: 'underline hover:no-underline' %>
diff --git a/app/views/groups/index.html.erb b/app/views/groups/index.html.erb index ac31069..74caec1 100644 --- a/app/views/groups/index.html.erb +++ b/app/views/groups/index.html.erb @@ -1,4 +1,4 @@ -
+

Message contents

diff --git a/app/views/groups/new.html.erb b/app/views/groups/new.html.erb index f714776..2a3d679 100644 --- a/app/views/groups/new.html.erb +++ b/app/views/groups/new.html.erb @@ -1,5 +1,5 @@ -
-
+
+
<%= link_to '< Back', groups_path, class: 'underline hover:no-underline' %>
diff --git a/app/views/groups/show.html.erb b/app/views/groups/show.html.erb index 26d75bc..2063258 100644 --- a/app/views/groups/show.html.erb +++ b/app/views/groups/show.html.erb @@ -1,4 +1,4 @@ -
+
<%= link_to '< Back', groups_path, class: 'underline hover:no-underline' %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index db548a8..df665ea 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -13,10 +13,10 @@ <%= render "layouts/navbar" %> -
+
<% if flash[:notice] %> -
-

+

+

<%= flash[:notice] %>

diff --git a/app/views/messages/new.html.erb b/app/views/messages/new.html.erb index 46059e4..d7444ae 100644 --- a/app/views/messages/new.html.erb +++ b/app/views/messages/new.html.erb @@ -1,4 +1,4 @@ -
+

Send a message to <%= @user.first_name %>

<%= simple_form_for [@user, @message] do |f| %> diff --git a/app/views/users/dashboard.html.erb b/app/views/users/dashboard.html.erb index e38de07..3da7b6d 100644 --- a/app/views/users/dashboard.html.erb +++ b/app/views/users/dashboard.html.erb @@ -1,5 +1,5 @@ -
+

Messages received

diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb index 69a2218..25a7f4c 100644 --- a/app/views/users/index.html.erb +++ b/app/views/users/index.html.erb @@ -1,5 +1,5 @@ -
+

Users

diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb index 848f805..cb678fa 100644 --- a/app/views/users/new.html.erb +++ b/app/views/users/new.html.erb @@ -1,5 +1,5 @@ -
-
+
+

Be first in line for our free happiness-boosting activities

diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 6fe7cb8..b83d42b 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -1,4 +1,4 @@ -
+
diff --git a/config/database.yml b/config/database.yml index 09ba006..3152dfa 100644 --- a/config/database.yml +++ b/config/database.yml @@ -21,7 +21,7 @@ default: &default development: <<: *default - database: rails_template_development + database: afs_tiny_happy_people # The specified database role being used to connect to PostgreSQL. # To create additional roles in PostgreSQL see `$ createuser --help`. diff --git a/config/routes.rb b/config/routes.rb index 2e99698..fd29e52 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,7 +20,7 @@ end resources :groups do - resources :contents + resources :contents, except: %i[index] end patch '/update_position/:id/', to: 'contents#update_position', as: 'update_position' diff --git a/lib/tasks/scheduler.rake b/lib/tasks/scheduler.rake index 2f3f709..437cbea 100644 --- a/lib/tasks/scheduler.rake +++ b/lib/tasks/scheduler.rake @@ -1,8 +1,14 @@ namespace :scheduler do desc "Send text message" task send_message: :environment do - User.contactable.each do |user| - SendMessageJob.perform_later(user:) + User.contactable.group_by(&:child_age_in_months_today).each do |age, users| + group = Group.find_by(age_in_months: age) + + next unless group + + users.each do |user| + SendMessageJob.perform_later(user:, group:) + end end end end diff --git a/test/factories/content.rb b/test/factories/content.rb index 37f7ac5..0e0c357 100644 --- a/test/factories/content.rb +++ b/test/factories/content.rb @@ -1,7 +1,9 @@ FactoryBot.define do factory :content do body { "Sample Body" } - upper_age { 19 } - lower_age { 18 } + link { "www.example.com" } + sequence(:position) { |n| n } + + group end end diff --git a/test/factories/group.rb b/test/factories/group.rb new file mode 100644 index 0000000..8090ff9 --- /dev/null +++ b/test/factories/group.rb @@ -0,0 +1,6 @@ +FactoryBot.define do + factory :group do + name { "Content for 18 month olds" } + age_in_months { 18 } + end +end diff --git a/test/jobs/send_message_job_test.rb b/test/jobs/send_message_job_test.rb index f52be9c..4de192e 100644 --- a/test/jobs/send_message_job_test.rb +++ b/test/jobs/send_message_job_test.rb @@ -5,18 +5,19 @@ class SendMessageJobTest < ActiveSupport::TestCase test '#perform sends messages with default content if no body present' do user = create(:user) - content = create(:content, lower_age: user.child_age_in_months_today) + group = create(:group, age_in_months: user.child_age_in_months_today) + content = create(:content, group:) stub_successful_twilio_call(content.body, user) - SendMessageJob.new.perform(user:) + SendMessageJob.new.perform(user:, group:) assert_equal 1, Message.count assert_equal content.body, Message.last.body end test '#perform does not send message if no appropriate content available' do - SendMessageJob.new.perform(user: create(:user)) + SendMessageJob.new.perform(user: create(:user), group: create(:group)) assert_equal 0, Message.count end diff --git a/test/lib/scheduler_send_message_test.rb b/test/lib/scheduler_send_message_test.rb index 638af61..ef7541c 100644 --- a/test/lib/scheduler_send_message_test.rb +++ b/test/lib/scheduler_send_message_test.rb @@ -5,7 +5,8 @@ class SchedulerNotifyTest < ActiveSupport::TestCase setup do @user = create(:user) - @content = create(:content, lower_age: @user.child_age_in_months_today) + @group = create(:group, age_in_months: @user.child_age_in_months_today) + @content = create(:content, group: @group) AfsTinyHappyPeople::Application.load_tasks Rake::Task['scheduler:send_message'].execute diff --git a/test/models/content_test.rb b/test/models/content_test.rb index e009f38..425cdad 100644 --- a/test/models/content_test.rb +++ b/test/models/content_test.rb @@ -14,13 +14,8 @@ def setup assert_not @content.valid? end - test "upper_age should be present" do - @content.upper_age = nil - assert_not @content.valid? - end - - test "lower_age should be present" do - @content.lower_age = nil + test "link should be present" do + @content.link = "" assert_not @content.valid? end end diff --git a/test/system/contents_test.rb b/test/system/contents_test.rb index eca6978..ff58b60 100644 --- a/test/system/contents_test.rb +++ b/test/system/contents_test.rb @@ -3,19 +3,19 @@ class ContentsTest < ApplicationSystemTestCase setup do @admin = create(:admin) + @group = create(:group) end test "creating new content" do sign_in - visit contents_path + visit group_path(@group) - assert_text "Messages" + assert_text @group.name - click_on "Create message content" + click_on "Add new message" fill_in "Body", with: "New content" - fill_in "Lower age", with: "18" - fill_in "Upper age", with: "19" + fill_in "Link", with: "www.example.com" click_on "Create" assert_text "Content for message was successfully created" @@ -23,12 +23,12 @@ class ContentsTest < ApplicationSystemTestCase end test "updating a content" do - create(:content, body: "Old Content") + create(:content, body: "Old Content", group: @group) sign_in - visit contents_path + visit group_path(@group) - assert_text "Messages" + assert_text "Old Content" click_on "Edit", match: :first