Skip to content

Commit

Permalink
709 adding species to default pet tasks (#879)
Browse files Browse the repository at this point in the history
* Added species to default pet tasks and handled related code

* Addressed comments in PR and updated tests
  • Loading branch information
atbalaji authored Jul 21, 2024
1 parent 375dec5 commit 554ca4c
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Organizations::Staff::DefaultPetTasksController < Organizations::BaseContr
layout "dashboard"

def index
@default_pet_tasks = authorized_scope(DefaultPetTask.all)
@default_pet_tasks = authorized_scope(DefaultPetTask.all).sort_by { |task| task.species }
end

def new
Expand Down Expand Up @@ -43,7 +43,7 @@ def destroy
private

def task_params
params.require(:default_pet_task).permit(:name, :description, :due_in_days, :recurring)
params.require(:default_pet_task).permit(:name, :description, :due_in_days, :species, :recurring)
end

def set_task
Expand Down
10 changes: 10 additions & 0 deletions app/models/application_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,14 @@ def human_enum_name(enum)
value = send(enum)
I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{enum_i18n_key}.#{value}")
end

def self.human_enum_names(enum)
enum_i18n_key = enum.to_s.singularize
names = []
send(enum).each_key do |key|
names << I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{enum_i18n_key}.#{key}")
end

names
end
end
3 changes: 3 additions & 0 deletions app/models/default_pet_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# due_in_days :integer
# name :string not null
# recurring :boolean default(FALSE)
# species :integer
# created_at :datetime not null
# updated_at :datetime not null
# organization_id :bigint not null
Expand All @@ -24,4 +25,6 @@ class DefaultPetTask < ApplicationRecord

validates :name, presence: true
validates_numericality_of :due_in_days, only_integer: true, greater_than_or_equal_to: 0, allow_nil: true

enum :species, ["All", *Pet.species.keys], default: 0
end
2 changes: 1 addition & 1 deletion app/models/pet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Pet < ApplicationRecord

validate :sensible_placement_type

enum species: ["Dog", "Cat"]
enum species: {"Dog" => 1, "Cat" => 2}
enum placement_type: ["Adoptable", "Fosterable", "Adoptable and Fosterable"]

WEIGHT_UNIT_LB = "lb".freeze
Expand Down
2 changes: 1 addition & 1 deletion app/services/organizations/default_pet_task_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
class Organizations::DefaultPetTaskService
def initialize(pet)
@pet = pet
@default_pet_tasks = DefaultPetTask.all
@default_pet_tasks = DefaultPetTask.where(species: DefaultPetTask.species.values_at("All", pet.species))
end

def default_tasks_array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
<div class="form-group">
<%= f.text_area :description, class: 'form-control', rows: 3 %>
</div>
<div class="form-group">
<%= f.select :species,
options_for_select(DefaultPetTask.human_enum_names(:species), task.human_enum_name(:species)),
{ prompt: t('general.please_select') },
{ class: 'form-control' } %>
</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>
Expand Down
22 changes: 18 additions & 4 deletions app/views/organizations/staff/default_pet_tasks/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,21 @@
<table class="table mb-0 text-nowrap table-hover table-centered">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Description</th>
<th scope="col">Due After</th>
<th scope="col">Recurring</th>
<th scope="col">
<%= t('.name') %>
</th>
<th scope="col">
<%= t('.description') %>
</th>
<th scope="col">
<%= t('.species') %>
</th>
<th scope="col">
<%= t('.due_after') %>
</th>
<th scope="col">
<%= t('.recurring') %>
</th>
<th scope="col"></th>
</tr>
</thead>
Expand All @@ -29,6 +40,9 @@
<td>
<%= task.description %>
</td>
<td>
<%= task.human_enum_name(:species) %>
</td>
<td>
<%= task.due_in_days ? pluralize(task.due_in_days, "day") : "" %>
</td>
Expand Down
12 changes: 12 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,12 @@ en:
withdrawn: Withdrawn
successful_applicant: Successful Applicant
adoption_made: Adoption Made
default_pet_task:
species:
one: "Species"
All: All
Dog: Dog
Cat: Cat
errors:
attributes:
general:
Expand Down Expand Up @@ -442,9 +448,15 @@ en:
error: "Default Pet Task not found."
error: "Default Pet Task not found."
index:
name: "Name"
description: "Description"
species: "Species"
due_after: "Due After"
recurring: "Recurring"
default_pet_tasks: "Default Pet Tasks"
create_default_task: "Create Default Task"
are_you_sure_delete: "Are you sure you want to delete this default pet task?"

new:
new_default_pet_task: "New Default Pet Task"
edit:
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240529060957_add_species_to_default_pet_tasks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddSpeciesToDefaultPetTasks < ActiveRecord::Migration[7.1]
def change
add_column :default_pet_tasks, :species, :integer
end
end
1 change: 1 addition & 0 deletions db/schema.rb

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

4 changes: 2 additions & 2 deletions db/seeds/01_alta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@
weight_unit: Pet::WEIGHT_UNITS.sample,
breed: Faker::Creature::Dog.breed,
description: "He just loves a run and a bum scratch at the end of the day",
species: 0,
species: Pet.species["Dog"],
placement_type: 0,
published: true
)
Expand Down Expand Up @@ -362,7 +362,7 @@
weight_unit: Pet::WEIGHT_UNITS.sample,
breed: Faker::Creature::Dog.breed,
description: Faker::Lorem.sentence,
species: 0,
species: Pet.species["Dog"],
placement_type: "Fosterable",
published: true
)
Expand Down
4 changes: 2 additions & 2 deletions db/seeds/02_baja.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@
weight_unit: Pet::WEIGHT_UNITS.sample,
breed: Faker::Creature::Dog.breed,
description: "He just loves a run and a bum scratch at the end of the day",
species: 0,
species: Pet.species["Dog"],
placement_type: 1,
published: true
)
Expand Down Expand Up @@ -315,7 +315,7 @@
weight_unit: Pet::WEIGHT_UNITS.sample,
breed: Faker::Creature::Dog.breed,
description: Faker::Lorem.sentence,
species: 0,
species: Pet.species["Dog"],
placement_type: "Fosterable",
published: true
)
Expand Down
2 changes: 1 addition & 1 deletion test/factories/pets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
placement_type { "Adoptable and Fosterable" }
published { true }
sex { Faker::Creature::Dog.gender }
species { Faker::Number.within(range: 0..1) }
species { Faker::Number.within(range: 1..2) }
weight_from { 10 }
weight_to { 20 }
weight_unit { "lb" }
Expand Down

0 comments on commit 554ca4c

Please sign in to comment.