Skip to content

Commit

Permalink
Fixed track inventory checkbox on Product > Stock page with regressio…
Browse files Browse the repository at this point in the history
…n test

Fixes spree#6286
  • Loading branch information
smartacus authored and Jeff Dutil committed Apr 16, 2015
1 parent 02bfddf commit b0e5972
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
jQuery ->
$('.track_inventory_checkbox').on 'click', ->
$(@).parents('form').submit()
$(this).siblings('.variant_track_inventory').val($(this).is(':checked'))
$(this).parents('form').submit()
$('.toggle_variant_track_inventory').on 'submit', ->
$.ajax
type: @method
url: @action
data: $(@).serialize()
data: $(this).serialize()
false
8 changes: 6 additions & 2 deletions backend/app/controllers/spree/admin/resource_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def update
flash[:success] = flash_message_for(@object, :successfully_updated)
respond_with(@object) do |format|
format.html { redirect_to location_after_save }
format.js { render :layout => false }
format.js { render layout: false }
end
else
invoke_callbacks(:update, :fails)
Expand Down Expand Up @@ -125,7 +125,7 @@ def resource
if parent_data
parent_model_name = parent_data[:model_name]
end
@resource = Spree::Admin::Resource.new controller_path, controller_name, parent_model_name
@resource = Spree::Admin::Resource.new controller_path, controller_name, parent_model_name, object_name
end

def load_resource
Expand Down Expand Up @@ -240,6 +240,10 @@ def collection_url(options = {})
end
end

# This method should be overridden when object_name does not match the controller name
def object_name
end

# Allow all attributes to be updatable.
#
# Other controllers can, should, override it to set custom logic
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Spree
module Admin
class VariantsIncludingMasterController < VariantsController
belongs_to "spree/product", find_by: :slug

def model_class
Spree::Variant
Expand Down
4 changes: 3 additions & 1 deletion backend/app/models/spree/admin/resource.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module Spree
module Admin
class Resource
def initialize(controller_path, controller_name, parent_model)
def initialize(controller_path, controller_name, parent_model, object_name = nil)
@controller_path = controller_path
@controller_name = controller_name
@parent_model = parent_model
@object_name = object_name
end

def sub_namespace_parts
Expand All @@ -24,6 +25,7 @@ def model_name
end

def object_name
return @object_name if @object_name
sub_namespace = sub_namespace_parts.join('_')
sub_namespace = "#{sub_namespace}_" if sub_namespace.length > 0
"#{sub_namespace}#{@controller_name.singularize}"
Expand Down
8 changes: 4 additions & 4 deletions backend/app/views/spree/admin/products/stock.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@
</td>
<td>
<%= variant.sku_and_options_text %>
<%= form_tag admin_product_variants_including_master_path(@product, variant, format: :js), method: :post, class: 'toggle_variant_track_inventory' do %>
<%= form_tag admin_product_variants_including_master_path(@product, variant, format: :js), method: :put, class: 'toggle_variant_track_inventory' do %>
<div class="checkbox">
<%= label_tag :track_inventory do %>
<%= check_box_tag 'track_inventory', 1, variant.track_inventory?,
class: 'track_inventory_checkbox' %>
<%= Spree.t(:track_inventory) %>
<%= hidden_field_tag 'variant[track_inventory]', variant.track_inventory?,
class: 'variant_track_inventory',
id: "variant_track_inventory_#{variant.id}" %>
<% end %>
</div>
<%= hidden_field_tag 'variant[track_inventory]', variant.track_inventory?,
class: 'variant_track_inventory',
id: "variant_track_inventory_#{variant.id}" %>
<% end if can?(:update, @product) && can?(:update, variant) %>
</td>

Expand Down
1 change: 1 addition & 0 deletions backend/app/views/spree/admin/variants/update.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= @object.as_json %>
15 changes: 15 additions & 0 deletions backend/spec/features/admin/products/stock_management_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@
end
end

context "toggle track inventory for a variant's stock item" do
let(:track_inventory) { find ".track_inventory_checkbox" }

before do
expect(track_inventory).to be_checked
track_inventory.set(false)
wait_for_ajax
end

it "persists the value when page reloaded", js: true do
visit current_path
expect(track_inventory).not_to be_checked
end
end

# Regression test for #2896
# The regression was that unchecking the last checkbox caused a redirect
# to happen. By ensuring that we're still on an /admin/products URL, we
Expand Down
5 changes: 5 additions & 0 deletions backend/spec/models/spree/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Admin
describe Resource, type: :model do
let(:resource_base) { Resource.new('spree/admin/test', 'test', 'widget') }
let(:resource_submodule) { Resource.new('spree/admin/submodule/test', 'test', 'widget') }
let(:resource_object_name) { Resource.new('spree/admin/test', 'test', 'gadget', 'widget') }

it "can get base model class" do
expect(resource_base.model_class).to eq(Spree::Test)
Expand All @@ -38,6 +39,10 @@ module Admin
it "can get submodule object name" do
expect(resource_submodule.object_name).to eq('submodule_test')
end

it "can get overridden object name" do
expect(resource_object_name.object_name).to eq('widget')
end
end
end
end

0 comments on commit b0e5972

Please sign in to comment.