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

Add models to support request units #4306

Merged
merged 16 commits into from
May 24, 2024
2 changes: 1 addition & 1 deletion app/models/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Item < ApplicationRecord
has_many :storage_locations, through: :inventory_items
has_many :donations, through: :line_items, source: :itemizable, source_type: "::Donation"
has_many :distributions, through: :line_items, source: :itemizable, source_type: "::Distribution"
has_many :request_units, class_name: "ItemRequestUnit", dependent: :destroy
has_many :request_units, class_name: "ItemUnit", dependent: :destroy

scope :active, -> { where(active: true) }

Expand Down
4 changes: 2 additions & 2 deletions app/models/item_request_unit.rb → app/models/item_unit.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# == Schema Information
#
# Table name: item_request_units
# Table name: item_units
#
# id :bigint not null, primary key
# name :string not null
# created_at :datetime not null
# updated_at :datetime not null
# item_id :bigint
#
class ItemRequestUnit < ApplicationRecord
class ItemUnit < ApplicationRecord
belongs_to :item

validate do
Expand Down
2 changes: 1 addition & 1 deletion app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Organization < ApplicationRecord
has_many :transfers
has_many :users, -> { distinct }, through: :roles
has_many :vendors
has_many :request_units
has_many :request_units, class_name: 'Unit'
end

has_many :items, dependent: :destroy do
Expand Down
3 changes: 2 additions & 1 deletion app/models/partners/item_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# name :string
# partner_key :string
# quantity :string
# request_unit :string
# created_at :datetime not null
# updated_at :datetime not null
# item_id :integer
Expand All @@ -29,7 +30,7 @@ class ItemRequest < Base
def request_unit_is_supported
return if request_unit.blank?

names = request.organization.request_units.map(&:name)
names = item.request_units.map(&:name)
unless names.include?(request_unit)

This comment was marked as outdated.

errors.add(:request_unit, "is not supported")
end
Expand Down
4 changes: 2 additions & 2 deletions app/models/request_unit.rb → app/models/unit.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# == Schema Information
#
# Table name: request_units
# Table name: units
#
# id :bigint not null, primary key
# name :string not null
# created_at :datetime not null
# updated_at :datetime not null
# organization_id :bigint
#
class RequestUnit < ApplicationRecord
class Unit < ApplicationRecord
belongs_to :organization
end
4 changes: 2 additions & 2 deletions db/migrate/20240426135118_add_pack_models.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class AddPackModels < ActiveRecord::Migration[7.0]
def change
create_table :request_units do |t|
create_table :units do |t|
t.string :name, null: false
t.references :organization, foreign_key: true
t.timestamps
end

create_table :item_request_units do |t|
create_table :item_units do |t|
t.string :name, null: false
t.references :item, foreign_key: true
t.timestamps
Expand Down
36 changes: 18 additions & 18 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -372,14 +372,6 @@
t.index ["partner_group_id"], name: "index_item_categories_partner_groups_on_partner_group_id"
end

create_table "item_request_units", force: :cascade do |t|
t.string "name", null: false
t.bigint "item_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["item_id"], name: "index_item_request_units_on_item_id"
end

create_table "item_requests", force: :cascade do |t|
t.string "name"
t.string "quantity"
Expand All @@ -394,6 +386,14 @@
t.index ["partner_request_id"], name: "index_item_requests_on_partner_request_id"
end

create_table "item_units", force: :cascade do |t|
t.string "name", null: false
t.bigint "item_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["item_id"], name: "index_item_units_on_item_id"
end

create_table "items", id: :serial, force: :cascade do |t|
t.string "name"
t.string "category"
Expand Down Expand Up @@ -734,14 +734,6 @@
t.datetime "updated_at", null: false
end

create_table "request_units", force: :cascade do |t|
t.string "name", null: false
t.bigint "organization_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["organization_id"], name: "index_request_units_on_organization_id"
end

create_table "requests", force: :cascade do |t|
t.bigint "partner_id"
t.bigint "organization_id"
Expand Down Expand Up @@ -799,6 +791,14 @@
t.index ["organization_id"], name: "index_transfers_on_organization_id"
end

create_table "units", force: :cascade do |t|
t.string "name", null: false
t.bigint "organization_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["organization_id"], name: "index_units_on_organization_id"
end

create_table "users", id: :serial, force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
Expand Down Expand Up @@ -897,7 +897,7 @@
add_foreign_key "item_categories", "organizations"
add_foreign_key "item_categories_partner_groups", "item_categories"
add_foreign_key "item_categories_partner_groups", "partner_groups"
add_foreign_key "item_request_units", "items"
add_foreign_key "item_units", "items"
add_foreign_key "items", "item_categories"
add_foreign_key "items", "kits"
add_foreign_key "kit_allocations", "kits"
Expand All @@ -913,9 +913,9 @@
add_foreign_key "partner_served_areas", "partner_profiles"
add_foreign_key "partners", "storage_locations", column: "default_storage_location_id"
add_foreign_key "product_drives", "organizations"
add_foreign_key "request_units", "organizations"
add_foreign_key "requests", "distributions"
add_foreign_key "requests", "organizations"
add_foreign_key "requests", "partners"
add_foreign_key "units", "organizations"
add_foreign_key "users", "users_roles", column: "last_role_id", on_delete: :nullify
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# == Schema Information
#
# Table name: item_request_units
# Table name: item_units
#
# id :bigint not null, primary key
# name :string not null
Expand All @@ -9,7 +9,7 @@
# item_id :bigint
#
FactoryBot.define do
factory :item_request_unit do
factory :item_unit do
sequence(:name) { |n| "Unit #{n}" }
item

Expand Down
4 changes: 2 additions & 2 deletions spec/factories/request_units.rb → spec/factories/units.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# == Schema Information
#
# Table name: request_units
# Table name: units
#
# id :bigint not null, primary key
# name :string not null
Expand All @@ -9,7 +9,7 @@
# organization_id :bigint
#
FactoryBot.define do
factory :request_unit do
factory :unit do
sequence(:name) { |n| "Unit #{n}" }
organization
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
# updated_at :datetime not null
# item_id :bigint
#
RSpec.describe ItemRequestUnit, type: :model do
RSpec.describe ItemUnit, type: :model do
context "Validations >" do
let(:organization) { create(:organization) }
let(:item) { create(:item, organization: organization) }
it "should only be valid if the organization has a corresponding unit" do
unit = build(:item_request_unit, item: item, name: "pack")
expect(unit.valid?).to eq(false)
expect(unit.errors.full_messages).to eq(["Name is not supported by the organization"])
item_unit = build(:item_unit, item: item, name: "pack")
expect(item_unit.valid?).to eq(false)
expect(item_unit.errors.full_messages).to eq(["Name is not supported by the organization"])

create(:request_unit, organization: organization, name: "pack")
create(:unit, organization: organization, name: "pack")
organization.reload
expect(unit.valid?).to eq(true)
expect(item_unit.valid?).to eq(true)
end
end
end
13 changes: 9 additions & 4 deletions spec/models/partners/item_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# name :string
# partner_key :string
# quantity :string
# request_unit :string
# created_at :datetime not null
# updated_at :datetime not null
# item_id :integer
Expand All @@ -28,17 +29,21 @@
it { should validate_presence_of(:name) }
it { should validate_presence_of(:partner_key) }

it "should only be able to use organization's request units" do
request_unit = create(:request_unit, name: 'pack', organization: organization)
it "should only be able to use item's request units" do
unit = create(:unit, organization: organization, name: 'pack')
item = create(:item, organization: organization)
item_unit = create(:item_unit, name: 'pack', item: item)
request = build(:request, organization: organization)

item_request = build(:item_request, request_unit: "flat", request: request)
item_request = build(:item_request, request_unit: "flat", request: request, item: item)

expect(item_request.valid?).to eq(false)
expect(item_request.errors.full_messages).to eq(["Request unit is not supported"])

request_unit.update!(name: 'flat')
unit.update!(name: 'flat')
organization.reload
item_unit.update!(name: 'flat')
item.reload
expect(item_request.valid?).to eq(true)
end
cielf marked this conversation as resolved.
Show resolved Hide resolved
end
Expand Down
Loading