-
-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4306 from rubyforgood/4096-packs-models
Add models to support request units
- Loading branch information
Showing
14 changed files
with
211 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# == Schema Information | ||
# | ||
# 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 ItemUnit < ApplicationRecord | ||
belongs_to :item | ||
|
||
validate do | ||
names = item.organization.request_units.map(&:name) | ||
unless names.include?(name) | ||
errors.add(:name, "is not supported by the organization") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# == Schema Information | ||
# | ||
# 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 Unit < ApplicationRecord | ||
belongs_to :organization | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class AddPackModels < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :units do |t| | ||
t.string :name, null: false | ||
t.references :organization, foreign_key: true | ||
t.timestamps | ||
end | ||
|
||
create_table :item_units do |t| | ||
t.string :name, null: false | ||
t.references :item, foreign_key: true | ||
t.timestamps | ||
end | ||
|
||
end | ||
end |
5 changes: 5 additions & 0 deletions
5
db/migrate/20240503200211_add_request_units_to_item_requests.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddRequestUnitsToItemRequests < ActiveRecord::Migration[7.0] | ||
def change | ||
add_column :item_requests, :request_unit, :string | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# == Schema Information | ||
# | ||
# 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 | ||
# | ||
FactoryBot.define do | ||
factory :item_unit do | ||
sequence(:name) { |n| "Unit #{n}" } | ||
item | ||
|
||
before(:create) do |unit, _| | ||
unit.item.organization.request_units.find_or_create_by(name: unit.name) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
FactoryBot.define do | ||
factory :item_request, class: Partners::ItemRequest do | ||
item | ||
request { build(:request, organization: item.organization) } | ||
quantity { 5 } | ||
sequence(:name) { |n| "Item Request #{n}" } | ||
sequence(:partner_key) { |n| "partner_key#{n}" } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# == Schema Information | ||
# | ||
# 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 | ||
# | ||
FactoryBot.define do | ||
factory :unit do | ||
sequence(:name) { |n| "Unit #{n}" } | ||
organization | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# == Schema Information | ||
# | ||
# Table name: item_request_units | ||
# | ||
# id :bigint not null, primary key | ||
# name :string not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# item_id :bigint | ||
# | ||
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 | ||
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(:unit, organization: organization, name: "pack") | ||
organization.reload | ||
expect(item_unit.valid?).to eq(true) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters