diff --git a/app/controllers/spree/admin/variants_controller.rb b/app/controllers/spree/admin/variants_controller.rb index 0936ffd155..0cda767181 100644 --- a/app/controllers/spree/admin/variants_controller.rb +++ b/app/controllers/spree/admin/variants_controller.rb @@ -4,14 +4,51 @@ module Spree module Admin class VariantsController < ResourceController helper 'spree/products' + include ProductFilterHelper + belongs_to 'spree/product', find_by: :permalink new_action.before :new_before + def index + @url_filters = product_filters(request.query_parameters) + end + + def edit + @url_filters = product_filters(request.query_parameters) + + super + end + + def update + @url_filter = product_filters(request.query_parameters) + + if @object.update(permitted_resource_params) + flash[:success] = flash_message_for(@object, :successfully_updated) + redirect_to admin_product_variants_url(params[:product_id], @url_filter) + else + redirect_to edit_admin_product_variant_url(params[:product_id], @object, @url_filter) + end + end + + def new + @url_filters = product_filters(request.query_parameters) + + super + end + def create + @url_filter = product_filters(request.query_parameters) + on_demand = params[:variant].delete(:on_demand) on_hand = params[:variant].delete(:on_hand) - super + @object.attributes = permitted_resource_params + if @object.save + flash[:success] = flash_message_for(@object, :successfully_created) + redirect_to admin_product_variants_url(params[:product_id], @url_filter) + else + redirect_to new_admin_product_variant_url(params[:product_id], @url_filter) + end return unless @object.present? && @object.valid? @@ -26,6 +63,8 @@ module Spree end def destroy + @url_filter = product_filters(request.query_parameters) + @variant = Spree::Variant.find(params[:id]) flash[:success] = if VariantDeleter.new.delete(@variant) Spree.t('notice_messages.variant_deleted') @@ -34,7 +73,7 @@ module Spree end respond_with(@variant) do |format| - format.html { redirect_to admin_product_variants_url(params[:product_id]) } + format.html { redirect_to admin_product_variants_url(params[:product_id], @url_filter) } end end diff --git a/app/views/spree/admin/variants/edit.html.haml b/app/views/spree/admin/variants/edit.html.haml index 057c8a9d10..5a90078848 100644 --- a/app/views/spree/admin/variants/edit.html.haml +++ b/app/views/spree/admin/variants/edit.html.haml @@ -4,8 +4,11 @@ = render partial: 'spree/shared/error_messages', locals: { target: @variant } -= form_for [:admin, @product, @variant] do |f| += form_for [:admin, @product, @variant], :url => admin_product_variant_path(@product, @variant, @url_filters) do |f| %fieldset.no-border-top %div = render partial: 'form', locals: { f: f } - = render partial: 'spree/admin/shared/edit_resource_links' + .form-buttons.filter-actions.actions + = button t(:update), 'icon-refresh' + %span.or= t(:or) + = button_link_to t(:cancel), collection_url(@url_filters), icon: 'icon-remove' diff --git a/app/views/spree/admin/variants/index.html.haml b/app/views/spree/admin/variants/index.html.haml index ce9c1edd86..fa2d206749 100644 --- a/app/views/spree/admin/variants/index.html.haml +++ b/app/views/spree/admin/variants/index.html.haml @@ -27,9 +27,10 @@ %td.align-center= variant.display_price.to_html %td.align-center= variant.sku %td.actions - = link_to_edit(variant, no_text: true) unless variant.deleted? - = link_to_delete(variant, no_text: true) unless variant.deleted? + = link_to_with_icon('icon-edit', Spree.t(:edit), edit_object_url(variant, @url_filters), no_text: true) unless variant.deleted? + = link_to_delete(variant, { url: object_url(variant, @url_filters), no_text: true }) unless variant.deleted? +// TODO this - if @product.empty_option_values? %p.first_add_option_types.no-objects-found = t('.to_add_variants_you_must_first_define') @@ -41,6 +42,6 @@ - content_for :page_actions do %ul.inline-menu %li#new_var_link - = link_to_with_icon('icon-plus', t('.new_variant'), new_admin_product_variant_url(@product), class: 'button') + = link_to_with_icon('icon-plus', t('.new_variant'), new_admin_product_variant_url(@product, @url_filters), class: 'button') - %li= link_to_with_icon('icon-filter', @deleted.blank? ? t('.show_deleted') : t('.show_active'), admin_product_variants_url(@product, deleted: @deleted.blank? ? "on" : "off"), class: 'button') + %li= link_to_with_icon('icon-filter', @deleted.blank? ? t('.show_deleted') : t('.show_active'), admin_product_variants_url(@product, @url_filters.merge(deleted: @deleted.blank? ? "on" : "off")), class: 'button') diff --git a/app/views/spree/admin/variants/new.html.haml b/app/views/spree/admin/variants/new.html.haml index 1ff9927713..30105a5c20 100644 --- a/app/views/spree/admin/variants/new.html.haml +++ b/app/views/spree/admin/variants/new.html.haml @@ -1,7 +1,11 @@ = render partial: 'spree/shared/error_messages', locals: { target: @variant } -= form_for [:admin, @product, @variant] do |f| += form_for [:admin, @product, @variant], :url => admin_product_variants_path(@product, @url_filters) do |f| %fieldset{'data-hook' => "admin_variant_new_form"} %legend{align: "center"}= t('.new_variant') = render partial: 'form', locals: { f: f } - = render partial: 'spree/admin/shared/new_resource_links' + + .form-buttons.filter-actions.actions + = button t('actions.create'), 'icon-ok' + %span.or= t(:or) + = button_link_to t('actions.cancel'), collection_url(@url_filters), icon: 'icon-remove' diff --git a/spec/features/admin/variants_spec.rb b/spec/features/admin/variants_spec.rb index 2b5df3c0aa..0ec536bd99 100644 --- a/spec/features/admin/variants_spec.rb +++ b/spec/features/admin/variants_spec.rb @@ -7,23 +7,77 @@ feature ' include AuthenticationHelper include WebHelper - scenario "creating a new variant" do - # Given a product with a unit-related option type - product = create(:simple_product, variant_unit: "weight", variant_unit_scale: "1") + describe "new variant", js: true do + scenario "creating a new variant" do + # Given a product with a unit-related option type + product = create(:simple_product, variant_unit: "weight", variant_unit_scale: "1") - # When I create a variant on the product - login_as_admin_and_visit spree.admin_product_variants_path product - click_link 'New Variant' - fill_in 'unit_value_human', with: '1' - fill_in 'variant_unit_description', with: 'foo' - click_button 'Create' + # When I create a variant on the product + login_as_admin_and_visit spree.admin_product_variants_path product + click_link 'New Variant' - # Then the variant should have been created - expect(page).to have_content "Variant \"#{product.name}\" has been successfully created!" + fill_in 'unit_value_human', with: '1' + fill_in 'variant_unit_description', with: 'foo' + click_button 'Create' + + # Then the variant should have been created + expect(page).to have_content "Variant \"#{product.name}\" has been successfully created!" + end + + scenario "creating a new variant from product variant page with filter" do + # Given a product with a unit-related option type + product = create(:simple_product, variant_unit: "weight", variant_unit_scale: "1") + filter = { producerFilter: 2 } + + # When I create a variant on the product + login_as_admin_and_visit spree.admin_product_variants_path(product, filter) + + click_link 'New Variant' + + # Cancel link should include product filter + expect(page).to have_link(I18n.t('actions.cancel'), href: %r{variants\?#{filter.to_query}}) + end + end + + describe "viewing product variant" do + scenario "when the product page has a product filter" do + # Given a product with a unit-related option type + product = create(:simple_product, variant_unit: "weight", variant_unit_scale: "1") + filter = { producerFilter: 2 } + + # When I create a variant on the product + login_as_admin_and_visit spree.admin_product_variants_path(product, filter) + + visit spree.admin_product_variants_path(product, filter) + + expect(page).to have_link("New Variant", href: %r{variants\/new\?#{filter.to_query}}) + expect(page).to have_link("Show Deleted", href: %r{variants\?deleted=on&#{filter.to_query}}) + + # Variant link should include product filter + variant = product.variants.first + expect(page).to have_link( + I18n.t(:edit), href: %r{variants\/#{variant.id}\/edit\?#{filter.to_query}} + ) + expect(page).to have_link( + I18n.t(:delete), href: %r{variants\/#{variant.id}\?#{filter.to_query}} + ) + end end describe "editing unit value and description for a variant", js: true do + scenario "when the product variant page has product filter" do + product = create(:simple_product, variant_unit: "weight", variant_unit_scale: "1") + filter = { producerFilter: 2 } + + # When I create a variant on the product + login_as_admin_and_visit spree.admin_product_variants_path(product, filter) + page.find('table.index .icon-edit').click + + # Cancel link should include product filter + expect(page).to have_link(I18n.t('actions.cancel'), href: %r{variants\?#{filter.to_query}}) + end + scenario "when variant_unit is weight" do # Given a product with unit-related option types, with a variant product = create(:simple_product, variant_unit: "weight", variant_unit_scale: "1")