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

Pre-select current tax category on product form #3936

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
5 changes: 5 additions & 0 deletions backend/app/controllers/spree/admin/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class ProductsController < ResourceController
helper_method :clone_object_url
before_action :split_params, only: [:create, :update]
before_action :normalize_variant_property_rules, only: [:update]
before_action :set_default_tax_category, only: [:new, :edit]

def show
redirect_to action: :edit
Expand Down Expand Up @@ -46,6 +47,10 @@ def clone

private

def set_default_tax_category
@product.tax_category_id ||= @default_tax_category&.id
end

def split_params
if params[:product][:taxon_ids].present?
params[:product][:taxon_ids] = params[:product][:taxon_ids].split(',')
Expand Down
2 changes: 1 addition & 1 deletion backend/app/views/spree/admin/products/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
<%= f.field_container :tax_category do %>
<%= f.label :tax_category_id, Spree::TaxCategory.model_name.human %>
<%= f.field_hint :tax_category, default_tax_category: @default_tax_category&.name %>
<%= f.collection_select(:tax_category_id, @tax_categories, :id, :name, { include_blank: t('spree.match_choices.none'), selected: @default_tax_category&.id }, { class: 'custom-select' }) %>
<%= f.collection_select(:tax_category_id, @tax_categories, :id, :name, { include_blank: t('spree.match_choices.none') }, { class: 'custom-select' }) %>
<%= f.error_message_on :tax_category %>
<% end %>
</div>
Expand Down
28 changes: 25 additions & 3 deletions backend/spec/features/admin/products/products_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -277,24 +277,46 @@ def build_option_type_with_values(name, values)
end
end

context 'updating a product', js: true do
context 'updating a product' do
let(:product) { create(:product) }

it 'should parse correctly available_on' do
it 'should parse correctly available_on', :js do
visit spree.admin_product_path(product)
fill_in "product_available_on", with: "2012/12/25"
click_button "Update"
expect(page).to have_content("successfully updated!")
expect(Spree::Product.last.available_on).to eq('Tue, 25 Dec 2012 00:00:00 UTC +00:00')
end

it 'should correctly update discontinue_on' do
it 'should correctly update discontinue_on', :js do
visit spree.admin_product_path(product)
fill_in "product_discontinue_on", with: "2020/12/4"
click_button "Update"
expect(page).to have_content("successfully updated!")
expect(product.reload.discontinue_on.to_s).to eq('2020-12-04 00:00:00 UTC')
end

context "when there is a default tax category" do
let!(:default_category) { create(:tax_category, name: 'Alcohol taxes', is_default: true) }

context "when the product does not have a tax category" do
it "pre-selects the default tax category" do
visit spree.admin_product_path(product)
expect(page).to have_select('product_tax_category_id', selected: default_category.name)
end
end

context "when the product already has a tax category" do
let(:clothing) { create(:tax_category, name: 'Clothing', is_default: false) }

before { product.update!(tax_category: clothing) }

it "pre-selects the product tax category" do
visit spree.admin_product_path(product)
expect(page).to have_select('product_tax_category_id', selected: clothing.name)
end
end
end
end

context 'deleting a product', js: true do
Expand Down