Skip to content

Commit

Permalink
🧹 Space, Furniture, and Room: Fill in missing associations (#1285)
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 authored Apr 2, 2023
2 parents 3e41b00 + d6429bb commit 77a01bf
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 6 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
4 changes: 2 additions & 2 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,7 +49,7 @@ def unlisted?
publicity_level&.to_sym == :unlisted
end

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

def full_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
3 changes: 3 additions & 0 deletions spec/models/room_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
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
space_1 = Space.create(name: "space1")
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 77a01bf

Please sign in to comment.