Add :in_stock, :stock_location_id, :stock_location_name to admin variant serializer, these are needed for the variant_autocomplete js code from spree

This commit is contained in:
luisramos0
2019-07-23 16:32:10 +01:00
parent 7e6259da31
commit 2d5eccbf97
3 changed files with 27 additions and 6 deletions

View File

@@ -123,7 +123,6 @@ Metrics/LineLength:
- app/serializers/api/admin/subscription_serializer.rb
- app/serializers/api/admin/tag_rule_serializer.rb
- app/serializers/api/admin/variant_override_serializer.rb
- app/serializers/api/admin/variant_serializer.rb
- app/services/cart_service.rb
- app/services/default_stock_location.rb
- app/services/embedded_page_service.rb

View File

@@ -1,6 +1,9 @@
class Api::Admin::VariantSerializer < ActiveModel::Serializer
attributes :id, :options_text, :unit_value, :unit_description, :unit_to_display, :on_demand, :display_as, :display_name, :name_to_display, :sku
attributes :on_hand, :price, :import_date, :name, :producer_name, :image
attributes :id, :name, :producer_name, :image, :sku, :import_date
attributes :options_text, :unit_value, :unit_description, :unit_to_display
attributes :display_as, :display_name, :name_to_display
attributes :price, :on_demand, :on_hand, :in_stock, :stock_location_id, :stock_location_name
has_many :variant_overrides
def name
@@ -17,7 +20,7 @@ class Api::Admin::VariantSerializer < ActiveModel::Serializer
end
def price
# Decimals are passed to json as strings, we need to run parseFloat.toFixed(2) on the client side.
# Decimals are passed to json as strings, we need to run parseFloat.toFixed(2) on the client.
object.price.nil? ? 0.to_f : object.price
end
@@ -26,6 +29,21 @@ class Api::Admin::VariantSerializer < ActiveModel::Serializer
end
def image
return if object.product.images.empty?
object.product.images.first.mini_url
end
def in_stock
object.in_stock?
end
def stock_location_id
return if object.stock_items.empty?
object.stock_items.first.stock_location.id
end
def stock_location_name
return if object.stock_items.empty?
object.stock_items.first.stock_location.name
end
end

View File

@@ -16,7 +16,11 @@
</ul>
<ul class='variant-data'>
<li class='variant-sku'><strong><%= t('admin.sku') %>:</strong> {{variant.sku}}</li>
<li class='variant-on_hand'><strong><%= t('on_hand') %>:</strong> {{variant.on_hand}}</li>
{{#if variant.on_demand }}
<li class='variant-on_demand'><strong><%= t('on_demand') %></strong></li>
{{ else }}
<li class='variant-on_hand'><strong><%= t('on_hand') %>:</strong> {{variant.on_hand}}</li>
{{/if}}
<li class='variant-options_text'><strong><%= t('.unit') %>:</strong> {{variant.options_text}}</li>
</ul>
</div>
@@ -38,7 +42,7 @@
</thead>
<tbody>
<tr>
{{#if variant.[in_stock?]}}
{{#if variant.in_stock}}
{{#if variant.on_demand}}
<td><%= Spree.t(:on_demand) %></td>
{{else}}