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 dfbd7db
Show file tree
Hide file tree
Showing 25 changed files with 123 additions and 110 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
117 changes: 59 additions & 58 deletions app/views/interests/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,73 +1,74 @@
<div class="md:flex bg-red-200 px-3 py-5 md:pt-24 md:px-28 md:pb-0">
<div class="md:w-7/12 md:mr-4">
<h1 class="md:text-5xl text-purple-900">
Fun, free happiness-boosting activities to enjoy with your child
</h1>
<p class="text-md md:text-xl mt-6 md:mt-8 mb-0">
“A great guide and confidence builder knowing we were doing alot right and then fantastic guidance in what we can do and apply to further develop our child." - Ali's mum
</p>
<a href="/examples" class="btn btn-primary bg-purple-600 text-white md:w-1/3 w-full block mt-5 md:mt-10">See some examples</a>
</div>
<div class="hidden md:block w-5/12">
<%= image_tag "phones.png" %>
<div class="absolute top-16 left-0">
<div class="md:flex bg-red-200 px-3 py-5 md:pt-24 md:px-28 md:pb-0">
<div class="md:w-7/12 md:mr-4">
<h1 class="md:text-5xl text-purple-900">
Fun, free happiness-boosting activities to enjoy with your child
</h1>
<p class="text-md md:text-xl mt-6 md:mt-8 mb-0">
“A great guide and confidence builder knowing we were doing alot right and then fantastic guidance in what we can do and apply to further develop our child." - Ali's mum
</p>
</div>
<div class="hidden md:block w-5/12">
<%= image_tag "phones.png" %>
</div>
</div>
</div>

<div class="px-3 py-5 md:py-24 md:px-28 bg-amber-300 items-center">
<div class="flex justify-around">
<h2 class="md:text-4xl md:mb-20 mb-10 text-center w-5/6">
Get bite-sized activities that spark imagination and connection delivered to your phone, every week
</h2>
</div>
<div class="flex justify-between">
<div class="w-full flex flex-col md:flex-row justify-around gap-8">
<div class="flex flex-col items-center gap-5">
<div class="w-24 h-24 md:w-40 md:h-40 flex items-center justify-around">
<div>
<%= image_tag "noun-mobile.png" %>
</div>
</div>
<p class="md:text-3xl text-center w-64">
Sign up with your mobile number
</p>
</div>
<div class="flex flex-col items-center gap-5">
<div>
<div class="px-3 py-5 md:py-24 md:px-28 bg-amber-300 items-center">
<div class="flex justify-around">
<h2 class="md:text-4xl md:mb-20 mb-10 text-center w-5/6">
Get bite-sized activities that spark imagination and connection delivered to your phone, every week
</h2>
</div>
<div class="flex justify-between">
<div class="w-full flex flex-col md:flex-row justify-around gap-8">
<div class="flex flex-col items-center gap-5">
<div class="w-24 h-24 md:w-40 md:h-40 flex items-center justify-around">
<div>
<%= image_tag "noun-sms.png" %>
<%= image_tag "noun-mobile.png" %>
</div>
</div>
</div>
<p class="md:text-3xl text-center md:w-96">
Get weekly text messages featuring short videos that demonstrate fun activities to enjoy with your child
</p>
</div>
<div class="flex flex-col items-center gap-5">
<div class="w-24 h-24 md:w-40 md:h-40 flex items-center justify-around">
<p class="md:text-3xl text-center w-64">
Sign up with your mobile number
</p>
</div>
<div class="flex flex-col items-center gap-5">
<div>
<%= image_tag "noun-child.png" %>
<div class="w-24 h-24 md:w-40 md:h-40 flex items-center justify-around">
<div>
<%= image_tag "noun-sms.png" %>
</div>
</div>
</div>
<p class="md:text-3xl text-center md:w-96">
Get weekly text messages featuring short videos that demonstrate fun activities to enjoy with your child
</p>
</div>
<div class="flex flex-col items-center gap-5">
<div class="w-24 h-24 md:w-40 md:h-40 flex items-center justify-around">
<div>
<%= image_tag "noun-child.png" %>
</div>
</div>
<p class="md:text-3xl text-center w-64">
Try them out!
</p>
</div>
<p class="md:text-3xl text-center w-64">
Try them out!
</p>
</div>
</div>
</div>
</div>
</div>

<div class="bg-zinc-50 flex justify-around">
<div class="py-8 px-8 md:py-24 md:w-5/12 w-full" id="interests">
<%= simple_form_for @interest do |form| %>
<h2 class="mb-2 md:text-4xl">
What brings you here today?
</h2>
<%= form.input :title, as: :check_boxes, label: false, collection: Interest::TITLES, wrapper_html: { class: "flex flex-col gap-6 md:text-xl mt-6" } %>
<%= form.input :other_title, label: "Something else?", required: false, wrapper_html: { class: "w-full mt-6" } %>
<div class="bg-zinc-50 flex justify-around">
<div class="py-8 px-8 md:py-24 md:w-5/12 w-full" id="interests">
<%= simple_form_for @interest do |form| %>
<h2 class="mb-2 md:text-4xl">
What brings you here today?
</h2>
<%= form.input :title, as: :check_boxes, label: false, collection: Interest::TITLES, wrapper_html: { class: "flex flex-col gap-6 md:text-xl mt-6" } %>
<%= form.input :other_title, label: "Something else?", required: false, wrapper_html: { class: "w-full mt-6" } %>
<%= form.submit "Next", class: "btn btn-primary mt-8 bg-purple-600 text-white w-full" %>
<% end %>
<%= form.submit "Next", class: "btn btn-primary mt-8 bg-purple-600 text-white w-full" %>
<% end %>
</div>
</div>
</div>
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="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="w-full pt-5">
<p class="w-fit ml-28 px-4 py-2">
<%= 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
Loading

0 comments on commit dfbd7db

Please sign in to comment.