mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-02 02:11:33 +00:00
Merge pull request #4064 from luisramos0/convert_search_Rabl
Replace views/admin/variants/search.rabl with Api::Admin::VariantSerializer
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -27,8 +27,6 @@ angular.module("admin.utils").directive "variantAutocomplete", ($timeout) ->
|
||||
window.variants = data # this is how spree auto complete JS code picks up variants
|
||||
results: data
|
||||
formatResult: (variant) ->
|
||||
if variant["images"][0] != undefined && variant["images"][0].image != undefined
|
||||
variant.image = variant.images[0].image.mini_url
|
||||
variantTemplate variant: variant
|
||||
formatSelection: (variant) ->
|
||||
element.parent().children(".options_placeholder").html variant.options_text
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
.add-line-item {
|
||||
fieldset {
|
||||
.vertical-align-top {
|
||||
vertical-align: top;
|
||||
}
|
||||
|
||||
.actions {
|
||||
padding-top: 18px;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ Spree::Admin::VariantsController.class_eval do
|
||||
def search
|
||||
scoper = OpenFoodNetwork::ScopeVariantsForSearch.new(params)
|
||||
@variants = scoper.search
|
||||
render json: @variants, each_serializer: Api::Admin::VariantSerializer
|
||||
end
|
||||
|
||||
def destroy
|
||||
|
||||
@@ -1,15 +1,49 @@
|
||||
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
|
||||
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
|
||||
if object.full_name.present?
|
||||
"#{object.name} - #{object.full_name}"
|
||||
else
|
||||
object.name
|
||||
end
|
||||
end
|
||||
|
||||
def on_hand
|
||||
return 0 if object.on_hand.nil?
|
||||
object.on_hand
|
||||
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
|
||||
|
||||
def producer_name
|
||||
object.product.supplier.name
|
||||
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
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#add-line-item
|
||||
#add-line-item.add-line-item
|
||||
%fieldset
|
||||
%legend{ align: 'center'}= t(:products)
|
||||
%table.no-borders.layout
|
||||
@@ -6,15 +6,15 @@
|
||||
%col{ width: '20%' }
|
||||
%col{ width: '20%' }
|
||||
%tr
|
||||
%td{ style: "vertical-align:top" }
|
||||
%td.vertical-align-top
|
||||
.field
|
||||
= label_tag :add_variant_id, t(:name_or_sku)
|
||||
= label_tag :add_variant_id, t('.name_or_sku')
|
||||
%input#add_variant_id.variant_autocomplete.fullwidth{ type: 'number', ng: { model: 'newItem.variant_id' } }
|
||||
%td{ style: "vertical-align:top" }
|
||||
%td.vertical-align-top
|
||||
.field
|
||||
= label_tag :add_quantity, t(:qty)
|
||||
= label_tag :add_quantity, t('.quantity')
|
||||
%input#add_quantity.fullwidth{ type: 'number', min: 1, ng: { model: 'newItem.quantity' } }
|
||||
%td{ style: "vertical-align:top" }
|
||||
%td
|
||||
.actions
|
||||
%a.icon-plus.button.fullwidth{ href: 'javascript:void(0)', method: :post, ng: { click: 'addSubscriptionLineItem()' } }
|
||||
= t(:add)
|
||||
= t('.add')
|
||||
|
||||
@@ -16,16 +16,13 @@
|
||||
</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>
|
||||
|
||||
{{#if variant.option_values}}
|
||||
<ul class='variant-options'>
|
||||
{{#each variant.option_values}}
|
||||
<li><strong>{{this.option_type.presentation}}:</strong> {{this.presentation}}</li>
|
||||
{{/each}}
|
||||
</ul>
|
||||
{{/if}}
|
||||
</div>
|
||||
</div>
|
||||
</script>
|
||||
@@ -45,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}}
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
#
|
||||
# overriding spree/core/app/views/spree/admin/variants/search.rabl
|
||||
#
|
||||
collection @variants
|
||||
attributes :sku, :options_text, :in_stock?, :on_demand, :on_hand, :id, :cost_price
|
||||
|
||||
node(:name) do |v|
|
||||
# TODO: when products must have a unit, full_name will always be present
|
||||
variant_specific = v.full_name
|
||||
if variant_specific.present?
|
||||
"#{v.name} - #{v.full_name}"
|
||||
else
|
||||
v.name
|
||||
end
|
||||
end
|
||||
|
||||
node(:full_name, &:full_name)
|
||||
|
||||
node(:producer_name) do |v|
|
||||
v.product.supplier.name
|
||||
end
|
||||
|
||||
node(:stock_location_id) do |v|
|
||||
v.stock_items.first.stock_location.id
|
||||
end
|
||||
|
||||
node(:stock_location_name) do |v|
|
||||
v.stock_items.first.stock_location.name
|
||||
end
|
||||
|
||||
child(images: :images) do
|
||||
attributes :mini_url
|
||||
end
|
||||
|
||||
child(option_values: :option_values) do
|
||||
child(option_type: :option_type) do
|
||||
attributes :name, :presentation
|
||||
end
|
||||
attributes :name, :presentation
|
||||
end
|
||||
@@ -1060,6 +1060,10 @@ en:
|
||||
The displayed prices are only an estimate and calculated at the time the subscription is changed.
|
||||
If you change prices or fees, orders will be updated, but the subscription will still display the old values.
|
||||
not_in_open_and_upcoming_order_cycles_warning: "There are no open or upcoming order cycles for this product."
|
||||
autocomplete:
|
||||
name_or_sku: "NAME OR SKU"
|
||||
quantity: "Quantity"
|
||||
add: "Add"
|
||||
details:
|
||||
details: Details
|
||||
invalid_error: Oops! Please fill in all of the required fields...
|
||||
@@ -3146,7 +3150,8 @@ See the %{link} to find out more about %{sitename}'s features and to start using
|
||||
confirmation_pending: "Email confirmation is pending. We've sent a confirmation email to %{address}."
|
||||
variants:
|
||||
autocomplete:
|
||||
producer_name: Producer
|
||||
producer_name: "Producer"
|
||||
unit: "Unit"
|
||||
general_settings:
|
||||
edit:
|
||||
legal_settings: "Legal Settings"
|
||||
|
||||
30
spec/serializers/api/admin/variant_serializer_spec.rb
Normal file
30
spec/serializers/api/admin/variant_serializer_spec.rb
Normal file
@@ -0,0 +1,30 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe Api::Admin::VariantSerializer do
|
||||
|
||||
let(:variant) { create(:variant) }
|
||||
|
||||
it "serializes the variant name" do
|
||||
serializer = Api::Admin::VariantSerializer.new variant
|
||||
|
||||
expect(serializer.to_json).to match variant.name
|
||||
end
|
||||
|
||||
it "serializes the variant options" do
|
||||
serializer = Api::Admin::VariantSerializer.new variant
|
||||
|
||||
expect(serializer.to_json).to match variant.options_text
|
||||
end
|
||||
|
||||
it "serializes the variant full name" do
|
||||
serializer = Api::Admin::VariantSerializer.new variant
|
||||
|
||||
expect(serializer.to_json).to match variant.full_name
|
||||
end
|
||||
|
||||
it "serializes the variant stock location id" do
|
||||
serializer = Api::Admin::VariantSerializer.new variant
|
||||
|
||||
expect(serializer.to_json).to match variant.stock_items.first.stock_location.id.to_s
|
||||
end
|
||||
end
|
||||
@@ -1,7 +0,0 @@
|
||||
describe Api::Admin::VariantSerializer do
|
||||
let(:variant) { create(:variant) }
|
||||
it "serializes a variant" do
|
||||
serializer = Api::Admin::VariantSerializer.new variant
|
||||
expect(serializer.to_json).to match variant.options_text
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user