-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Marketplace: Products Controller persists Products!
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
Showing
12 changed files
with
94 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |