Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Added `CSV::ProductPresenter` (#12201)

Damian Legawiec00db942b4070
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index b7cc032b46..3061ee68cf 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -179,6 +179,8 @@ module Spree
delegate :display_amount, :display_price, :has_default_price?,
:display_compare_at_price, :images, to: :find_or_build_master
+ delegate :name, to: :brand, prefix: true, allow_nil: true
+
alias master_images images
state_machine :status, initial: :draft do
diff --git a/core/app/presenters/spree/csv/product_presenter.rb b/core/app/presenters/spree/csv/product_presenter.rb
new file mode 100644
index 0000000000..c57dfdc1cd
--- /dev/null
+++ b/core/app/presenters/spree/csv/product_presenter.rb
@@ -0,0 +1,65 @@
+require 'csv'
+
+module Spree
+ module CSV
+ class ProductPresenter
+ def initialize(product, properties = [], store = nil)
+ @product = product
+ @properties = properties
+ @store = store || product.stores.first
+ @currency = @store.default_currency
+ end
+
+ attr_accessor :product, :properties, :store, :currency
+
+ def call
+ Rails.cache.fetch([@product.cache_key_with_version, currency, properties.map(&:id).join('-')]) do
+ [
+ product.id,
+ product.try(:vendor_name),
+ product.try(:brand_name),
+ product.name,
+ product.description&.html_safe,
+ product.amount_in(currency).to_f,
+ product.meta_title,
+ product.meta_description,
+ product.meta_keywords,
+ product.tag_list.to_s,
+ product.label_list.to_s,
+ product.width,
+ product.height,
+ product.depth,
+ product.weight,
+ product.available_on&.strftime('%Y-%m-%d %H:%M:%S'),
+ product.discontinue_on&.strftime('%Y-%m-%d %H:%M:%S'),
+ product.status,
+ *map_categories(product),
+ product.total_on_hand == BigDecimal::INFINITY ? '∞' : product.total_on_hand,
+ *present_properties
+ ]
+ end
+ end
+
+ private
+
+ def map_categories(product)
+ categories = [nil, nil, nil]
+
+ product.taxons.reorder(depth: :desc).first(3).pluck(:pretty_name).each_with_index do |category_name, index|
+ categories[index] = category_name
+ end
+
+ categories
+ end
+
+ def present_properties
+ properties.flat_map do |property|
+ [
+ property.name,
+ product.property(property.name)
+ ]
+ end
+ end
+ end
+ end
+end
diff --git a/core/config/initializers/inflections.rb b/core/config/initializers/inflections.rb
index d594ff3c78..b5c808881f 100644
--- a/core/config/initializers/inflections.rb
+++ b/core/config/initializers/inflections.rb
@@ -1,4 +1,5 @@
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym 'RMA'
inflect.acronym 'B2B'
+ inflect.acronym 'CSV'
end
diff --git a/core/spec/presenters/spree/csv/product_presenter_spec.rb b/core/spec/presenters/spree/csv/product_presenter_spec.rb
new file mode 100644
index 0000000000..6239792872
--- /dev/null
+++ b/core/spec/presenters/spree/csv/product_presenter_spec.rb
@@ -0,0 +1,102 @@
+require 'spec_helper'
+
+describe Spree::CSV::ProductPresenter do
+ describe '#call' do
+ subject(:presentation) { described_class.new(product, properties).call }
+
+ let(:store) { Spree::Store.default }
+ let(:brand_taxonomy) { store.taxonomies.find_by(name: Spree.t(:taxonomy_brands_name)) || create(:taxonomy, name: Spree.t(:taxonomy_brands_name), store: store) }
+ let(:brand) { create(:taxon, taxonomy: brand_taxonomy, name: 'Super seller') }
+ let(:property) { create(:property, :material) }
+ let(:product) do
+ create(:product,
+ product_properties: [create(:product_property, property: property, value: 'Cotton')],
+ taxons: [brand])
+ end
+
+ let(:properties) { [property] }
+
+ before(:each) do
+ product.update(available_on: Time.current, weight: 77, width: 777)
+ product.label_list.add('label1', 'label2')
+ product.tag_list.add('tag1', 'tag2')
+ product.save!
+ end
+
+ it 'presents product with properties' do
+ product.reload
+
+ expect(presentation.length).to eq(24)
+
+ expect(presentation[0]).to eq(product.id)
+ expect(presentation[2]).to eq(product.brand.name)
+ expect(presentation[3]).to eq(product.name)
+ expect(presentation[4]).to eq(product.description)
+ expect(presentation[5]).to eq(product.price.to_f)
+ expect(presentation[6]).to eq(product.meta_title)
+ expect(presentation[7]).to eq(product.meta_description)
+ expect(presentation[8]).to eq(product.meta_keywords)
+ expect(presentation[9].to_s).to eq(product.tag_list.to_s)
+ expect(presentation[10].to_s).to eq(product.label_list.to_s)
+ expect(presentation[11]).to eq(product.width)
+ expect(presentation[12]).to eq(product.height)
+ expect(presentation[13]).to eq(product.depth)
+ expect(presentation[14]).to eq(product.weight)
+ expect(presentation[15]).to eq(product.available_on.strftime('%Y-%m-%d %H:%M:%S'))
+ expect(presentation[16]).to eq(product.discontinue_on)
+ expect(presentation[17]).to eq(product.status)
+ expect(presentation[18]).to eq('Brands -> Super seller')
+ expect(presentation[19]).to eq(nil)
+ expect(presentation[20]).to eq(nil)
+ expect(presentation[21]).to eq(0)
+ expect(presentation[22]).to eq(property.name)
+ expect(presentation[23]).to eq(product.property(property.name))
+ end
+
+ context 'when providing more properties' do
+ let(:property_2) { create(:property, :manufacturer) }
+ let(:property_3) { create(:property, :brand) }
+
+ let(:properties) { [property, property_2, property_3] }
+
+ before do
+ product.product_properties << create(:product_property, property: property_2, value: 'Global manufacturer')
+ product.product_properties << create(:product_property, property: property_3, value: 'Global brand')
+ end
+
+ it 'presents product with properties' do
+ product.reload
+
+ expect(presentation.length).to eq(28)
+
+ expect(presentation[0]).to eq(product.id)
+ expect(presentation[2]).to eq(product.brand.name)
+ expect(presentation[3]).to eq(product.name)
+ expect(presentation[4]).to eq(product.description)
+ expect(presentation[5]).to eq(product.price.to_f)
+ expect(presentation[6]).to eq(product.meta_title)
+ expect(presentation[7]).to eq(product.meta_description)
+ expect(presentation[8]).to eq(product.meta_keywords)
+ expect(presentation[9].to_s).to eq(product.tag_list.to_s)
+ expect(presentation[10].to_s).to eq(product.label_list.to_s)
+ expect(presentation[11]).to eq(product.width)
+ expect(presentation[12]).to eq(product.height)
+ expect(presentation[13]).to eq(product.depth)
+ expect(presentation[14]).to eq(product.weight)
+ expect(presentation[15]).to eq(product.available_on.strftime('%Y-%m-%d %H:%M:%S'))
+ expect(presentation[16]).to eq(product.discontinue_on)
+ expect(presentation[17]).to eq(product.status)
+ expect(presentation[18]).to eq('Brands -> Super seller')
+ expect(presentation[19]).to eq(nil)
+ expect(presentation[20]).to eq(nil)
+ expect(presentation[21]).to eq(0)
+ expect(presentation[22]).to eq('material')
+ expect(presentation[23]).to eq('Cotton')
+ expect(presentation[24]).to eq('manufacturer')
+ expect(presentation[25]).to eq('Global manufacturer')
+ expect(presentation[26]).to eq('brand')
+ expect(presentation[27]).to eq('Global brand')
+ end
+ end
+ end
+end