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 %> +