Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] main from solidusio:main #402

Merged
merged 18 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 1 addition & 4 deletions api/lib/spree/api_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ class ApiConfiguration < Preferences::Configuration
:variant_id
]

preference :promotion_attributes, :array, default: [
:id, :name, :description, :expires_at, :starts_at, :type, :usage_limit,
:advertise, :path
]
preference :promotion_attributes, :array, default: Spree::Config.promotions.promotion_api_attributes

preference :store_attributes, :array, default: [
:id, :name, :url, :meta_description, :meta_keywords, :seo_title,
Expand Down
18 changes: 2 additions & 16 deletions api/spec/features/checkout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ module Spree
before do
stub_spree_preferences(Spree::Api::Config, requires_authentication: false)
end
let!(:promotion) { FactoryBot.create(:promotion, :with_order_adjustment, code: 'foo', weighted_order_adjustment_amount: 10) }
let(:promotion_code) { promotion.codes.first }
let!(:store) { FactoryBot.create(:store) }
let(:bill_address) { FactoryBot.create(:address) }
let(:ship_address) { FactoryBot.create(:address) }
Expand Down Expand Up @@ -62,14 +60,6 @@ def create_line_item(variant, quantity = 1)
expect(response).to have_http_status(:created)
end

def add_promotion(_promotion)
expect {
post "/api/orders/#{@order.number}/coupon_codes",
params: { coupon_code: promotion_code.value }
}.to change { @order.promotions.count }.by 1
expect(response).to have_http_status(:ok)
end

def add_address(address, billing: true)
address_type = billing ? :bill_address : :ship_address
# It seems we are missing an order-scoped address api endpoint since we need
Expand Down Expand Up @@ -103,24 +93,22 @@ def assert_order_expectations
expect(@order.state).to eq 'complete'
expect(@order.completed_at).to be_a ActiveSupport::TimeWithZone
expect(@order.item_total).to eq 600.00
expect(@order.total).to eq 600.00
expect(@order.adjustment_total).to eq(-10.00)
expect(@order.total).to eq 610.00
expect(@order.adjustment_total).to eq(0)
expect(@order.shipment_total).to eq 10.00
expect(@order.user).to eq @user
expect(@order.bill_address).to eq bill_address
expect(@order.ship_address).to eq ship_address
expect(@order.payments.length).to eq 1
expect(@order.line_items.any? { |li| li.variant == variant_1 && li.quantity == 2 }).to eq true
expect(@order.line_items.any? { |li| li.variant == variant_2 && li.quantity == 2 }).to eq true
expect(@order.promotions).to eq [promotion]
end

it "is able to checkout with individualized requests" do
login
create_order

create_line_item(variant_1, 2)
add_promotion(promotion)
create_line_item(variant_2, 2)

add_address(bill_address)
Expand Down Expand Up @@ -152,7 +140,6 @@ def assert_order_expectations
}
})

add_promotion(promotion)
add_payment

advance
Expand Down Expand Up @@ -180,7 +167,6 @@ def assert_order_expectations
}
})

add_promotion(promotion)
add_payment

advance
Expand Down
Loading
Loading