Skip to content

Commit

Permalink
Merge pull request #5674 from nebulab/spaghetticode/admin-new-tax-cat…
Browse files Browse the repository at this point in the history
…egory

[Admin] Create new Tax Categories
  • Loading branch information
spaghetticode authored Mar 11, 2024
2 parents 75d3196 + 5584f93 commit 9c03da2
Show file tree
Hide file tree
Showing 16 changed files with 173 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@ def search_url
solidus_admin.tax_categories_path
end

def actions
def page_actions
render component("ui/button").new(
tag: :a,
text: t('.add'),
href: spree.new_admin_tax_category_path,
href: solidus_admin.new_tax_category_path, data: { turbo_frame: :new_tax_category_modal },
icon: "add-line",
class: "align-self-end w-full",
)
end

def turbo_frames
%w[new_tax_category_modal]
end

def search_key
:name_or_description_cont
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<%= turbo_frame_tag :new_tax_category_modal do %>
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
<%= form_for @tax_category, url: solidus_admin.tax_categories_path(page: params[:page], q: params[:q]), html: { id: form_id } do |f| %>
<div class="flex flex-col gap-6 pb-4">
<%= render component("ui/forms/field").text_field(f, :name) %>
<%= render component("ui/forms/field").text_field(f, :tax_code) %>
<%= render component("ui/forms/field").text_field(f, :description) %>
<label class="flex gap-2 items-center">
<%= render component("ui/forms/checkbox").new(
name: "#{f.object_name}[is_default]",
value: "1",
checked: f.object.is_default
) %>
<span class="font-semibold text-xs ml-2"><%= Spree::TaxCategory.human_attribute_name :is_default %></span>
<%= render component("ui/toggletip").new(text: t(".hints.is_default")) %>
</label>
</div>
<% modal.with_actions do %>
<form method="dialog">
<%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %>
</form>
<%= render component("ui/button").new(form: form_id, type: :submit, text: t('.submit')) %>
<% end %>
<% end %>
<% end %>
<% end %>
<%= render component("tax_categories/index").new(page: @page) %>
12 changes: 12 additions & 0 deletions admin/app/components/solidus_admin/tax_categories/new/component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class SolidusAdmin::TaxCategories::New::Component < SolidusAdmin::TaxCategories::Index::Component
def initialize(page:, tax_category:)
@page = page
@tax_category = tax_category
end

def form_id
dom_id(@tax_category, "#{stimulus_id}_new_tax_category_form")
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Add your component translations here.
# Use the translation in the example in your template with `t(".hello")`.
en:
title: "New Tax Category"
cancel: "Cancel"
submit: "Add Tax Category"
hints:
is_default: "When checked, this tax category will be selected by default when creating new products or variants."
14 changes: 7 additions & 7 deletions admin/app/components/solidus_admin/ui/modal/component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
<h3 class="text-xl font-semibold text-gray-900">
<%= @title %>
</h3>
<%= render component('ui/button').new(
tag: :a,
href: @close_path,
icon: 'close-line',
scheme: :ghost,
title: t('.close'),
) %>
<form method="dialog">
<%= render component('ui/button').new(
icon: 'close-line',
scheme: :ghost,
title: t('.close'),
) %>
</form>
</header>

<div class="p-4 overflow-auto">
Expand Down
7 changes: 7 additions & 0 deletions admin/app/components/solidus_admin/ui/modal/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Controller } from "@hotwired/stimulus";

export default class extends Controller {
connect() {
this.element.showModal();
}
}
2 changes: 1 addition & 1 deletion admin/app/components/solidus_admin/ui/modal/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class SolidusAdmin::UI::Modal::Component < SolidusAdmin::BaseComponent
renders_one :actions

def initialize(title:, close_path: nil, open: true, **attributes)
def initialize(title:, close_path: nil, open: false, **attributes)
@title = title
@close_path = close_path
@attributes = attributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@
<% end %>
<% end %>
<% end %>
<% turbo_frames.each do |frame| %>
<%= turbo_frame_tag frame %>
<% end %>
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,8 @@ def render_sidebar

page_with_sidebar_aside { sidebar } if sidebar
end

def turbo_frames
[]
end
end
57 changes: 50 additions & 7 deletions admin/app/controllers/solidus_admin/tax_categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,47 @@ module SolidusAdmin
class TaxCategoriesController < SolidusAdmin::BaseController
include SolidusAdmin::ControllerHelpers::Search

def index
tax_categories = apply_search_to(
Spree::TaxCategory.order(created_at: :desc, id: :desc),
param: :q,
)
def new
@tax_category = Spree::TaxCategory.new

set_page_and_extract_portion_from(tax_categories)
set_index_page

respond_to do |format|
format.html { render component('tax_categories/new').new(page: @page, tax_category: @tax_category) }
end
end

def create
@tax_category = Spree::TaxCategory.new(tax_category_params)

if @tax_category.save
respond_to do |format|
flash[:notice] = t('.success')

format.html do
redirect_to solidus_admin.tax_categories_path, status: :see_other
end

format.turbo_stream do
# we need to explicitly write the refresh tag for now.
# See https://github.com/hotwired/turbo-rails/issues/579
render turbo_stream: '<turbo-stream action="refresh" />'
end
end
else
set_index_page

respond_to do |format|
format.html do
page_component = component('tax_categories/new').new(page: @page, tax_category: @tax_category)
render page_component, status: :unprocessable_entity
end
end
end
end

def index
set_index_page

respond_to do |format|
format.html { render component('tax_categories/index').new(page: @page) }
Expand All @@ -34,7 +68,16 @@ def load_tax_category
end

def tax_category_params
params.require(:tax_category).permit(:tax_category_id, permitted_tax_category_attributes)
params.require(:tax_category).permit(:name, :description, :is_default, :tax_code)
end

def set_index_page
tax_categories = apply_search_to(
Spree::TaxCategory.order(created_at: :desc, id: :desc),
param: :q,
)

set_page_and_extract_portion_from(tax_categories)
end
end
end
1 change: 1 addition & 0 deletions admin/app/views/layouts/solidus_admin/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<%= stylesheet_link_tag SolidusAdmin::Config.theme_path(session[:admin_light_theme]), media: '(prefers-color-scheme: light)', "data-turbo-track": "reload" %>
<%= stylesheet_link_tag SolidusAdmin::Config.theme_path(session[:admin_dark_theme]), media: '(prefers-color-scheme: dark)', "data-turbo-track": "reload" %>
<%= javascript_importmap_tags "solidus_admin/application", shim: false, importmap: SolidusAdmin.importmap %>
<meta name="turbo-refresh-scroll", content="preserve">
</head>

<body class="bg-gray-15 font-sans">
Expand Down
2 changes: 2 additions & 0 deletions admin/config/locales/tax_categories.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ en:
title: "Tax Categories"
destroy:
success: "Tax categories were successfully removed."
create:
success: "Tax category was successfully created."
2 changes: 1 addition & 1 deletion admin/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
admin_resources :option_types, only: [:index, :destroy], sortable: true
admin_resources :taxonomies, only: [:index, :destroy], sortable: true
admin_resources :promotion_categories, only: [:index, :destroy]
admin_resources :tax_categories, only: [:index, :destroy]
admin_resources :tax_categories, only: [:new, :index, :create, :destroy]
admin_resources :tax_rates, only: [:index, :destroy]
admin_resources :payment_methods, only: [:index, :destroy], sortable: true
admin_resources :stock_items, only: [:index, :edit, :update]
Expand Down
2 changes: 1 addition & 1 deletion admin/solidus_admin.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ Gem::Specification.new do |s|
s.add_dependency 'solidus_backend'
s.add_dependency 'solidus_core', '> 4.2'
s.add_dependency 'stimulus-rails', '~> 1.2'
s.add_dependency 'turbo-rails', '~> 1.4'
s.add_dependency 'turbo-rails', '~> 2.0'
s.add_dependency 'view_component', '~> 3.9'
end
39 changes: 39 additions & 0 deletions admin/spec/features/tax_categories_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,43 @@
expect(Spree::TaxCategory.count).to eq(1)
expect(page).to be_axe_clean
end

context "when creating a new tax category" do
let(:query) { "?page=1&q%5Bname_or_description_cont%5D=Cloth" }

before do
visit "/admin/tax_categories#{query}"
click_on "Add new"
expect(page).to have_content("New Tax Category")
expect(page).to be_axe_clean
end

it "opens a modal" do
expect(page).to have_selector("dialog")
within("dialog") { click_on "Cancel" }
expect(page).not_to have_selector("dialog")
expect(page.current_url).to include(query)
end

context "with valid data" do
it "successfully creates a new tax category, keeping page and q params" do
fill_in "Name", with: "Clothing"

click_on "Add Tax Category"

expect(page).to have_content("Tax category was successfully created.")
expect(Spree::TaxCategory.find_by(name: "Clothing")).to be_present
expect(page.current_url).to include(query)
end
end

context "with invalid data" do
it "fails to create a new tax category, keeping page and q params" do
click_on "Add Tax Category"

expect(page).to have_content "can't be blank"
expect(page.current_url).to include(query)
end
end
end
end
2 changes: 2 additions & 0 deletions core/app/models/spree/tax_category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Spree
class TaxCategory < Spree::Base
include Spree::SoftDeletable

self.allowed_ransackable_attributes = %w[name description]

after_discard do
self.tax_rate_tax_categories = []
end
Expand Down

0 comments on commit 9c03da2

Please sign in to comment.