-
-
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.
- #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... * `Marketplace`: Test Create and Update `TaxRate` * 🧹 `Marketplace`: `TaxRate#tax_rate` is a float! Like a 🚢 I think mayyybeeeeeeee it's better to support decimals. * 🧹🛠️ `Marketplace`: Model `Product`<=>`TaxRate` relationship This also addresses Ana's comments re: Tidying
- Loading branch information
Showing
18 changed files
with
185 additions
and
6 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
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,9 @@ | ||
class Marketplace | ||
class TaxRate < Record | ||
self.table_name = "marketplace_tax_rates" | ||
self.location_parent = :marketplace | ||
|
||
belongs_to :marketplace, inverse_of: :tax_rates | ||
has_many :tax_rates, inverse_of: :tax_rates | ||
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,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 |
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,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: 0.1, min: 0, max: 100 %> | ||
|
||
<%= tax_rate_form.submit %> | ||
<%- 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,3 @@ | ||
<%= link_to tax_rate.location(:edit) do %> | ||
<%= tax_rate.label %>: <%= number_to_percentage(tax_rate.tax_rate, precision: 1) %> | ||
<%- 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,2 @@ | ||
<%- breadcrumb :edit_tax_rate, tax_rate %> | ||
<%= render "form", tax_rate: tax_rate %> |
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,2 @@ | ||
<%- breadcrumb :new_tax_rate, tax_rate %> | ||
<%= render "form", tax_rate: tax_rate %> |
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,39 @@ | ||
class Marketplace | ||
class TaxRatesController < Controller | ||
def new | ||
tax_rate | ||
end | ||
|
||
def create | ||
if tax_rate.save | ||
redirect_to marketplace.location(:edit) | ||
else | ||
render :new | ||
end | ||
end | ||
|
||
def update | ||
if tax_rate.update(tax_rate_params) | ||
redirect_to marketplace.location(:edit) | ||
else | ||
render :edit | ||
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 |
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,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> |
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,14 @@ | ||
class CreateMarketplaceTaxRates < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :marketplace_tax_rates, id: :uuid do |t| | ||
t.float :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 |
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
39 changes: 39 additions & 0 deletions
39
spec/furniture/marketplace/tax_rates_controller_request_spec.rb
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,39 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Marketplace::TaxRatesController, type: :request do | ||
subject(:result) do | ||
perform_request | ||
test_response | ||
end | ||
|
||
before { sign_in(space, person) } | ||
|
||
let(:space) { marketplace.space } | ||
let(:marketplace) { create(:marketplace) } | ||
let(:person) { create(:membership, space: space).member } | ||
|
||
describe "#new" do | ||
let(:perform_request) { get polymorphic_path(marketplace.location(:new, child: :tax_rate)) } | ||
|
||
it { is_expected.to be_ok } | ||
end | ||
|
||
describe "#create" do | ||
let(:perform_request) do | ||
post polymorphic_path(marketplace.location(child: :tax_rates)), params: {tax_rate: attributes_for(:marketplace_tax_rate)} | ||
end | ||
|
||
it { is_expected.to redirect_to(marketplace.location(:edit)) } | ||
end | ||
|
||
describe "#update" do | ||
let(:tax_rate) { create(:marketplace_tax_rate, marketplace: marketplace) } | ||
let(:perform_request) do | ||
put polymorphic_path(tax_rate.location), params: {tax_rate: {label: "Hey", tax_rate: 23}} | ||
end | ||
|
||
it { is_expected.to redirect_to(marketplace.location(:edit)) } | ||
specify { expect { result }.to change { tax_rate.reload.label }.to("Hey") } | ||
specify { expect { result }.to change { tax_rate.reload.tax_rate }.to(23) } | ||
end | ||
end |