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
Extract Data Feeds to spree_data_feeds extension (#13688)
Damian Legawiec63ed93e078ea
diff --git a/spree/api/app/controllers/spree/api/v3/store/data_feeds_controller.rb b/spree/api/app/controllers/spree/api/v3/store/data_feeds_controller.rbdeleted file mode 100644index 16edcc16f7..0000000000--- a/spree/api/app/controllers/spree/api/v3/store/data_feeds_controller.rb+++ /dev/null@@ -1,20 +0,0 @@-module Spree- module Api- module V3- module Store- class DataFeedsController < Store::BaseController- skip_before_action :authenticate_api_key!- skip_before_action :authenticate_user-- # GET /api/v3/store/feeds/:slug.xml- def show- data_feed = current_store.data_feeds.active.find_by!(slug: params[:slug])- presenter = data_feed.class.presenter_class.new(data_feed)-- render xml: presenter.call- end- end- end- end- end-enddiff --git a/spree/api/config/routes.rb b/spree/api/config/routes.rbindex fd15ff08d3..16b2dc7046 100644--- a/spree/api/config/routes.rb+++ b/spree/api/config/routes.rb@@ -83,8 +83,6 @@ Spree::Core::Engine.add_routes do# Access via token in URLget 'digitals/:token', to: 'digitals#show', as: :digital_download- # Data Feeds (public, no auth required)- resources :feeds, only: [:show], controller: 'data_feeds', param: :slugendendenddiff --git a/spree/api/spec/controllers/spree/api/v3/store/data_feeds_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/data_feeds_controller_spec.rbdeleted file mode 100644index 4d5b782b76..0000000000--- a/spree/api/spec/controllers/spree/api/v3/store/data_feeds_controller_spec.rb+++ /dev/null@@ -1,38 +0,0 @@-require 'spec_helper'--RSpec.describe Spree::Api::V3::Store::DataFeedsController, type: :controller do- render_views-- include_context 'API v3 Store'-- let!(:data_feed) { create(:google_data_feed, store: store, slug: 'test-feed') }-- describe 'GET #show' do- it 'returns the feed XML without authentication' do- get :show, params: { slug: 'test-feed' }-- expect(response).to have_http_status(:ok)- expect(response.content_type).to include('application/xml')- expect(response.body).to include('<rss')- expect(response.body).to include(store.name)- end-- context 'when data feed is inactive' do- let!(:data_feed) { create(:google_data_feed, store: store, slug: 'inactive-feed', active: false) }-- it 'returns 404' do- get :show, params: { slug: 'inactive-feed' }-- expect(response).to have_http_status(:not_found)- end- end-- context 'when data feed does not exist' do- it 'returns 404' do- get :show, params: { slug: 'nonexistent' }-- expect(response).to have_http_status(:not_found)- end- end- end-enddiff --git a/spree/core/app/models/spree/data_feed.rb b/spree/core/app/models/spree/data_feed.rbdeleted file mode 100644index 2f76e73079..0000000000--- a/spree/core/app/models/spree/data_feed.rb+++ /dev/null@@ -1,47 +0,0 @@-module Spree- class DataFeed < Spree.base_class- has_prefix_id :df-- belongs_to :store, class_name: 'Spree::Store', foreign_key: 'store_id'-- scope :for_store, ->(store) { where(store: store) }- scope :active, -> { where(active: true) }-- before_validation :generate_slug-- with_options presence: true do- validates :store- validates :name, uniqueness: true- validates :slug, uniqueness: { scope: :store_id }- end-- def formatted_url- "#{store.formatted_url}/api/v3/store/feeds/#{slug}.xml"- end-- private-- def generate_slug- new_slug = slug.blank? ? SecureRandom.uuid : slug.parameterize- write_attribute(:slug, new_slug)- end-- class << self- def label- raise NotImplementedError- end-- def provider_name- raise NotImplementedError- end-- def presenter_class- raise NotImplementedError- end-- def available_types- Spree.data_feed_types- end- end- end-enddiff --git a/spree/core/app/models/spree/data_feed/google.rb b/spree/core/app/models/spree/data_feed/google.rbdeleted file mode 100644index 85b596b937..0000000000--- a/spree/core/app/models/spree/data_feed/google.rb+++ /dev/null@@ -1,19 +0,0 @@-require_dependency 'spree/data_feed'--module Spree- class DataFeed::Google < DataFeed- class << self- def label- 'Google Merchant Center Feed'- end-- def provider_name- 'google'- end-- def presenter_class- Spree::DataFeeds::GooglePresenter- end- end- end-enddiff --git a/spree/core/app/models/spree/store.rb b/spree/core/app/models/spree/store.rbindex 94487bff8b..dab1df7c12 100644--- a/spree/core/app/models/spree/store.rb+++ b/spree/core/app/models/spree/store.rb@@ -93,8 +93,6 @@ module Spreehas_many :wishlists, class_name: 'Spree::Wishlist'- has_many :data_feeds, class_name: 'Spree::DataFeed'-belongs_to :default_country, class_name: 'Spree::Country'belongs_to :checkout_zone, class_name: 'Spree::Zone'diff --git a/spree/core/app/presenters/spree/data_feeds/base_presenter.rb b/spree/core/app/presenters/spree/data_feeds/base_presenter.rbdeleted file mode 100644index 764d8aefa4..0000000000--- a/spree/core/app/presenters/spree/data_feeds/base_presenter.rb+++ /dev/null@@ -1,23 +0,0 @@-module Spree- module DataFeeds- class BasePresenter- def initialize(data_feed)- @data_feed = data_feed- @store = data_feed.store- end-- attr_reader :data_feed, :store-- # @return [String] the feed content (XML, CSV, etc.)- def call- raise NotImplementedError- end-- private-- def products- store.products.active- end- end- end-enddiff --git a/spree/core/app/presenters/spree/data_feeds/google_presenter.rb b/spree/core/app/presenters/spree/data_feeds/google_presenter.rbdeleted file mode 100644index 08bdbdfd21..0000000000--- a/spree/core/app/presenters/spree/data_feeds/google_presenter.rb+++ /dev/null@@ -1,94 +0,0 @@-require 'nokogiri'--module Spree- module DataFeeds- class GooglePresenter < BasePresenter- # @return [String] RSS XML feed for Google Merchant Center- def call- 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- build_store_info(xml)- build_items(xml)- end- end- end-- builder.to_xml- end-- private-- def build_store_info(xml)- xml.title store.name- xml.link store.url- xml.description store.meta_description- end-- def build_items(xml)- products.includes(:variants_including_master, :properties, :product_properties).find_each do |product|- product.variants_including_master.active.each do |variant|- next if variant.is_master? && product.has_variants?-- build_item(xml, product, variant)- end- end- end-- def build_item(xml, product, variant)- image_url = image_link(variant, product)- return if image_url.nil?-- xml.item do- build_required_attributes(xml, product, variant, image_url)- build_optional_attributes(xml, product)- end- end-- def build_required_attributes(xml, product, variant, image_url)- xml['g'].id variant.id- xml['g'].item_group_id product.id- xml['g'].title format_title(product, variant)- xml['g'].description product.description || format_title(product, variant)- xml['g'].link "#{store.url}/products/#{product.slug}"- xml['g'].image_link image_url- xml['g'].price "#{variant.price} #{variant.cost_currency}"- xml['g'].availability availability(product)- xml['g'].availability_date product.available_on.xmlschema if product.available_on.present?- end-- def build_optional_attributes(xml, product)- product.product_properties.includes(:property).each do |product_property|- name = product_property.property&.name- value = product_property.value-- next if name.blank? || value.blank?-- xml['g'].send(name, value)- end- end-- def format_title(product, variant)- parts = [product.name]- variant.option_values.each do |option_value|- parts << option_value.name- end- parts.join(' - ')- end-- def image_link(variant, product)- image = variant.thumbnail || product.thumbnail- return if image.nil?-- Rails.application.routes.url_helpers.cdn_image_url(image.attachment.variant(:xlarge))- end-- def availability(product)- return 'in stock' if product.available? && (product.available_on.nil? || product.available_on.past?)- return 'backorder' if product.backorderable? && product.backordered? && product.available_on&.future?-- 'out of stock'- end- end- end-enddiff --git a/spree/core/db/migrate/20221229132350_create_spree_data_feed_settings.rb b/spree/core/db/migrate/20221229132350_create_spree_data_feed_settings.rbdeleted file mode 100644index f5d791da0a..0000000000--- a/spree/core/db/migrate/20221229132350_create_spree_data_feed_settings.rb+++ /dev/null@@ -1,14 +0,0 @@-class CreateSpreeDataFeedSettings < ActiveRecord::Migration[6.0]- def change- create_table :spree_data_feed_settings do |t|- t.references :spree_store-- t.string :name- t.string :provider- t.string :uuid, unique: true- t.boolean :enabled, default: true-- t.timestamps- end- end-enddiff --git a/spree/core/db/migrate/20230415155958_rename_data_feed_settings_table.rb b/spree/core/db/migrate/20230415155958_rename_data_feed_settings_table.rbdeleted file mode 100644index 560acdf604..0000000000--- a/spree/core/db/migrate/20230415155958_rename_data_feed_settings_table.rb+++ /dev/null@@ -1,5 +0,0 @@-class RenameDataFeedSettingsTable < ActiveRecord::Migration[6.1]- def change- rename_table :spree_data_feed_settings, :spree_data_feeds- end-enddiff --git a/spree/core/db/migrate/20230415160828_rename_data_feed_table_columns.rb b/spree/core/db/migrate/20230415160828_rename_data_feed_table_columns.rbdeleted file mode 100644index f07e8f7442..0000000000--- a/spree/core/db/migrate/20230415160828_rename_data_feed_table_columns.rb+++ /dev/null@@ -1,7 +0,0 @@-class RenameDataFeedTableColumns < ActiveRecord::Migration[6.1]- def change- rename_column :spree_data_feeds, :spree_store_id, :store_id- rename_column :spree_data_feeds, :enabled, :active- rename_column :spree_data_feeds, :uuid, :slug- end-enddiff --git a/spree/core/db/migrate/20230415161226_add_indexes_to_data_feeds_table.rb b/spree/core/db/migrate/20230415161226_add_indexes_to_data_feeds_table.rbdeleted file mode 100644index 4a4d2e5599..0000000000--- a/spree/core/db/migrate/20230415161226_add_indexes_to_data_feeds_table.rb+++ /dev/null@@ -1,5 +0,0 @@-class AddIndexesToDataFeedsTable < ActiveRecord::Migration[6.1]- def change- add_index :spree_data_feeds, [:store_id, :slug, :provider]- end-enddiff --git a/spree/core/db/migrate/20230512094803_rename_data_feeds_column_provider_to_type.rb b/spree/core/db/migrate/20230512094803_rename_data_feeds_column_provider_to_type.rbdeleted file mode 100644index 9b2a8e1248..0000000000--- a/spree/core/db/migrate/20230512094803_rename_data_feeds_column_provider_to_type.rb+++ /dev/null@@ -1,5 +0,0 @@-class RenameDataFeedsColumnProviderToType < ActiveRecord::Migration[6.1]- def change- rename_column :spree_data_feeds, :provider, :type- end-enddiff --git a/spree/core/lib/spree/core.rb b/spree/core/lib/spree/core.rbindex 5d4096cff0..eab9e38779 100644--- a/spree/core/lib/spree/core.rb+++ b/spree/core/lib/spree/core.rb@@ -242,14 +242,6 @@ module SpreeRails.application.config.spree.line_item_comparison_hooks = valueend- def self.data_feed_types- Rails.application.config.spree.data_feed_types- end-- def self.data_feed_types=(value)- Rails.application.config.spree.data_feed_types = value- end-def self.export_typesRails.application.config.spree.export_typesenddiff --git a/spree/core/lib/spree/core/engine.rb b/spree/core/lib/spree/core/engine.rbindex dff1790450..e3b5099b91 100644--- a/spree/core/lib/spree/core/engine.rb+++ b/spree/core/lib/spree/core/engine.rb@@ -14,7 +14,6 @@ module Spree:promotions,:pricing,:line_item_comparison_hooks,- :data_feed_types,:export_types,:import_types,:taxon_rules,@@ -176,10 +175,6 @@ module SpreePromotion::Actions::FreeShipping]- Rails.application.config.spree.data_feed_types = [- Spree::DataFeed::Google- ]-Rails.application.config.spree.export_types = [Spree::Exports::Products,Spree::Exports::Orders,diff --git a/spree/core/lib/spree/testing_support/factories/google_data_feed_factory.rb b/spree/core/lib/spree/testing_support/factories/google_data_feed_factory.rbdeleted file mode 100644index 20a5b996cc..0000000000--- a/spree/core/lib/spree/testing_support/factories/google_data_feed_factory.rb+++ /dev/null@@ -1,7 +0,0 @@-FactoryBot.define do- factory :google_data_feed, class: Spree::DataFeed::Google do- active { true }- association :store, factory: :store- name { 'test' }- end-enddiff --git a/spree/core/spec/lib/spree/core_environment_spec.rb b/spree/core/spec/lib/spree/core_environment_spec.rbindex f43e3ec84f..f63bceb805 100644--- a/spree/core/spec/lib/spree/core_environment_spec.rb+++ b/spree/core/spec/lib/spree/core_environment_spec.rb@@ -38,11 +38,6 @@ RSpec.describe 'Spree environment accessors' doexpect(Spree.line_item_comparison_hooks).to eq(Rails.application.config.spree.line_item_comparison_hooks)end- it 'provides access to data_feed_types' do- expect(Spree.data_feed_types).to eq(Rails.application.config.spree.data_feed_types)- expect(Spree.data_feed_types).to be_an(Array)- end-it 'provides access to export_types' doexpect(Spree.export_types).to eq(Rails.application.config.spree.export_types)expect(Spree.export_types).to be_an(Array)diff --git a/spree/core/spec/models/spree/data_feed/google_spec.rb b/spree/core/spec/models/spree/data_feed/google_spec.rbdeleted file mode 100644index feb84efcf5..0000000000--- a/spree/core/spec/models/spree/data_feed/google_spec.rb+++ /dev/null@@ -1,44 +0,0 @@-require 'spec_helper'--describe Spree::DataFeed::Google, type: :model do- let(:store) { create(:store, url: 'http://store.test') }-- describe '#create' do- context 'when slug is not provided' do- let(:data_feed) { create(:google_data_feed, store: store, slug: nil) }-- it 'generates slug automatically' do- expect(data_feed.slug).not_to be_empty- end- end-- context 'when slug is provided' do- let(:data_feed) { create(:google_data_feed, store: store, slug: 'test-slug') }-- it 'uses the slug provided' do- expect(data_feed.slug).to eq('test-slug')- end- end- end-- describe '#formatted_url' do- let(:data_feed) { create(:google_data_feed, store: store, slug: 'test-feed') }- let(:expected_url) { 'http://store.test/api/v3/store/feeds/test-feed.xml' }-- before do- allow(Rails.application.routes).to receive(:default_url_options).and_return(protocol: 'http', port: nil)- end-- it 'returns full url to the data feed' do- expect(data_feed.formatted_url).to eq(expected_url)- end- end-- describe '.label' do- subject { Spree::DataFeed::Google.label }-- it 'returns a descriptive label' do- expect(subject).to eq('Google Merchant Center Feed')- end- end-enddiff --git a/spree/core/spec/presenters/spree/data_feeds/google_presenter_spec.rb b/spree/core/spec/presenters/spree/data_feeds/google_presenter_spec.rbdeleted file mode 100644index 7ed1f9353c..0000000000--- a/spree/core/spec/presenters/spree/data_feeds/google_presenter_spec.rb+++ /dev/null@@ -1,130 +0,0 @@-require 'spec_helper'--RSpec.describe Spree::DataFeeds::GooglePresenter do- let(:store) { @default_store }- let(:data_feed) { create(:google_data_feed, store: store) }- let(:product) { create(:product, stores: [store]) }- let!(:variant) { create(:with_image_variant, product: product) }-- subject { described_class.new(data_feed) }-- describe '#call' do- let(:xml) { subject.call }-- context 'store information' do- it 'includes store name' do- expect(xml).to include("<title>#{store.name}</title>")- end-- it 'includes store url' do- expect(xml).to include("<link>#{store.url}</link>")- end-- it 'includes store description' do- expect(xml).to include("<description>#{store.meta_description}</description>")- end- end-- context 'required attributes' do- it 'includes variant id' do- expect(xml).to include("<g:id>#{variant.id}</g:id>")- end-- it 'includes product id as item_group_id' do- expect(xml).to include("<g:item_group_id>#{product.id}</g:item_group_id>")- end-- it 'includes title with variant option values' do- expect(xml).to include("<g:title>#{product.name} - #{variant.option_values.first.name}</g:title>")- end-- it 'includes description' do- expect(xml).to include("<g:description>#{product.description}</g:description>")- end-- it 'includes link' do- expect(xml).to include("<g:link>#{store.url}/products/#{product.slug}</g:link>")- end-- it 'includes image link' do- image = variant.images.first- expected_url = Rails.application.routes.url_helpers.cdn_image_url(image.attachment.variant(:xlarge))- expect(xml).to include("<g:image_link>#{expected_url}</g:image_link>")- end-- it 'includes price' do- expect(xml).to include("<g:price>#{variant.price} #{variant.cost_currency}</g:price>")- end- end-- context 'availability' do- it 'shows in stock for available products' do- expect(xml).to include('<g:availability>in stock</g:availability>')- end-- it 'includes availability date' do- expect(xml).to include("<g:availability_date>#{product.available_on.xmlschema}</g:availability_date>")- end-- context 'when product is set to backorderable' do- let(:product) { create(:product, stores: [store], available_on: 1.year.from_now) }-- it 'shows backorder' do- expect(xml).to include('<g:availability>backorder</g:availability>')- end- end-- context 'when availability date is nil' do- let(:product) { create(:product, stores: [store], available_on: nil) }-- it 'shows in stock' do- expect(xml).to include('<g:availability>in stock</g:availability>')- end-- it 'does not include availability date' do- expect(xml).not_to include('<g:availability_date>')- end- end- end-- context 'product with only master variant' do- let(:product) { create(:product, stores: [store]) }- let!(:variant) { nil }-- before do- product.master.images << create(:image)- end-- it 'includes master variant in feed' do- expect(xml).to include("<g:id>#{product.master.id}</g:id>")- end-- it 'includes product name as title' do- expect(xml).to include("<g:title>#{product.name}</g:title>")- end- end-- context 'optional attributes from product properties' do- let(:product) { create(:product_with_properties, stores: [store]) }-- it 'includes product properties' do- expect(xml).to include("<g:brand>#{product.property('brand')}</g:brand>")- end- end-- context 'title generation does not mutate product name' do- it 'generates independent titles for multiple variants' do- option_type = create(:option_type)- option_a = create(:option_value, name: 'option-a', option_type: option_type)- option_b = create(:option_value, name: 'option-b', option_type: option_type)-- create(:with_image_variant, product: product, option_values: [option_a])- create(:with_image_variant, product: product, option_values: [option_b])-- expect(xml).to include("#{product.name} - option-a</g:title>")- expect(xml).to include("#{product.name} - option-b</g:title>")- expect(xml).not_to include('option-a - option-b')- end- end- end-end