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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Refactor service, model and controller.
wjwitek519ace474dd2
diff --git a/api/app/controllers/spree/api/v2/platform/google_export_options_controller.rb b/api/app/controllers/spree/api/v2/platform/google_export_options_controller.rbdeleted file mode 100644index 8cd84b7181..0000000000--- a/api/app/controllers/spree/api/v2/platform/google_export_options_controller.rb+++ /dev/null@@ -1,15 +0,0 @@-module Spree- module Api- module V2- module Platform- class GoogleExportOptionsController < ResourceController- def show- @options = GoogleExportOption.find_by store: params[:store_id]-- send_data @options.export, filename: 'products.rss', type: 'text/xml'- end- end- end- end- end-enddiff --git a/api/app/controllers/spree/api/v2/storefront/stores_controller.rb b/api/app/controllers/spree/api/v2/storefront/stores_controller.rbindex 6699fee62e..fb480b110e 100644--- a/api/app/controllers/spree/api/v2/storefront/stores_controller.rb+++ b/api/app/controllers/spree/api/v2/storefront/stores_controller.rb@@ -3,12 +3,26 @@ module Spreemodule V2module Storefrontclass StoresController < ::Spree::Api::V2::ResourceController+ before_action :update_options+def currentrender_serialized_payload { serialize_resource(current_store) }end+ def export_google_rss_feed+ send_data export_google_rss, filename: 'products.rss', type: 'text/xml'+ end+private+ def update_options+ @settings = Spree::GoogleFeedSetting.find_by(spree_store_id: current_store)+ end++ def export_google_rss+ Spree::Dependencies.export_google_rss_service.constantize.new.call(@settings)+ end+def model_classSpree::Storeenddiff --git a/api/config/routes.rb b/api/config/routes.rbindex 7cd76d1050..07959c553d 100644--- a/api/config/routes.rb+++ b/api/config/routes.rb@@ -63,6 +63,9 @@ Spree::Core::Engine.add_routes doendget '/digitals/:token', to: 'digitals#download', as: 'digital'++ # google export API+ get '/rss/google', to: 'stores#export_google_rss_feed'endnamespace :platform do@@ -206,9 +209,6 @@ Spree::Core::Engine.add_routes doresources :events, only: :indexresources :subscribersend-- # google export API- get '/rss/:store_id', to: 'google_export_options#show'endendenddiff --git a/core/app/models/spree/google_export_option.rb b/core/app/models/spree/google_feed_setting.rbsimilarity index 53%rename from core/app/models/spree/google_export_option.rbrename to core/app/models/spree/google_feed_setting.rbindex b3fd52a425..ce312be93c 100644--- a/core/app/models/spree/google_export_option.rb+++ b/core/app/models/spree/google_feed_setting.rb@@ -1,10 +1,10 @@module Spree- class GoogleExportOption < Base- def export- Spree::Dependencies.export.constantize.new.export_google_rss(self)- end+ class GoogleFeedSetting < Base+ belongs_to :store, class_name: 'Spree::Store', foreign_key: 'spree_store_id'++ validates :store, presence: true- def true_keys+ def enabled_keyskeys = []attributes.each do |key, value|diff --git a/core/app/services/spree/export.rb b/core/app/services/spree/export.rbdeleted file mode 100644index f26172b135..0000000000--- a/core/app/services/spree/export.rb+++ /dev/null@@ -1,103 +0,0 @@-require 'nokogiri'--module Spree- class Export- prepend Spree::ServiceModule::Base-- def export_google_rss(options)- store = Spree::Store.find(options.store)- @options = options-- builder = Nokogiri::XML::Builder.new do |xml|- xml.rss('xmlns:g' => 'http://base.google.com/ns/1.0', 'version' => '2.0') do- xml.channel do- store_information(xml, store)- Spree::Product.find_each do |product|- next unless validate_product(product)-- product.variants.each do |variant|- next unless validate_variant(variant, product)-- xml.item do- required_product_information(xml, variant, product)- optional_information(xml, product)- end- end- end- end- end- end-- builder.to_xml- end-- def required_product_information(xml, variant, product)- xml['g'].id variant.id- xml['g'].title variant.sku- xml['g'].description product.description- xml['g'].link product.slug- xml['g'].image_link get_image_link(variant, product)- xml['g'].price format_price(variant)- xml['g'].availability get_availability(product)- xml['g'].availability_date product.available_on.xmlschema- end-- def optional_information(xml, product)- @options.true_keys.each do |key|- if @options.send(key) && !product.property(key.to_s).nil?- xml['g'].send(key, product.property(key.to_s))- end- end- end-- def validate_product(product)- unless product.deleted_at.nil?- false- end- true- end-- def validate_variant(variant, product)- unless variant.deleted_at.nil?- false- end- # TODO: make variant sku and description fallback to product's if they are nil- if variant.id.nil? || variant.sku.nil? || variant.description.nil? || variant.slug.nil? || get_image_link(variant, product).nil?- false- end- true- end-- def get_image_link(variant, product)- # try getting image from variant- img = variant.images.first&.plp_url-- # if no image specified for variant try getting product image- if img.nil?- img = product.images.first&.plp_url- end-- img- end-- def get_availability(product)- if product.available_on.past?- 'in stock'- elsif !product.available_on.nil?- 'backorder'- else- 'out of stock'- end- end-- def format_price(variant)- "#{variant.cost_price} #{variant.cost_currency}"- end-- def store_information(xml, store)- xml.title store.name- xml.link store.url- xml.description store.meta_description- end- end-enddiff --git a/core/app/services/spree/export/google_rss.rb b/core/app/services/spree/export/google_rss.rbnew file mode 100644index 0000000000..74364a37ce--- /dev/null+++ b/core/app/services/spree/export/google_rss.rb@@ -0,0 +1,133 @@+require 'nokogiri'++module Spree+ module Export+ class GoogleRss+ def call(options)+ @store = Spree::Store.find(options.spree_store_id)+ @options = options++ builder = Nokogiri::XML::Builder.new do |xml|+ xml.rss('xmlns:g' => 'http://base.google.com/ns/1.0', 'version' => '2.0') do+ xml.channel do+ add_store_information_to_xml(xml)+ Spree::Product.find_each do |product|+ product.variants.active.each do |variant|+ add_variant_information_to_xml(xml, product, variant)+ end+ end+ end+ end+ end++ builder.to_xml+ end++ private++ def add_store_information_to_xml(xml)+ xml.title @store.name+ xml.link @store.url+ xml.description @store.meta_description+ end++ def add_variant_information_to_xml(xml, product, variant)+ return if get_image_link(variant, product).nil?++ xml.item do+ xml['g'].id variant.id+ xml['g'].title format_title(product, variant)+ xml['g'].description get_description(product, variant)+ xml['g'].link "#{@store.url}/#{product.slug}"+ xml['g'].image_link get_image_link(variant, product)+ xml['g'].price format_price(variant)+ xml['g'].availability get_availability(product)+ xml['g'].availability_date product.available_on.xmlschema++ add_optional_information(xml, product)+ end+ end++ def format_title(product, variant)+ # Title of a variant is created by joining title of a product and variant's option_values, as they are+ # what differentiaties it from other variants.+ title = product.name+ variant.option_values.find_each do |option_value|+ title << " - #{option_value.name}"+ end+ title+ end++ def get_description(product, variant)+ return product.description unless product.description.nil?++ format_title(product, variant)+ end++ def get_image_link(variant, product)+ # try getting image from variant+ img = variant.images.first&.plp_url++ # if no image specified for variant try getting product image+ if img.nil?+ img = product.images.first&.plp_url+ end++ img+ end++ def format_price(variant)+ "#{variant.cost_price} #{variant.cost_currency}"+ end++ def get_availability(product)+ return 'in stock' if product.available_on.past?+ return 'backorder' unless product.available_on.nil?++ 'out of stock'+ end++ def add_optional_information(xml, product)+ @options.enabled_keys.each do |key|+ if @options.send(key) && !product.property(key.to_s).nil?+ xml['g'].send(key, product.property(key.to_s))+ end+ end+ end++ # example of modifing optional information+ #+ # By default, this code assumes that any information that is not required by Google+ # (see https://support.google.com/merchants/answer/160589?hl=en) is stored in Spree::Products's properties.+ # If it's in other column you can modify add_optional_information like this:+ # def add_optional_information(xml, product)+ # @options.enabled_keys.each do |key|+ # if @options.send(key) && !product.property(key.to_s).nil?+ # xml['g'].send(key, product.property(key.to_s))+ # end+ # end+ # if !product.column_name.nil?+ # xml['g'].attribute product.column_name+ # end+ # end+ #+ # If the column is part of variant or for example is option value, you will need to add variant to function's+ # arguments and modify code analogically as above.+ #+ # def add_optional_information(xml, product, variant)+ # @options.enabled_keys.each do |key|+ # if @options.send(key) && !product.property(key.to_s).nil?+ # xml['g'].send(key, product.property(key.to_s))+ # end+ # end+ # if !variant.column_name.nil?+ # xml['g'].attribute variant.column_name+ # end+ # size_value = variant.option_value("size")+ # if !size_value.nil?+ # xml['g'].size size_value+ # end+ # end+ end+ end+enddiff --git a/core/db/migrate/20221229132350_create_spree_google_export_options.rb b/core/db/migrate/20221229132350_create_spree_google_feed_settings.rbsimilarity index 64%rename from core/db/migrate/20221229132350_create_spree_google_export_options.rbrename to core/db/migrate/20221229132350_create_spree_google_feed_settings.rbindex 10ebbececb..90709a247d 100644--- a/core/db/migrate/20221229132350_create_spree_google_export_options.rb+++ b/core/db/migrate/20221229132350_create_spree_google_feed_settings.rb@@ -1,7 +1,7 @@-class CreateSpreeGoogleExportOptions < ActiveRecord::Migration[6.0]+class CreateSpreeGoogleFeedSettings < ActiveRecord::Migration[6.0]def change- create_table :spree_google_export_options do |t|- t.integer :store+ create_table :spree_google_feed_settings do |t|+ t.references :spree_storekeys = [:material, :brand, :gender, :condition, :gtin, :mpn, :adult, :multipack, :is_bundle, :color, :pattern,:size, :item_group_id]diff --git a/core/lib/spree/core/dependencies.rb b/core/lib/spree/core/dependencies.rbindex 07d38111e8..13a000d21b 100644--- a/core/lib/spree/core/dependencies.rb+++ b/core/lib/spree/core/dependencies.rb@@ -21,7 +21,7 @@ module Spree:order_approve_service, :order_cancel_service, :shipment_change_state_service, :shipment_update_service,:shipment_create_service, :shipment_add_item_service, :shipment_remove_item_service,:payment_create_service, :address_create_service, :address_update_service,- :checkout_select_shipping_method_service, :export+ :checkout_select_shipping_method_service, :export_google_rss_service].freezeattr_accessor *INJECTION_POINTS@@ -112,7 +112,7 @@ module Spree@error_handler = 'Spree::ErrorReporter'# export- @export = 'Spree::Export'+ @export_google_rss_service = 'Spree::Export::GoogleRss'enddef set_default_finders