Skip to content

Commit

Permalink
Default tasks - due in days (#461)
Browse files Browse the repository at this point in the history
* Added due_in_days column

* Added validation for due_in_days

* Allow editing due_in_days for default tasks

* Set task due_date based on default task due_in_days

* Added column for due_in_days to index view

* Set a variety of due dates on the seed data tasks
  • Loading branch information
tmorton authored Feb 2, 2024
1 parent e33279f commit 1ee14d8
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ def destroy
private

def task_params
params.require(:default_pet_task).permit(:name, :description)
params.require(:default_pet_task).permit(:name, :description, :due_in_days)
end
end
2 changes: 2 additions & 0 deletions app/models/default_pet_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#
# id :bigint not null, primary key
# description :string
# due_in_days :integer
# name :string not null
# created_at :datetime not null
# updated_at :datetime not null
Expand All @@ -21,4 +22,5 @@ class DefaultPetTask < ApplicationRecord
acts_as_tenant(:organization)

validates :name, presence: true
validates_numericality_of :due_in_days, only_integer: true, greater_than_or_equal_to: 0, allow_nil: true
end
3 changes: 2 additions & 1 deletion app/services/organizations/default_pet_task_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ def default_tasks_array
pet_id: @pet.id,
name: task.name,
description: task.description,
completed: false
completed: false,
due_date: task.due_in_days&.days&.from_now&.beginning_of_day
}
end
end
Expand Down
4 changes: 4 additions & 0 deletions app/views/organizations/default_pet_tasks/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<%= f.text_area :description, class: 'form-control', rows: 3 %>
</div>

<div class="form-group">
<%= f.number_field :due_in_days, class: 'form-control', help: 'The task will be due this number of days after the pet is created.' %>
</div>

<%= f.submit t('general.save'), class: 'btn btn-primary' %>
<%= link_to t('general.cancel'), default_pet_tasks_path, class: 'btn btn-secondary' %>
<% end %>
Expand Down
4 changes: 4 additions & 0 deletions app/views/organizations/default_pet_tasks/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<tr>
<th scope="col">Name</th>
<th scope="col">Description</th>
<th scope="col">Due After</th>
<th scope="col">Recurrence</th>
<th scope="col"></th>
</tr>
Expand All @@ -31,6 +32,9 @@
<td>
<%= task.description %>
</td>
<td>
<%= task.due_in_days ? pluralize(task.due_in_days, "day") : "" %>
</td>
<td>
-
</td>
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240201133430_add_due_in_days_to_default_tasks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDueInDaysToDefaultTasks < ActiveRecord::Migration[7.0]
def change
add_column :default_pet_tasks, :due_in_days, :integer, null: true
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion db/seeds/01_alta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,13 @@
)
pet.images.attach(io: File.open(path), filename: "hero.jpg")

due_dates = [Date.today - 1.day, Date.today, Date.today + 30.days]
DefaultPetTask.all.each_with_index do |task, index|
Task.create!(
pet_id: pet.id,
name: task.name,
description: task.description,
due_date: (Date.today + (index - 1).days)
due_date: due_dates[index]
)
end
end
Expand Down
6 changes: 4 additions & 2 deletions db/seeds/02_baja.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,13 @@
)
pet.images.attach(io: File.open(path), filename: "hero.jpg")

DefaultPetTask.all.each do |task|
due_dates = [Date.today - 1.day, Date.today, Date.today + 30.days]
DefaultPetTask.all.each_with_index do |task, index|
Task.create!(
pet_id: pet.id,
name: task.name,
description: task.description
description: task.description,
due_date: due_dates[index]
)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class Organizations::DefaultPetTasksControllerTest < ActionDispatch::Integration
post default_pet_tasks_path, params: {
default_pet_task: {
name: "New Task",
description: "Descrition of new Task"
description: "Descrition of new Task",
due_in_days: 5
}
}
end
Expand Down
1 change: 1 addition & 0 deletions test/models/default_pet_task_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class DefaultPetTaskTest < ActiveSupport::TestCase
should validate_presence_of(:name)
should validate_numericality_of(:due_in_days).only_integer.is_greater_than_or_equal_to(0).allow_nil

test "should have valid factory" do
assert build(:default_pet_task).valid?
Expand Down
25 changes: 25 additions & 0 deletions test/services/organizations/default_pet_task_service_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "test_helper"

class Organizations::DefaultPetTaskServiceTest < ActiveSupport::TestCase
test "creates tasks from the organization's default tasks" do
organization = create(:organization)
default_task = create(:default_pet_task, organization: organization)
pet = create(:pet, organization: organization)

Organizations::DefaultPetTaskService.new(pet).create_tasks

assert_equal 1, pet.tasks.count
assert_equal default_task.name, pet.tasks.first.name
assert_nil pet.tasks.first.due_date
end

test "creates tasks with due_date set based on the default task's due_in_days" do
organization = create(:organization)
create(:default_pet_task, organization: organization, due_in_days: 5)
pet = create(:pet, organization: organization)

Organizations::DefaultPetTaskService.new(pet).create_tasks

assert_equal 5.days.from_now.beginning_of_day, pet.tasks.first.due_date
end
end

0 comments on commit 1ee14d8

Please sign in to comment.