Skip to content

Commit

Permalink
Marketplace: Cart attach to Shopper (#977)
Browse files Browse the repository at this point in the history
* attach at Marketplace_cart to a Person
* associate Cart to a Shopper

Co-authored-by: Zee Spencer <[email protected]>
Co-authored-by: Ana <[email protected]>

#831
  • Loading branch information
KellyAH authored Dec 8, 2022
1 parent def8759 commit 07cd651
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/furniture/marketplace/cart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Cart < ApplicationRecord
self.table_name = 'marketplace_carts'

belongs_to :marketplace, inverse_of: :carts
belongs_to :shopper, inverse_of: :carts
delegate :space, :room, to: :marketplace
has_many :cart_products, dependent: :destroy, inverse_of: :cart
has_many :products, through: :cart_products, inverse_of: :carts
Expand Down
10 changes: 9 additions & 1 deletion app/furniture/marketplace/marketplaces/_marketplace.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@

<%- cart = marketplace.carts.find_or_create_by(id: session[:current_cart] ||= SecureRandom.uuid) %>


<%- shopper = if current_person.is_a?(Guest)
Marketplace::Shopper.find_or_create_by(id: session[:current_cart] ||= SecureRandom.uuid)
else
Marketplace::Shopper.find_or_create_by(person: current_person)
end %>

<%- cart = marketplace.carts.find_or_create_by(shopper: shopper) %>

<%= render cart %>

Expand Down
10 changes: 10 additions & 0 deletions app/furniture/marketplace/shopper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class Marketplace
class Shopper < ApplicationRecord
self.table_name = 'marketplace_shoppers'

belongs_to :person, optional: true
has_many :carts, inverse_of: :shopper
end
end
12 changes: 12 additions & 0 deletions db/migrate/20221208013832_add_shopper_to_marketplace_carts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class AddShopperToMarketplaceCarts < ActiveRecord::Migration[7.0]
def change
create_table :marketplace_shoppers, id: :uuid do |t|
t.references(:person, type: :uuid, foreign_key: {to_table: :people}, index: {unique: true})
t.timestamps
end

change_table :marketplace_carts do |t|
t.references :shopper, type: :uuid, foreign_key: {to_table: :marketplace_shoppers}
end
end
end
13 changes: 12 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2022_10_13_010533) do
ActiveRecord::Schema[7.0].define(version: 2022_12_08_013832) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
Expand Down Expand Up @@ -148,7 +148,9 @@
t.uuid "marketplace_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.uuid "shopper_id"
t.index ["marketplace_id"], name: "index_marketplace_carts_on_marketplace_id"
t.index ["shopper_id"], name: "index_marketplace_carts_on_shopper_id"
end

create_table "marketplace_products", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
Expand All @@ -162,6 +164,13 @@
t.index ["marketplace_id"], name: "index_marketplace_products_on_marketplace_id"
end

create_table "marketplace_shoppers", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.uuid "person_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["person_id"], name: "index_marketplace_shoppers_on_person_id", unique: true
end

create_table "memberships", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.uuid "member_id"
t.uuid "space_id"
Expand Down Expand Up @@ -226,7 +235,9 @@
add_foreign_key "marketplace_cart_products", "marketplace_carts", column: "cart_id"
add_foreign_key "marketplace_cart_products", "marketplace_products", column: "product_id"
add_foreign_key "marketplace_carts", "furniture_placements", column: "marketplace_id"
add_foreign_key "marketplace_carts", "marketplace_shoppers", column: "shopper_id"
add_foreign_key "marketplace_products", "furniture_placements", column: "marketplace_id"
add_foreign_key "marketplace_shoppers", "people"
add_foreign_key "memberships", "invitations"
add_foreign_key "spaces", "rooms", column: "entrance_id"
end
5 changes: 5 additions & 0 deletions spec/factories/furniture/marketplace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@

factory :marketplace_cart, class: "Marketplace::Cart" do
marketplace
association(:shopper, factory: :marketplace_shopper)
end

factory :marketplace_shopper, class: "Marketplace::Shopper" do
end

end
2 changes: 2 additions & 0 deletions spec/furniture/marketplace/cart_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

it { is_expected.to belong_to(:marketplace).class_name("Marketplace::Marketplace").inverse_of(:carts) }

it { is_expected.to belong_to(:shopper).inverse_of(:carts) }

describe "#price_total" do
subject(:price_total) { cart.price_total }

Expand Down
7 changes: 7 additions & 0 deletions spec/furniture/marketplace/shopper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "rails_helper"

RSpec.describe Marketplace::Shopper, type: :model do
it { is_expected.to belong_to(:person).optional }

it { is_expected.to have_many(:carts).inverse_of(:shopper) }
end

0 comments on commit 07cd651

Please sign in to comment.