Skip to content

Commit

Permalink
Marketplace: Products Controller persists Products!
Browse files Browse the repository at this point in the history
This starts to shift our Furniture and Item system away from an
inheritance oriented rails-adjacent structure to a more Rails-forward
structure.

Furniture now places it's routes against the *room*, which allows it to
support either Singleton-type Resource routes, or more traditional
Resources routes.

Further, it brings the `Placeable` interface a bit closer to
ActiveRecord; which should reduce pain-points as we unwind the current
overly-restrictive and complex Furniture/Item design.
  • Loading branch information
zspencer committed Oct 1, 2022
1 parent cf38d49 commit 714bd48
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/furniture/breakout_tables_by_jitsi.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class BreakoutTablesByJitsi
def self.append_routes(router)
def self.deprecated_append_routes(router)
router.resources :breakout_tables_by_jitsi, only: [:show], controller: 'breakout_tables_by_jitsi_by_jitsi/'
end
include Placeable
Expand Down
10 changes: 9 additions & 1 deletion app/furniture/furniture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ module Furniture
spotlight: Spotlight,
}.freeze

# Appends each {Furniture}'s CRUD actions
# Appends each {Furniture}'s CRUD actions under a FurniturePlacement
# @deprecated
def self.deprecated_append_routes(routing_context)
REGISTRY.each_value do |furniture|
furniture.deprecated_append_routes(routing_context) if furniture.respond_to?(:deprecated_append_routes)
end
end

# Appends each Furnitures CRUD actions within the {Room}
def self.append_routes(routing_context)
REGISTRY.each_value do |furniture|
furniture.append_routes(routing_context) if furniture.respond_to?(:append_routes)
Expand Down
4 changes: 4 additions & 0 deletions app/furniture/marketplace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ def self.append_routes(router)
router.resources :products, only: %i[create index], module: "marketplace"
end
end

def products
Marketplace::Product.where(space: space)
end
end
1 change: 1 addition & 0 deletions app/furniture/marketplace/product.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Marketplace
class Product < ApplicationRecord
self.table_name = 'marketplace_products'
belongs_to :space
end
end
8 changes: 8 additions & 0 deletions app/furniture/marketplace/product_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true
class Marketplace
class ProductPolicy < ApplicationPolicy
def permitted_attributes(_params)
%i[name description price_cents price_currency]
end
end
end
14 changes: 12 additions & 2 deletions app/furniture/marketplace/products_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
class Marketplace
class ProductsController < ApplicationController
class ProductsController < FurnitureController
def create
product = marketplace.products.new(product_params)
product.save!
end

def create ; end
def marketplace
Marketplace.find_by(room: room)
end

def product_params
policy(Marketplace::Product).permit(params.require(:product))
end
end
end
2 changes: 1 addition & 1 deletion app/furniture/payment_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class PaymentForm
include Placeable

def self.append_routes(router)
def self.deprecated_append_routes(router)
router.scope module: 'payment_form' do
router.resource :payment_form, only: [:show] do
router.resources :payments, only: %i[create index]
Expand Down
17 changes: 14 additions & 3 deletions app/furniture/placeable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,34 @@ module Placeable
# @return {FurniturePlacement}
attr_accessor :placement

delegate :id, :room, :utilities, :persisted?, :save!, to: :placement
delegate :id, :room, :space, :utilities, :persisted?, :save!, to: :placement

def self.included(placeable)
placeable.include ActiveModel::Model
placeable.include ActiveModel::Attributes
placeable.include ActiveModel::AttributeAssignment
placeable.extend ClassMethods
end

module ClassMethods
def find_by(room:)
room.furniture_placements.find_by(furniture_kind: furniture_kind).furniture
end

def furniture_kind
name.demodulize.underscore
end
end

def settings
placement.settings
end

def in_room_template
"#{self.class.name.demodulize.underscore}/in_room"
"#{self.class.furniture_kind}/in_room"
end

def form_template
"#{self.class.name.demodulize.underscore}/form"
"#{self.class.furniture_kind}/form"
end
end
2 changes: 1 addition & 1 deletion app/furniture/spotlight.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class Spotlight
include Placeable

def self.append_routes(router)
def self.deprecated_append_routes(router)
router.scope module: 'spotlight' do
router.resource :spotlight, only: [:show] do
router.resources :images, only: %i[create edit update]
Expand Down
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
end

resources :rooms, only: %i[show edit update new create destroy] do
Furniture.append_routes(self)
resource :waiting_room, only: %i[show update]
resources :furniture_placements, only: %i[create edit update destroy]
resource :furniture, only: [] do
Furniture.append_routes(self)
Furniture.deprecated_append_routes(self)
end
end

Expand Down
18 changes: 18 additions & 0 deletions spec/factories/furniture/marketplace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FactoryBot.define do
factory :marketplace do
transient do
room { build(:room) }
end

placement do
association :furniture_placement, { furniture_kind: 'marketplace', room: room }
end
end

factory :marketplace_product, class: 'Marketplace::Product' do
name { Faker::TvShows::DrWho.specie }
price_cents { Random.rand(1_00..9999_99) }

association :space
end
end
23 changes: 23 additions & 0 deletions spec/furniture/marketplace/products_request_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'rails_helper'

RSpec.describe Marketplace::ProductsController, type: :request do
let(:marketplace) { create(:marketplace)}
let(:space) { marketplace.space }
let(:room) { marketplace.room }
describe "POST /products" do

it "Creates a Product in the Marketplace" do
attributes = attributes_for(:marketplace_product)

expect do
post polymorphic_path([space, room, marketplace, :products]), params: { product: attributes }
end.to change(marketplace.products, :count).by(1)

created_product = marketplace.products.last
expect(created_product.name).to eql(attributes[:name])
expect(created_product.description).to eql(attributes[:description])
expect(created_product.price_cents).to eql(attributes[:price_cents])
expect(created_product.price_currency).to eql(Money.default_currency.to_s)
end
end
end

0 comments on commit 714bd48

Please sign in to comment.