Skip to content

Commit

Permalink
Tests working and layout changes
Browse files Browse the repository at this point in the history
  • Loading branch information
cdccollins committed Sep 19, 2024
1 parent 40af730 commit 349d63b
Show file tree
Hide file tree
Showing 24 changed files with 64 additions and 52 deletions.
4 changes: 4 additions & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 0 additions & 4 deletions app/controllers/contents_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 6 additions & 3 deletions app/jobs/send_message_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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
2 changes: 1 addition & 1 deletion app/models/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ class Content < ApplicationRecord

has_many :messages

validates_presence_of :body
validates_presence_of :body, :link
end
6 changes: 2 additions & 4 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/views/groups/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="flex justify-around bg-zinc-50">
<div class="w-full md:w-5/12 px-5 py-6 md:py-16">
<div class="flex justify-around">
<div class="w-full md:w-5/12">
<div class="mb-2">
<%= link_to '< Back', groups_path, class: 'underline hover:no-underline' %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/groups/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="p-20 bg-zinc-50 flex justify-around">
<div class="flex justify-around">
<div class="w-3/4">
<div class="flex justify-between items-baseline">
<h1>Message contents</h1>
Expand Down
4 changes: 2 additions & 2 deletions app/views/groups/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="flex justify-around bg-zinc-50">
<div class="w-full md:w-5/12 px-5 py-6 md:py-16">
<div class="flex justify-around">
<div class="w-full md:w-5/12">
<div class="mb-2">
<%= link_to '< Back', groups_path, class: 'underline hover:no-underline' %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/groups/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="p-20 bg-zinc-50 flex justify-around">
<div class="flex justify-around">
<div class="w-3/4">
<div class="mb-2">
<%= link_to '< Back', groups_path, class: 'underline hover:no-underline' %>
Expand Down
6 changes: 3 additions & 3 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

<body>
<%= render "layouts/navbar" %>
<main>
<main class="bg-zinc-50 py-20 w-full <%= @full_width ? '' : 'xl:container' %> mx-auto flex-1 <%= @disable_layout_padding ? '' : 'p-4' %>">
<% if flash[:notice] %>
<div class="bg-red-200 w-full border-b border-red-200 pt-5">
<p class="bg-red-400 w-fit ml-28 px-4 py-2 rounded-lg">
<div class="bg-zinc-50 w-full pt-5">
<p class="w-fit ml-28 px-4 py-2 rounded-lg">
<%= flash[:notice] %>
</p>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/messages/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="p-28 bg-zinc-50 flex justify-around">
<div class="flex justify-around">
<div class="w-1/2">
<h1 class="text-3xl font-bold">Send a message to <%= @user.first_name %></h1>
<%= simple_form_for [@user, @message] do |f| %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/dashboard.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

<div class="p-20 bg-zinc-50 flex justify-around">
<div class="flex justify-around">
<div class="w-3/4">
<h1 class="text-3xl">Messages received</h1>
<table class="table-auto w-full">
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

<div class="p-20 bg-zinc-50 flex justify-around">
<div class="flex justify-around">
<div class="w-3/4">
<h1 class="text-3xl">Users</h1>
<table class="table-auto w-full">
Expand Down
4 changes: 2 additions & 2 deletions app/views/users/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="flex justify-around bg-zinc-50">
<div class="w-full md:w-5/12 px-5 py-6 md:py-16">
<div class="flex justify-around ">
<div class="w-full md:w-5/12">
<h1 class="text-purple-900 md:text-4xl text-2xl">
Be first in line for our free happiness-boosting activities
</h1>
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="p-20 bg-zinc-50 flex justify-around">
<div class="flex justify-around">
<div class="w-3/4">
<div class="bg-white px-4 py-4">
<div class="flex justify-between items-center">
Expand Down
2 changes: 1 addition & 1 deletion config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
10 changes: 8 additions & 2 deletions lib/tasks/scheduler.rake
Original file line number Diff line number Diff line change
@@ -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
6 changes: 4 additions & 2 deletions test/factories/content.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions test/factories/group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryBot.define do
factory :group do
name { "Content for 18 month olds" }
age_in_months { 18 }
end
end
7 changes: 4 additions & 3 deletions test/jobs/send_message_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion test/lib/scheduler_send_message_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 2 additions & 7 deletions test/models/content_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 8 additions & 8 deletions test/system/contents_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@
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"
assert_text "New content"
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

Expand Down

0 comments on commit 349d63b

Please sign in to comment.