Skip to content

Commit

Permalink
🧹 Space, Furniture, and Room: Fill in missing assocations
Browse files Browse the repository at this point in the history
- #832
- #1155
- #1154
- #709

As I was working to onboard Piikup, I discovered I couldn't really write
an easy script to merge into a space since I didn't have the
associations together.

Now they're super tight! Yyyaaaaaaah!
  • Loading branch information
zspencer committed Mar 31, 2023
1 parent e6500a4 commit 0a4e702
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 7 deletions.
4 changes: 2 additions & 2 deletions app/models/furniture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class Furniture < ApplicationRecord

ranks :slot, with_same: [:room_id]

belongs_to :room
delegate :space, to: :room
belongs_to :room, inverse_of: :furnitures
has_one :space, through: :room, inverse_of: :furnitures

attribute :furniture_kind, :string

Expand Down
5 changes: 2 additions & 3 deletions app/models/room.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# A Room in Convene acts as a gathering place.
class Room < ApplicationRecord
# The space whose settings govern the default publicity and access controls for the Room.
belongs_to :space
belongs_to :space, inverse_of: :rooms
include WithinLocation
self.location_parent = :space

Expand Down Expand Up @@ -49,8 +49,7 @@ def unlisted?
publicity_level&.to_sym == :unlisted
end

has_many :furnitures, dependent: :destroy_async
accepts_nested_attributes_for :furnitures
has_many :furnitures, dependent: :destroy_async, inverse_of: :room

def full_slug
"#{space.slug}--#{slug}"
Expand Down
1 change: 1 addition & 0 deletions app/models/space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Space < ApplicationRecord

# The Rooms within this Space
has_many :rooms, inverse_of: :space, dependent: :destroy_async
has_many :furnitures, through: :rooms, inverse_of: :space

belongs_to :entrance, class_name: "Room", optional: true

Expand Down
4 changes: 2 additions & 2 deletions spec/models/furniture_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require "rails_helper"

RSpec.describe Furniture do
it { is_expected.to belong_to(:room) }
it { is_expected.to delegate_method(:space).to(:room) }
it { is_expected.to belong_to(:room).inverse_of(:furnitures) }
it { is_expected.to have_one(:space).through(:room).inverse_of(:furnitures) }

describe "#furniture" do
it "returns the configured piece of furniture" do
Expand Down
2 changes: 2 additions & 0 deletions spec/models/room_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

RSpec.describe Room do
let(:space) { Space.new }
it { is_expected.to have_many(:furnitures).inverse_of(:room) }
it { is_expected.to belong_to(:space).inverse_of(:rooms) }

describe ".slug" do
it "creates unique slugs by space scope" do
Expand Down
1 change: 1 addition & 0 deletions spec/models/space_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

RSpec.describe Space do
it { is_expected.to have_many(:rooms) }
it { is_expected.to have_many(:furnitures).through(:rooms).inverse_of(:space) }

it do
expect(subject).to belong_to(:entrance).class_name("Room")
Expand Down

0 comments on commit 0a4e702

Please sign in to comment.