Skip to content

Commit

Permalink
Merge pull request #5697 from nebulab/spaghetticode/update-tax-category
Browse files Browse the repository at this point in the history
[Admin] Add Update Tax Category feature
  • Loading branch information
tvdeyen authored Apr 12, 2024
2 parents 9fb0abb + f38fbd6 commit cb0ff1e
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<%= turbo_frame_tag :edit_tax_category_modal do %>
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
<%= form_for @tax_category, url: solidus_admin.tax_category_path(@tax_category), 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) %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class SolidusAdmin::TaxCategories::Edit::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}_edit_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: "Edit Tax Category"
cancel: "Cancel"
submit: "Update Tax Category"
hints:
is_default: "When checked, this tax category will be selected by default when creating new products or variants."
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class SolidusAdmin::TaxCategories::Index::Component < SolidusAdmin::Taxes::Component
def row_url(tax_category)
spree.edit_admin_tax_category_path(tax_category)
spree.edit_admin_tax_category_path(tax_category, _turbo_frame: :edit_tax_category_modal)
end

def model_class
Expand All @@ -24,7 +24,10 @@ def page_actions
end

def turbo_frames
%w[new_tax_category_modal]
%w[
new_tax_category_modal
edit_tax_category_modal
]
end

def search_key
Expand Down
41 changes: 41 additions & 0 deletions admin/app/controllers/solidus_admin/tax_categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module SolidusAdmin
class TaxCategoriesController < SolidusAdmin::BaseController
include SolidusAdmin::ControllerHelpers::Search

before_action :find_tax_category, only: %i[edit update]

def new
@tax_category = Spree::TaxCategory.new

Expand All @@ -14,6 +16,16 @@ def new
end
end

def edit
@tax_category = Spree::TaxCategory.find(params[:id])

set_index_page

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

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

Expand Down Expand Up @@ -43,6 +55,31 @@ def create
end
end

def update
if @tax_category.update(tax_category_params)
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
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/edit').new(page: @page, tax_category: @tax_category)
render page_component, status: :unprocessable_entity
end
end
end
end

def index
set_index_page

Expand All @@ -67,6 +104,10 @@ def load_tax_category
authorize! action_name, @tax_category
end

def find_tax_category
@tax_category = Spree::TaxCategory.find(params[:id])
end

def tax_category_params
params.require(:tax_category).permit(:name, :description, :is_default, :tax_code)
end
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 @@ -6,3 +6,5 @@ en:
success: "Tax categories were successfully removed."
create:
success: "Tax category was successfully created."
update:
success: "Tax category was successfully updated."
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: [:new, :index, :create, :destroy]
admin_resources :tax_categories, except: [:show]
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

0 comments on commit cb0ff1e

Please sign in to comment.