Skip to content

Commit

Permalink
Marketplace: Initial CRUD for TaxRate
Browse files Browse the repository at this point in the history
- #1137

This throws together the basic `CRUD` operations for `TaxRate`. Going to
do a bit of tidying and add some tests, then record a video of the use
case.

I may decide I want to add in the setting of the `TaxRate` on the
`Product`, but maybe I'll wait and add that independently...
  • Loading branch information
zspencer committed Mar 5, 2023
1 parent e02407f commit 7ad14d8
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ApplicationController < ActionController::Base
before_action :ensure_on_byo_domain

include Pundit::Authorization
after_action :verify_authorized
after_action :verify_authorized, except: [:index] # rubocop:disable Rails/LexicallyScopedActionFilter
after_action :verify_policy_scoped
before_action :prepend_theme_views

Expand Down
10 changes: 10 additions & 0 deletions app/furniture/marketplace/breadcrumbs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,13 @@
parent :marketplace_product, product
link "Edit", product.location(:edit)
end

crumb :new_tax_rate do |tax_rate|
parent :edit_marketplace, tax_rate.marketplace
link "Add a Tax Rate", marketplace.location(:new, child: :tax_rate)
end

crumb :edit_tax_rate do |tax_rate|
parent :edit_marketplace, tax_rate.marketplace
link "Edit Tax Rate '#{tax_rate.label}'", marketplace.location(:new, child: :tax_rate)
end
2 changes: 2 additions & 0 deletions app/furniture/marketplace/marketplace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Marketplace < FurniturePlacement
has_many :carts, inverse_of: :marketplace, dependent: :destroy
has_many :orders, inverse_of: :marketplace

has_many :tax_rates, inverse_of: :marketplace

# The Secret Stripe API key belonging to the owner of the Marketplace
def stripe_api_key
space.utility_hookups.find_by!(utility_slug: :stripe).utility.api_token
Expand Down
8 changes: 8 additions & 0 deletions app/furniture/marketplace/marketplaces/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@
<%= button_to t('marketplace.product.index'),
marketplace.location(child: :products), method: :get, data: { turbo: false } %>
<%- end %>

<h3>Tax Rates</h3>
<%= render policy_scope(marketplace.tax_rates) %>


<%- if policy(marketplace.tax_rates.new).create? %>
<%= link_to("Add Tax Rate", marketplace.location(:new, child: :tax_rate)) %>
<%- end %>
6 changes: 5 additions & 1 deletion app/furniture/marketplace/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ def self.append_routes(router)
router.resource :checkout, only: [:show, :create]
end

router.resources :tax_rates

router.resources :orders, only: [:show]
router.resources :products
router.resources :products do
router.resources :tax_rates, only: [:create, :destroy, :index, :show]
end
router.resource :stripe_account
end
end
Expand Down
7 changes: 7 additions & 0 deletions app/furniture/marketplace/tax_rate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Marketplace
class TaxRate < Record
self.table_name = "marketplace_tax_rates"
belongs_to :marketplace
self.location_parent = :marketplace
end
end
21 changes: 21 additions & 0 deletions app/furniture/marketplace/tax_rate_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Marketplace
class TaxRatePolicy < ApplicationPolicy
alias_method :tax_rate, :object

def create?
person.member_of?(tax_rate.marketplace.space)
end

alias_method :update?, :create?

def permitted_attributes(_)
[:label, :tax_rate]
end

class Scope < ApplicationScope
def resolve
scope.all
end
end
end
end
6 changes: 6 additions & 0 deletions app/furniture/marketplace/tax_rates/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<%= form_with(model: tax_rate.location) do |tax_rate_form| %>
<%= render "text_field", attribute: :label, form: tax_rate_form %>
<%= render "number_field", attribute: :tax_rate, form: tax_rate_form, required: true, step: 1, min: 0, max: 100 %>

<%= tax_rate_form.submit %>
<%- end %>
3 changes: 3 additions & 0 deletions app/furniture/marketplace/tax_rates/_tax_rate.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= link_to tax_rate.location(:edit) do %>
<%= tax_rate.label %>: <%= number_to_percentage(tax_rate.tax_rate, precision: 0) %>
<%- end %>
2 changes: 2 additions & 0 deletions app/furniture/marketplace/tax_rates/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%- breadcrumb :edit_tax_rate, tax_rate %>
<%= render "form", tax_rate: tax_rate %>
2 changes: 2 additions & 0 deletions app/furniture/marketplace/tax_rates/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%- breadcrumb :new_tax_rate, tax_rate %>
<%= render "form", tax_rate: tax_rate %>
31 changes: 31 additions & 0 deletions app/furniture/marketplace/tax_rates_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Marketplace
class TaxRatesController < Controller
def new
tax_rate
end

def create
if tax_rate.save
redirect_to marketplace.location(child: :tax_rates)
else
render :new
end
end

def tax_rate_params
policy(TaxRate).permit(params.require(:tax_rate))
end

helper_method def tax_rate
@tax_rate ||= if params[:id]
policy_scope(marketplace.tax_rates).find(params[:id])
elsif params[:tax_rate]
marketplace.tax_rates.new(tax_rate_params)
else
marketplace.tax_rates.new
end.tap do |tax_rate|
authorize(tax_rate)
end
end
end
end
7 changes: 5 additions & 2 deletions app/views/application/_number_field.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<% required ||= required | false %>
<% required = local_assigns[:required] || false %>
<% min = local_assigns[:min] %>
<% max = local_assigns[:max] %>
<% step = local_assigns[:step] %>
<div>
<%= form.label attribute %>
<%= form.number_field attribute, required: required %>
<%= form.number_field attribute, required: required, min: min, max: max, step: step %>
<%= render partial: "error", locals: { model: form.object, attribute: attribute } %>
</div>
14 changes: 14 additions & 0 deletions db/migrate/20230305005417_create_marketplace_tax_rates.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateMarketplaceTaxRates < ActiveRecord::Migration[7.0]
def change
create_table :marketplace_tax_rates, id: :uuid do |t|
t.integer :tax_rate
t.string :label
t.references :marketplace, type: :uuid, foreign_key: {to_table: :furniture_placements}
t.timestamps
end

change_table :marketplace_products do |t|
t.references :tax_rate, type: :uuid, foreign_key: {to_table: :marketplace_tax_rates}
end
end
end
19 changes: 16 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_03_04_003325) do
ActiveRecord::Schema[7.0].define(version: 2023_03_05_005417) 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 @@ -148,7 +148,9 @@
t.string "price_currency", default: "USD", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.uuid "tax_rate_id"
t.index ["marketplace_id"], name: "index_marketplace_products_on_marketplace_id"
t.index ["tax_rate_id"], name: "index_marketplace_products_on_tax_rate_id"
end

create_table "marketplace_shoppers", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
Expand All @@ -158,6 +160,15 @@
t.index ["person_id"], name: "index_marketplace_shoppers_on_person_id", unique: true
end

create_table "marketplace_tax_rates", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.integer "tax_rate"
t.string "label"
t.uuid "marketplace_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["marketplace_id"], name: "index_marketplace_tax_rates_on_marketplace_id"
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 @@ -222,7 +233,9 @@
add_foreign_key "marketplace_cart_products", "marketplace_products", column: "product_id"
add_foreign_key "marketplace_orders", "marketplace_shoppers", column: "shopper_id"
add_foreign_key "marketplace_products", "furniture_placements", column: "marketplace_id"
add_foreign_key "marketplace_products", "marketplace_tax_rates", column: "tax_rate_id"
add_foreign_key "marketplace_shoppers", "people"
add_foreign_key "marketplace_tax_rates", "furniture_placements", column: "marketplace_id"
add_foreign_key "memberships", "invitations"
add_foreign_key "spaces", "rooms", column: "entrance_id"
end

0 comments on commit 7ad14d8

Please sign in to comment.