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 all 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
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
14 changes: 14 additions & 0 deletions spec/models/item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,20 @@
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
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)
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
Loading