Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Operation slot machine #2301

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/furniture/content_block.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ContentBlock < ApplicationRecord
belongs_to :slot
has_one :section, through: :slot
has_one :space, through: :section
end
11 changes: 11 additions & 0 deletions app/furniture/content_block/content_block_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class ContentBlock
class ContentBlockPolicy < ApplicationPolicy
alias_method :content_block, :object
def create?
current_person.member_of?(content_block.space)
end

class Scope < ApplicationScope
end
end
end
9 changes: 9 additions & 0 deletions app/furniture/content_block/content_blocks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ContentBlock
class ContentBlocksController < ApplicationController
expose :content_block, scope: -> { policy_scope(ContentBlock, policy_scope_class: ContentBlockPolicy::Scope) }, model: ContentBlock

def new
authorize(content_block, policy_class: ContentBlockPolicy)
end
end
end
Empty file.
Empty file added app/lib/appends_routes.rb
Empty file.
1 change: 1 addition & 0 deletions app/lib/space_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def self.append_routes(router)
Furniture.append_routes(router)
router.resources :furnitures, only: %i[create edit update destroy]
router.resource :hero_image, controller: "room/hero_images"
router.resources :content_block, controller: "content_block/content_blocks"
end

router.resources :utilities
Expand Down
2 changes: 1 addition & 1 deletion app/models/furniture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
# as JSON, so that {Furniture} can be tweaked and configured as appropriate for
# it's particular use case.
class Furniture < ApplicationRecord
include RankedModel
location(parent: :room)

include RankedModel
ranks :slot, with_same: [:room_id]

belongs_to :room, inverse_of: :gizmos
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
2 changes: 2 additions & 0 deletions app/models/section.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Section < Room
end
10 changes: 10 additions & 0 deletions app/models/slot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Slot < ApplicationRecord
belongs_to :section, class_name: "Room", inverse_of: :slots
self.location_parent = :section
has_one :space, through: :section

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

include RankedModel
ranks :slot_order, with_same: [:section_id]
end
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
11 changes: 11 additions & 0 deletions app/views/rooms/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@
<%= 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 %>

<%= link_to "Add Content Block", room.location(:new, child: :content_block), class: "button"%>
<%- end %>
<%- end %>

<div class="mt-3 gap-y-3">
<%= render CardComponent.new do %>
<h2>Gizmos</h2>
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20240314003612_create_slots.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateSlots < ActiveRecord::Migration[7.1]
def change
create_table :slots, id: :uuid do |t|
t.references :section, foreign_key: {to_table: :rooms}, type: :uuid
t.references :slottable, polymorphic: true, type: :uuid
t.integer :slot_order, null: true
t.timestamps
end
end
end
7 changes: 7 additions & 0 deletions db/migrate/20240328004901_create_content_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class CreateContentBlocks < ActiveRecord::Migration[7.1]
def change
create_table :content_blocks, id: :uuid do |t|
t.timestamps
end
end
end
17 changes: 17 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
t.index ["person_id"], name: "index_authentication_methods_on_person_id"
end

create_table "content_blocks", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "friendly_id_slugs", force: :cascade do |t|
t.string "slug", null: false
t.integer "sluggable_id", null: false
Expand Down Expand Up @@ -311,6 +316,17 @@
t.index ["space_id"], name: "index_rooms_on_space_id"
end

create_table "slots", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.uuid "section_id"
t.string "slottable_type"
t.uuid "slottable_id"
t.integer "slot_order"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["section_id"], name: "index_slots_on_section_id"
t.index ["slottable_type", "slottable_id"], name: "index_slots_on_slottable"
end

create_table "space_agreements", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.uuid "space_id"
t.string "name", null: false
Expand Down Expand Up @@ -373,6 +389,7 @@
add_foreign_key "marketplace_vendor_representatives", "people"
add_foreign_key "memberships", "invitations"
add_foreign_key "rooms", "media", column: "hero_image_id"
add_foreign_key "slots", "rooms", column: "section_id"
add_foreign_key "space_agreements", "spaces"
add_foreign_key "spaces", "rooms", column: "entrance_id"
end
5 changes: 5 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,8 @@
)
journal = FactoryBot.create(:journal, room: journal_section)
FactoryBot.create_list(:journal_entry, 7, :with_keywords, :published, journal:)

_content_block_section = FactoryBot.create(:room, space:, name: "Content Block-o-Clock",
description: "Content Blocks show static Words, Photos, or Videos!")

# FactoryBot.create(:content_block, in_section: content_block_section)
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
6 changes: 6 additions & 0 deletions spec/models/slot_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "rails_helper"

RSpec.describe Slot do
it { is_expected.to belong_to(:section).class_name(:Room).inverse_of(:slots) }
it { is_expected.to belong_to(:slottable).inverse_of(:slot) }
end
23 changes: 23 additions & 0 deletions spec/system/slots_system_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require "rails_helper"

RSpec.describe "Slots" do
include ActionText::SystemTestHelper

let(:space) { create(:space, :with_members) }
let(:section) { create(:room, space:) }

scenario "Adding a Content Block to a Slot" do
sign_in(space.members.first, space)
visit(polymorphic_path(section.location(:edit)))

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

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

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