Skip to content

Commit

Permalink
WIP - Step toward Slottables
Browse files Browse the repository at this point in the history
We decided that instead of creating a `Slot`, we want to create Gizmos
directly, and we want to make those `Gizmo` by sending users to the
actual `/new` page for the particular `Gizmo`.

We're thinking we will make the UI cards of Gizmos, so that they can
include things like why you might want to add that Gizmo, or the help docs
or the ... whatever. We don't know. It'll be fine.
  • Loading branch information
zspencer committed Mar 28, 2024
1 parent 8899c0b commit c8b7267
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 10 deletions.
19 changes: 19 additions & 0 deletions app/controllers/slots_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class SlotsController < ApplicationController
expose :slot, scope: -> { policy_scope(current_room.slots) }

def new
authorize(slot)
end

def create
if authorize(slot).save
redirect_to(slot.slottable.location)
else
render :new, status: :unprocessable_entity
end
end

def slot_params
params.require(:slot).permit([:slottable_type])
end
end
1 change: 1 addition & 0 deletions app/lib/space_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def self.append_routes(router)
router.resources :rooms, only: %i[show edit update new create destroy] do
Furniture.append_routes(router)
router.resources :furnitures, only: %i[create edit update destroy]
router.resources :slots, only: %i[new create]
router.resource :hero_image, controller: "room/hero_images"
end

Expand Down
2 changes: 2 additions & 0 deletions app/models/room.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Room < ApplicationRecord
has_many :gizmos, dependent: :destroy, inverse_of: :room, class_name: :Furniture
accepts_nested_attributes_for :gizmos

has_many :slots, dependent: :destroy, inverse_of: :section

DESCRIPTION_MAX_LENGTH = 300
validates :description, length: {maximum: DESCRIPTION_MAX_LENGTH, allow_blank: true}

Expand Down
6 changes: 4 additions & 2 deletions app/models/slot.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class Slot < ApplicationRecord
belongs_to :section, class_name: "Room"
belongs_to :slottable, polymorphic: true
belongs_to :section, class_name: "Room", inverse_of: :slots
has_one :space, through: :section

belongs_to :slottable, polymorphic: true, inverse_of: :slot

include RankedModel
ranks :slot_order, with_same: [:section_id]
Expand Down
10 changes: 10 additions & 0 deletions app/policies/slot_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class SlotPolicy < ApplicationPolicy
alias_method :slot, :object

def create?
current_person.operator? || current_person.member_of?(slot.space)
end

class Scope < ApplicationScope
end
end
13 changes: 13 additions & 0 deletions app/views/rooms/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@
<%= render "rooms/hero_image/form", room: room %>
<% end %>
<%- end %>
<%- if current_person.operator? || Rails.env.test? %>
<%= render CardComponent.new do |card| %>
<%- card.with_header do %>
<h2>Gizmos (but lighter)</h2>
<%- end %>
<%- card.with_footer(variant: :action_bar) do%>
<%= link_to "Add a Slottable", room.location(:new, child: :slot),
class: "button w-full" %>
<%- end %>
<%- end %>
<%- end %>
<div class="mt-3 gap-y-3">
<%= render CardComponent.new do %>
<h2>Gizmos</h2>
Expand Down
5 changes: 5 additions & 0 deletions app/views/slots/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= form_with(model: [slot.space, slot.section, slot]) do |slot_form| %>
<%= slot_form.label(:slottable_type) %>
<%= slot_form.select(:slottable_type, [:MarkdownTextBlock]) %>
<%= slot_form.submit %>
<%- end %>
2 changes: 2 additions & 0 deletions spec/models/room_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
let(:space) { Space.new }

it { is_expected.to have_many(:gizmos).inverse_of(:room).dependent(:destroy) }
it { is_expected.to have_many(:slots).inverse_of(:section).dependent(:destroy) }

it { is_expected.to belong_to(:space).inverse_of(:rooms) }

describe "#description" do
Expand Down
4 changes: 2 additions & 2 deletions spec/models/slot_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "rails_helper"

RSpec.describe Slot do
it { is_expected.to belong_to(:section).class_name(:Room) }
it { is_expected.to belong_to(:slottable) }
it { is_expected.to belong_to(:section).class_name(:Room).inverse_of(:slots) }
it { is_expected.to belong_to(:slottable).inverse_of(:slot) }
end
12 changes: 6 additions & 6 deletions spec/system/slots_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
sign_in(space.members.first, space)
visit(polymorphic_path(section.location(:edit)))

click_link("Add a Slottable")
click_link("Add Text Block")
expect(page).to have_current_path(polymorphic_path(section.location(:new, child: :text_block)))

select("Text Block", from: "Type")

click_button("Create Slottable")
fill_in_rich_text_area("Body", with: "Prepare yourself for AMAZING")
click_button("Create")

expect(section.slots.count).to eq(1)
expect(section.slots.slottable).to be_a(TextBlock)
expect(page).to have_current_path(polymorphic_path(section.slots.slottable))
expect(section.slots.first.slottable).to be_a(TextBlock)
expect(sections.slots.first.slottable.body).to eq("Prepare yourself for AMAZING")
end
end

0 comments on commit c8b7267

Please sign in to comment.