Skip to content

Commit

Permalink
Merge pull request #3210 from nebulab/kennyadsl/jbuilder-2.9.0
Browse files Browse the repository at this point in the history
Improve jbuilder serialization for Oj gem
  • Loading branch information
kennyadsl authored May 28, 2019
2 parents 897dc04 + 71a1ef7 commit 5fc79f6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
4 changes: 4 additions & 0 deletions api/app/helpers/spree/api/api_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ def variant_attributes
@@variant_attributes
end
end

def total_on_hand_for(object)
object.total_on_hand.finite? ? object.total_on_hand : nil
end
end
end
end
3 changes: 2 additions & 1 deletion api/app/views/spree/api/products/_product.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

@product_attributes ||= product_attributes
json.cache! [I18n.locale, @current_user_roles.include?('admin'), current_pricing_options, @product_attributes, @exclude_data, product] do
json.(product, *@product_attributes)
json.(product, *(@product_attributes - [:total_on_hand]))
json.total_on_hand(total_on_hand_for(product))
json.price(product.price_for(current_pricing_options).try(:to_d))
json.display_price(product.price_for(current_pricing_options).to_s)

Expand Down
5 changes: 1 addition & 4 deletions api/app/views/spree/api/variants/_small.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ json.cache! [I18n.locale, current_pricing_options, variant] do
json.in_stock(variant.in_stock?)
json.is_backorderable(variant.is_backorderable?)

# We can't represent Float::INFINITY in JSON
# Under JSON this woulb be NULL
# Under oj this would error
json.total_on_hand(variant.should_track_inventory? ? variant.total_on_hand : nil)
json.total_on_hand(total_on_hand_for(variant))

json.is_destroyed(variant.destroyed?)
json.option_values(variant.option_values) do |option_value|
Expand Down
19 changes: 19 additions & 0 deletions api/spec/requests/spree/api/products_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,25 @@ module Spree
expect(json_response['shipping_category_id']).to eq shipping_id
end

context "when tracking is disabled" do
before { Config.track_inventory_levels = false }

it "still displays valid json with total_on_hand Float::INFINITY" do
post spree.api_products_path, params: {
product: {
name: "The Other Product",
price: 19.99,
shipping_category_id: create(:shipping_category).id
}
}

expect(response.status).to eq(201)
expect(json_response['total_on_hand']).to eq nil
end

after { Config.track_inventory_levels = true }
end

it "puts the created product in the given taxon" do
product_data[:taxon_ids] = taxon_1.id.to_s
post spree.api_products_path, params: { product: product_data }
Expand Down
14 changes: 14 additions & 0 deletions api/spec/requests/spree/api/variants_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,20 @@ module Spree
expect(json_response["variant_properties"].first).to have_attributes(expected_attrs)
end
end

context "when tracking is disabled" do
before do
Config.track_inventory_levels = false
subject
end

it "still displays valid json with total_on_hand Float::INFINITY" do
expect(response.status).to eq(200)
expect(json_response['total_on_hand']).to eq nil
end

after { Config.track_inventory_levels = true }
end
end

it "can learn how to create a new variant" do
Expand Down

0 comments on commit 5fc79f6

Please sign in to comment.