Skip to content

Commit c3c973a

Browse files
committed
Add associations and specs to verify them
1 parent 07354ab commit c3c973a

12 files changed

+100
-84
lines changed

app/models/item_category.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55
# id :bigint not null, primary key
66
# description :text
7-
# name :string
7+
# name :string not null
88
# created_at :datetime not null
99
# updated_at :datetime not null
1010
# organization_id :integer not null
@@ -16,4 +16,5 @@ class ItemCategory < ApplicationRecord
1616

1717
belongs_to :organization
1818
has_many :items, -> { order(name: :asc) }, inverse_of: :item_category
19+
has_and_belongs_to_many :partner_groups
1920
end

app/models/partner.rb

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
#
33
# Table name: partners
44
#
5-
# id :integer not null, primary key
6-
# email :string
7-
# name :string
8-
# notes :text
9-
# quota :integer
10-
# send_reminders :boolean default(FALSE), not null
11-
# status :integer default("uninvited")
12-
# created_at :datetime not null
13-
# updated_at :datetime not null
14-
# organization_id :integer
5+
# id :integer not null, primary key
6+
# email :string
7+
# name :string
8+
# notes :text
9+
# quota :integer
10+
# send_reminders :boolean default(FALSE), not null
11+
# status :integer default("uninvited")
12+
# created_at :datetime not null
13+
# updated_at :datetime not null
14+
# organization_id :integer
15+
# partner_group_id :bigint
1516
#
1617

1718
class Partner < ApplicationRecord
@@ -26,14 +27,10 @@ class Partner < ApplicationRecord
2627
enum status: { uninvited: 0, invited: 1, awaiting_review: 2, approved: 3, error: 4, recertification_required: 5, deactivated: 6 }
2728

2829
belongs_to :organization
30+
belongs_to :partner_group, optional: true
2931
has_many :distributions, dependent: :destroy
30-
3132
has_many :requests, dependent: :destroy
3233

33-
has_many :partner_group_memberships, dependent: :destroy
34-
has_many :partner_groups, through: :partner_group_memberships
35-
has_many :requestable_items, through: :partner_groups, source: :items
36-
3734
has_many_attached :documents
3835

3936
validates :organization, presence: true

app/models/partner_group.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@
1010
#
1111
class PartnerGroup < ApplicationRecord
1212
belongs_to :organization
13-
has_many :partner_group_memberships, dependent: :destroy
14-
has_many :partners, through: :partner_group_memberships
15-
16-
has_many :partner_group_items, dependent: :destroy
17-
has_many :items, through: :partner_group_items
13+
has_many :partners
14+
has_and_belongs_to_many :item_categories
1815

1916
validates :organization, presence: true
2017
validates :name, presence: true, uniqueness: { scope: :organization }

db/migrate/20210729003516_add_join_table_between_partner_groups_to_item_categories.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class AddJoinTableBetweenPartnerGroupsToItemCategories < ActiveRecord::Migration[6.1]
22
def change
3-
create_table :partner_groups_item_categories do |t|
4-
t.references :partner, foreign_key: true
5-
t.references :item_categories, foreign_key: true
3+
create_table :item_categories_partner_groups do |t|
4+
t.references :partner_group, foreign_key: true, null: false
5+
t.references :item_category, foreign_key: true, null: false
66

77
t.timestamps
88
end

db/partners_schema.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 20_200_518_010_905) do
13+
ActiveRecord::Schema.define(version: 2020_05_18_010905) do
14+
1415
# These are extensions that must be enabled in order to support this database
1516
enable_extension "plpgsql"
1617

@@ -21,7 +22,7 @@
2122
t.bigint "blob_id", null: false
2223
t.datetime "created_at", null: false
2324
t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
24-
t.index %w(record_type record_id name blob_id), name: "index_active_storage_attachments_uniqueness", unique: true
25+
t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
2526
end
2627

2728
create_table "active_storage_blobs", force: :cascade do |t|
@@ -115,7 +116,7 @@
115116
t.string "value"
116117
t.datetime "created_at", null: false
117118
t.datetime "updated_at", null: false
118-
t.index %w(feature_key key value), name: "index_flipper_gates_on_feature_key_and_key_and_value", unique: true
119+
t.index ["feature_key", "key", "value"], name: "index_flipper_gates_on_feature_key_and_key_and_value", unique: true
119120
end
120121

121122
create_table "item_requests", force: :cascade do |t|
@@ -261,7 +262,7 @@
261262
t.index ["invitation_token"], name: "index_users_on_invitation_token", unique: true
262263
t.index ["invitations_count"], name: "index_users_on_invitations_count"
263264
t.index ["invited_by_id"], name: "index_users_on_invited_by_id"
264-
t.index %w(invited_by_type invited_by_id), name: "index_users_on_invited_by_type_and_invited_by_id"
265+
t.index ["invited_by_type", "invited_by_id"], name: "index_users_on_invited_by"
265266
t.index ["partner_id"], name: "index_users_on_partner_id"
266267
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
267268
end

db/schema.rb

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 20_210_729_003_516) do
13+
ActiveRecord::Schema.define(version: 2021_07_29_003516) do
14+
1415
# These are extensions that must be enabled in order to support this database
1516
enable_extension "plpgsql"
1617

@@ -32,7 +33,7 @@
3233
t.bigint "record_id", null: false
3334
t.datetime "created_at", null: false
3435
t.datetime "updated_at", null: false
35-
t.index %w(record_type record_id name), name: "index_action_text_rich_texts_uniqueness", unique: true
36+
t.index ["record_type", "record_id", "name"], name: "index_action_text_rich_texts_uniqueness", unique: true
3637
end
3738

3839
create_table "active_storage_attachments", force: :cascade do |t|
@@ -42,7 +43,7 @@
4243
t.bigint "blob_id", null: false
4344
t.datetime "created_at", null: false
4445
t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
45-
t.index %w(record_type record_id name blob_id), name: "index_active_storage_attachments_uniqueness", unique: true
46+
t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
4647
end
4748

4849
create_table "active_storage_blobs", force: :cascade do |t|
@@ -62,7 +63,7 @@
6263
t.string "variation_digest", null: false
6364
t.datetime "created_at", precision: 6, null: false
6465
t.datetime "updated_at", precision: 6, null: false
65-
t.index %w(blob_id variation_digest), name: "index_active_storage_variant_records_uniqueness", unique: true
66+
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
6667
end
6768

6869
create_table "adjustments", id: :serial, force: :cascade do |t|
@@ -99,7 +100,7 @@
99100
t.datetime "updated_at", null: false
100101
t.integer "organization_id"
101102
t.string "barcodeable_type", default: "Item"
102-
t.index %w(barcodeable_type barcodeable_id), name: "index_barcode_items_on_barcodeable_type_and_barcodeable_id"
103+
t.index ["barcodeable_type", "barcodeable_id"], name: "index_barcode_items_on_barcodeable_type_and_barcodeable_id"
103104
t.index ["organization_id"], name: "index_barcode_items_on_organization_id"
104105
end
105106

@@ -136,7 +137,7 @@
136137
t.string "business_name"
137138
t.float "latitude"
138139
t.float "longitude"
139-
t.index %w(latitude longitude), name: "index_diaper_drive_participants_on_latitude_and_longitude"
140+
t.index ["latitude", "longitude"], name: "index_diaper_drive_participants_on_latitude_and_longitude"
140141
end
141142

142143
create_table "diaper_drives", force: :cascade do |t|
@@ -175,7 +176,7 @@
175176
t.integer "organization_id"
176177
t.float "latitude"
177178
t.float "longitude"
178-
t.index %w(latitude longitude), name: "index_donation_sites_on_latitude_and_longitude"
179+
t.index ["latitude", "longitude"], name: "index_donation_sites_on_latitude_and_longitude"
179180
t.index ["organization_id"], name: "index_donation_sites_on_organization_id"
180181
end
181182

@@ -212,7 +213,7 @@
212213
t.string "value"
213214
t.datetime "created_at", null: false
214215
t.datetime "updated_at", null: false
215-
t.index %w(feature_key key value), name: "index_flipper_gates_on_feature_key_and_key_and_value", unique: true
216+
t.index ["feature_key", "key", "value"], name: "index_flipper_gates_on_feature_key_and_key_and_value", unique: true
216217
end
217218

218219
create_table "inventory_items", id: :serial, force: :cascade do |t|
@@ -229,7 +230,16 @@
229230
t.integer "organization_id", null: false
230231
t.datetime "created_at", precision: 6, null: false
231232
t.datetime "updated_at", precision: 6, null: false
232-
t.index %w(name organization_id), name: "index_item_categories_on_name_and_organization_id", unique: true
233+
t.index ["name", "organization_id"], name: "index_item_categories_on_name_and_organization_id", unique: true
234+
end
235+
236+
create_table "item_categories_partner_groups", force: :cascade do |t|
237+
t.bigint "partner_group_id", null: false
238+
t.bigint "item_category_id", null: false
239+
t.datetime "created_at", precision: 6, null: false
240+
t.datetime "updated_at", precision: 6, null: false
241+
t.index ["item_category_id"], name: "index_item_categories_partner_groups_on_item_category_id"
242+
t.index ["partner_group_id"], name: "index_item_categories_partner_groups_on_partner_group_id"
233243
end
234244

235245
create_table "items", id: :serial, force: :cascade do |t|
@@ -263,7 +273,7 @@
263273
t.boolean "active", default: true
264274
t.boolean "visible_to_partners", default: true, null: false
265275
t.integer "value_in_cents", default: 0
266-
t.index %w(name organization_id), name: "index_kits_on_name_and_organization_id", unique: true
276+
t.index ["name", "organization_id"], name: "index_kits_on_name_and_organization_id", unique: true
267277
t.index ["organization_id"], name: "index_kits_on_organization_id"
268278
end
269279

@@ -274,7 +284,7 @@
274284
t.string "itemizable_type"
275285
t.datetime "created_at", null: false
276286
t.datetime "updated_at", null: false
277-
t.index %w(itemizable_id itemizable_type), name: "index_line_items_on_itemizable_id_and_itemizable_type"
287+
t.index ["itemizable_id", "itemizable_type"], name: "index_line_items_on_itemizable_id_and_itemizable_type"
278288
end
279289

280290
create_table "manufacturers", force: :cascade do |t|
@@ -305,7 +315,7 @@
305315
t.integer "default_storage_location"
306316
t.text "partner_form_fields", default: [], array: true
307317
t.integer "account_request_id"
308-
t.index %w(latitude longitude), name: "index_organizations_on_latitude_and_longitude"
318+
t.index ["latitude", "longitude"], name: "index_organizations_on_latitude_and_longitude"
309319
t.index ["short_name"], name: "index_organizations_on_short_name"
310320
end
311321

@@ -314,19 +324,10 @@
314324
t.string "name"
315325
t.datetime "created_at", precision: 6, null: false
316326
t.datetime "updated_at", precision: 6, null: false
317-
t.index %w(name organization_id), name: "index_partner_groups_on_name_and_organization_id", unique: true
327+
t.index ["name", "organization_id"], name: "index_partner_groups_on_name_and_organization_id", unique: true
318328
t.index ["organization_id"], name: "index_partner_groups_on_organization_id"
319329
end
320330

321-
create_table "partner_groups_item_categories", force: :cascade do |t|
322-
t.bigint "partner_id"
323-
t.bigint "item_categories_id"
324-
t.datetime "created_at", precision: 6, null: false
325-
t.datetime "updated_at", precision: 6, null: false
326-
t.index ["item_categories_id"], name: "index_partner_groups_item_categories_on_item_categories_id"
327-
t.index ["partner_id"], name: "index_partner_groups_item_categories_on_partner_id"
328-
end
329-
330331
create_table "partners", id: :serial, force: :cascade do |t|
331332
t.string "name"
332333
t.string "email"
@@ -337,9 +338,9 @@
337338
t.boolean "send_reminders", default: false, null: false
338339
t.text "notes"
339340
t.integer "quota"
340-
t.bigint "partner_groups_id"
341+
t.bigint "partner_group_id"
341342
t.index ["organization_id"], name: "index_partners_on_organization_id"
342-
t.index ["partner_groups_id"], name: "index_partners_on_partner_groups_id"
343+
t.index ["partner_group_id"], name: "index_partners_on_partner_group_id"
343344
end
344345

345346
create_table "purchases", force: :cascade do |t|
@@ -384,7 +385,7 @@
384385
t.float "longitude"
385386
t.integer "square_footage"
386387
t.string "warehouse_type"
387-
t.index %w(latitude longitude), name: "index_storage_locations_on_latitude_and_longitude"
388+
t.index ["latitude", "longitude"], name: "index_storage_locations_on_latitude_and_longitude"
388389
t.index ["organization_id"], name: "index_storage_locations_on_organization_id"
389390
end
390391

@@ -430,7 +431,7 @@
430431
t.index ["invitation_token"], name: "index_users_on_invitation_token", unique: true
431432
t.index ["invitations_count"], name: "index_users_on_invitations_count"
432433
t.index ["invited_by_id"], name: "index_users_on_invited_by_id"
433-
t.index %w(invited_by_type invited_by_id), name: "index_users_on_invited_by"
434+
t.index ["invited_by_type", "invited_by_id"], name: "index_users_on_invited_by"
434435
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
435436
end
436437

@@ -446,7 +447,7 @@
446447
t.float "longitude"
447448
t.datetime "created_at", null: false
448449
t.datetime "updated_at", null: false
449-
t.index %w(latitude longitude), name: "index_vendors_on_latitude_and_longitude"
450+
t.index ["latitude", "longitude"], name: "index_vendors_on_latitude_and_longitude"
450451
end
451452

452453
create_table "versions", force: :cascade do |t|
@@ -457,7 +458,7 @@
457458
t.jsonb "object"
458459
t.datetime "created_at"
459460
t.jsonb "object_changes"
460-
t.index %w(item_type item_id), name: "index_versions_on_item_type_and_item_id"
461+
t.index ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id"
461462
end
462463

463464
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
@@ -471,14 +472,14 @@
471472
add_foreign_key "donations", "manufacturers"
472473
add_foreign_key "donations", "storage_locations"
473474
add_foreign_key "item_categories", "organizations"
475+
add_foreign_key "item_categories_partner_groups", "item_categories"
476+
add_foreign_key "item_categories_partner_groups", "partner_groups"
474477
add_foreign_key "items", "item_categories"
475478
add_foreign_key "items", "kits"
476479
add_foreign_key "kits", "organizations"
477480
add_foreign_key "manufacturers", "organizations"
478481
add_foreign_key "organizations", "account_requests"
479482
add_foreign_key "partner_groups", "organizations"
480-
add_foreign_key "partner_groups_item_categories", "item_categories", column: "item_categories_id"
481-
add_foreign_key "partner_groups_item_categories", "partners"
482483
add_foreign_key "requests", "distributions"
483484
add_foreign_key "requests", "organizations"
484485
add_foreign_key "requests", "partners"

spec/factories/item_categories.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55
# id :bigint not null, primary key
66
# description :text
7-
# name :string
7+
# name :string not null
88
# created_at :datetime not null
99
# updated_at :datetime not null
1010
# organization_id :integer not null

spec/factories/partners.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
#
33
# Table name: partners
44
#
5-
# id :integer not null, primary key
6-
# email :string
7-
# name :string
8-
# notes :text
9-
# quota :integer
10-
# send_reminders :boolean default(FALSE), not null
11-
# status :integer default("uninvited")
12-
# created_at :datetime not null
13-
# updated_at :datetime not null
14-
# organization_id :integer
5+
# id :integer not null, primary key
6+
# email :string
7+
# name :string
8+
# notes :text
9+
# quota :integer
10+
# send_reminders :boolean default(FALSE), not null
11+
# status :integer default("uninvited")
12+
# created_at :datetime not null
13+
# updated_at :datetime not null
14+
# organization_id :integer
15+
# partner_group_id :bigint
1516
#
1617

1718
FactoryBot.define do

spec/factories/users.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
#
33
# Table name: users
44
#
5-
# id :bigint not null, primary key
5+
# id :integer not null, primary key
66
# current_sign_in_at :datetime
77
# current_sign_in_ip :inet
8+
# discarded_at :datetime
89
# email :string default(""), not null
910
# encrypted_password :string default(""), not null
1011
# invitation_accepted_at :datetime
@@ -14,17 +15,20 @@
1415
# invitation_token :string
1516
# invitations_count :integer default(0)
1617
# invited_by_type :string
18+
# last_request_at :datetime
1719
# last_sign_in_at :datetime
1820
# last_sign_in_ip :inet
19-
# name :string
21+
# name :string default("CHANGEME"), not null
22+
# organization_admin :boolean
2023
# remember_created_at :datetime
2124
# reset_password_sent_at :datetime
2225
# reset_password_token :string
2326
# sign_in_count :integer default(0), not null
27+
# super_admin :boolean default(FALSE)
2428
# created_at :datetime not null
2529
# updated_at :datetime not null
26-
# invited_by_id :bigint
27-
# partner_id :bigint
30+
# invited_by_id :integer
31+
# organization_id :integer
2832
#
2933

3034
FactoryBot.define do

0 commit comments

Comments
 (0)