Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5037 default the quantity per individual to 50 in the data #5080

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/models/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Item < ApplicationRecord
include Exportable
include Valuable

after_initialize :set_default_distribution_quantity, if: :new_record?
after_update :update_associated_kit_name, if: -> { kit.present? }
before_create :set_reporting_category
before_destroy :validate_destroy, prepend: true
Expand Down Expand Up @@ -228,6 +229,10 @@ def set_reporting_category
self.reporting_category = base_item.reporting_category if base_item.reporting_category
end

def set_default_distribution_quantity
self.distribution_quantity ||= kit_id.present? ? 1 : 50
end

def update_associated_kit_name
kit.update(name: name)
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/items/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<% end %>

<%= f.input :name, label: "Quantity Per Individual", wrapper: :input_group do %>
<%= f.input_field :distribution_quantity, class: "form-control" %>
<%= f.input_field :distribution_quantity, class: "form-control", value: f.object.distribution_quantity || (f.object.kit_id.present? ? 1 : 50) %>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this if we have an after_initialize?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it would be helpful for the user to see what the default was in the form. I just removed it and learned that we don't need it after all. Thank you for pointing this out.

<% end %>

<% if current_user.has_cached_role?(Role::ORG_ADMIN, current_organization) %>
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20250307161544_set_default_distribution_quantity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class SetDefaultDistributionQuantity < ActiveRecord::Migration[7.2]
def up
Item.where(distribution_quantity: nil).find_each do |item|
item.update_column(:distribution_quantity, item.kit_id.present? ? 1 : 50)
end
end

def down
raise ActiveRecord::IrreversibleMigration
end
end
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.2].define(version: 2025_03_02_154355) do
ActiveRecord::Schema[7.2].define(version: 2025_03_07_161544) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down
16 changes: 16 additions & 0 deletions spec/models/item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,22 @@
end
end

describe "when distribution_quantity is set by default" do
it "should set distribution_quantity to 50 for regular items" do
item = Item.new
item.valid?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't need to call this - your callback is after_initialize, not before_validate.

expect(item.distribution_quantity).to eq(50)
end

it "should set distribution_quantity to 1 for kits" do
organization = create(:organization)
kit = create(:kit, organization: organization)
item = Item.new(kit: kit)
item.valid?
expect(item.distribution_quantity).to eq(1)
end
end

describe "distribution_quantity and package size" do
it "have nil values if an empty string is passed" do
expect(create(:item, distribution_quantity: '').distribution_quantity).to be_nil
Expand Down