From 5b2a42c507caf4a310b9add7dbced37b5e050138 Mon Sep 17 00:00:00 2001 From: Zee Spencer <50284+zspencer@users.noreply.github.com> Date: Wed, 13 Mar 2024 18:50:33 -0700 Subject: [PATCH] WIP - Step toward Slottables 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. --- app/controllers/slots_controller.rb | 19 +++++++++++++++++++ app/lib/space_routes.rb | 1 + app/models/room.rb | 2 ++ app/models/slot.rb | 6 ++++-- app/policies/slot_policy.rb | 10 ++++++++++ app/views/rooms/edit.html.erb | 13 +++++++++++++ app/views/slots/new.html.erb | 5 +++++ spec/models/room_spec.rb | 2 ++ spec/models/slot_spec.rb | 4 ++-- spec/system/slots_system_spec.rb | 12 ++++++------ 10 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 app/controllers/slots_controller.rb create mode 100644 app/policies/slot_policy.rb create mode 100644 app/views/slots/new.html.erb diff --git a/app/controllers/slots_controller.rb b/app/controllers/slots_controller.rb new file mode 100644 index 000000000..36517beee --- /dev/null +++ b/app/controllers/slots_controller.rb @@ -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 diff --git a/app/lib/space_routes.rb b/app/lib/space_routes.rb index 6f3891ee4..2b96f8478 100644 --- a/app/lib/space_routes.rb +++ b/app/lib/space_routes.rb @@ -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 diff --git a/app/models/room.rb b/app/models/room.rb index 4794a282a..596893070 100644 --- a/app/models/room.rb +++ b/app/models/room.rb @@ -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} diff --git a/app/models/slot.rb b/app/models/slot.rb index 6cf8228ba..30095d45f 100644 --- a/app/models/slot.rb +++ b/app/models/slot.rb @@ -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] diff --git a/app/policies/slot_policy.rb b/app/policies/slot_policy.rb new file mode 100644 index 000000000..80da6fd56 --- /dev/null +++ b/app/policies/slot_policy.rb @@ -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 diff --git a/app/views/rooms/edit.html.erb b/app/views/rooms/edit.html.erb index fab30b491..b87a0ce64 100644 --- a/app/views/rooms/edit.html.erb +++ b/app/views/rooms/edit.html.erb @@ -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 %> +