Skip to content

Commit

Permalink
Marketplace: TaxRates connect to Bazaar
Browse files Browse the repository at this point in the history
- #1137

OK this is a bit larger than I wanted it to be, but it should be a
purely structural change.

- Creates a `Marketplace::Bazaar` class to attach the `TaxRate`
- Migrates `TaxRate` connection to the `Bazaar` on migration
- Maintains the wiring of the `TaxRate` to the `Marketplace`
  • Loading branch information
zspencer committed May 14, 2023
1 parent 2b85da5 commit 2fbb5c7
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 11 deletions.
12 changes: 12 additions & 0 deletions app/furniture/marketplace/bazaar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Marketplace
# Serves as a connection point for cross-marketplace functionality, like
# {TaxRate} and {Shopper}.
class Bazaar < ::Space
has_many :marketplaces, through: :rooms, source: :furnitures, inverse_of: :bazaar, class_name: "Marketplace"
has_many :tax_rates, inverse_of: :bazaar, dependent: :destroy

def space
becomes(Space)
end
end
end
3 changes: 3 additions & 0 deletions app/furniture/marketplace/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ class Controller < FurnitureController
@marketplace ||= policy_scope(Marketplace).find(params[:marketplace_id])
end

delegate :bazaar, to: :marketplace
helper_method :bazaar

helper_method def shopper
@shopper ||= if current_person.is_a?(Guest)
Shopper.find_or_create_by(id: session[:guest_shopper_id] ||= SecureRandom.uuid)
Expand Down
5 changes: 4 additions & 1 deletion app/furniture/marketplace/marketplace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ class Marketplace < Furniture
location(parent: :room)
default_scope { where(furniture_kind: "marketplace") }

has_one :space, through: :room, inverse_of: :furnitures
has_one :bazaar, through: :room, inverse_of: :furnitures, source: :space, class_name: "Bazaar"
has_many :tax_rates, inverse_of: :marketplace

has_many :products, inverse_of: :marketplace, dependent: :destroy
has_many :carts, inverse_of: :marketplace, dependent: :destroy
has_many :orders, inverse_of: :marketplace

has_many :tax_rates, inverse_of: :marketplace
has_many :delivery_areas, inverse_of: :marketplace, dependent: :destroy

setting :notify_emails
Expand Down
2 changes: 1 addition & 1 deletion app/furniture/marketplace/products/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<%= render "text_field", { attribute: :name, form: f} %>
<%= render "text_area", { attribute: :description, form: f} %>
<%= render "money_field", { attribute: :price, form: f, min: 0, step: 0.01} %>
<%= render "collection_check_boxes", { attribute: :tax_rate_ids, collection: marketplace.tax_rates, value_method: :id, text_method: :label, form: f} %>
<%= render "collection_check_boxes", { attribute: :tax_rate_ids, collection: bazaar.tax_rates, value_method: :id, text_method: :label, form: f} %>
<%- if product.photo.present? %>
<div>
<%= image_tag product.photo.variant(resize_to_limit: [150, 150]), class: "rounded-lg" %>
Expand Down
3 changes: 2 additions & 1 deletion app/furniture/marketplace/tax_rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ class TaxRate < Record
location(parent: :marketplace)

belongs_to :marketplace, inverse_of: :tax_rates
has_one :space, through: :marketplace
belongs_to :bazaar, inverse_of: :tax_rates
delegate :space, to: :bazaar

validates :tax_rate, numericality: {greater_than: 0, less_than_or_equal_to: 100}, presence: true
validates :label, presence: true
Expand Down
6 changes: 3 additions & 3 deletions app/furniture/marketplace/tax_rates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ def tax_rate_params
@tax_rate ||= if params[:id]
tax_rates.find(params[:id])
elsif params[:tax_rate]
tax_rates.new(tax_rate_params)
tax_rates.new(tax_rate_params.merge(marketplace: marketplace))
else
tax_rates.new
tax_rates.new(marketplace: marketplace)
end.tap do |tax_rate|
authorize(tax_rate)
end
end

helper_method def tax_rates
@tax_rates ||= policy_scope(marketplace.tax_rates)
@tax_rates ||= policy_scope(bazaar.tax_rates)
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class MarketplaceAddBazaarToTaxRate < ActiveRecord::Migration[7.0]
disable_ddl_transaction!
def change
add_reference :marketplace_tax_rates, :bazaar, type: :uuid, null: true, index: {algorithm: :concurrently}
end
end
8 changes: 5 additions & 3 deletions 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: 2023_04_27_003441) do
ActiveRecord::Schema[7.0].define(version: 2023_05_14_174823) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
Expand All @@ -22,12 +22,12 @@
"expired",
"ignored",
"revoked",
"sent",
"sent"
], force: :cascade

create_enum :membership_status, [
"active",
"revoked",
"revoked"
], force: :cascade

create_table "active_storage_attachments", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
Expand Down Expand Up @@ -190,6 +190,8 @@
t.uuid "marketplace_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.uuid "bazaar_id"
t.index ["bazaar_id"], name: "index_marketplace_tax_rates_on_bazaar_id"
t.index ["marketplace_id"], name: "index_marketplace_tax_rates_on_marketplace_id"
end

Expand Down
Binary file modified docs/erd.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions lib/tasks/release.rake
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,13 @@ namespace :release do
task after_build: [:environment, "db:prepare"] do
Lockbox.migrate(Marketplace::Order)
SystemTestSpace.prepare
Marketplace::TaxRate.all.find_each do |tax_rate|
if tax_rate.marketplace.blank?
Marketplace::ProductTaxRate.where(tax_rate: tax_rate).each(&:destroy!)
tax_rate.destroy!
next
end
tax_rate.update!(bazaar: tax_rate.marketplace.bazaar)
end
end
end
7 changes: 6 additions & 1 deletion spec/factories/furniture/marketplace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
end
end

factory :marketplace_bazaar, class: "Marketplace::Bazaar" do
sequence(:name) { |n| "The Market of #{Faker::Fantasy::Tolkien.location} #{n}" }
end

factory :marketplace_product, class: "Marketplace::Product" do
name { Faker::TvShows::DrWho.specie }
price_cents { Random.rand(1_00..999_99) }
Expand Down Expand Up @@ -160,9 +164,10 @@
end

factory :marketplace_tax_rate, class: "Marketplace::TaxRate" do
marketplace { association(:marketplace) }
bazaar { marketplace&.bazaar }
label { "#{Faker::TvShows::RuPaul.queen} Tax" }
tax_rate { (1..45).to_a.sample }
marketplace { association(:marketplace) }
end

factory :marketplace_delivery_area, class: "Marketplace::DeliveryArea" do
Expand Down
6 changes: 6 additions & 0 deletions spec/furniture/marketplace/bazaar_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "rails_helper"

RSpec.describe Marketplace::Bazaar, type: :model do
it { is_expected.to have_many(:marketplaces).through(:rooms).inverse_of(:bazaar) }
it { is_expected.to have_many(:tax_rates).inverse_of(:bazaar).dependent(:destroy) }
end
2 changes: 1 addition & 1 deletion spec/furniture/marketplace/tax_rate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

it { is_expected.to have_many(:product_tax_rates).inverse_of(:tax_rate) }
it { is_expected.to have_many(:products).through(:product_tax_rates).inverse_of(:tax_rates) }
it { is_expected.to have_one(:space).through(:marketplace) }
it { is_expected.to belong_to(:bazaar).inverse_of(:tax_rates) }
it { is_expected.to belong_to(:marketplace).inverse_of(:tax_rates) }
end
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
end

it { is_expected.to redirect_to(marketplace.location(child: :tax_rates)) }

specify do
expect { perform_request }.to change { marketplace.tax_rates.count }.by(1)
end
end

describe "#edit" do
Expand Down

0 comments on commit 2fbb5c7

Please sign in to comment.