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.

Refactor services to be clearer.

wjwiteka26e56915b29
diff --git a/api/app/controllers/spree/api/v2/storefront/stores_controller.rb b/api/app/controllers/spree/api/v2/storefront/stores_controller.rb
index fb480b110e..009e68678f 100644
--- a/api/app/controllers/spree/api/v2/storefront/stores_controller.rb
+++ b/api/app/controllers/spree/api/v2/storefront/stores_controller.rb
@@ -3,23 +3,22 @@ module Spree
module V2
module Storefront
class StoresController < ::Spree::Api::V2::ResourceController
- before_action :update_options
-
def current
render_serialized_payload { serialize_resource(current_store) }
end
def export_google_rss_feed
- send_data export_google_rss, filename: 'products.rss', type: 'text/xml'
+ send_data export_google_rss_service.value[:file], filename: 'products.rss', type: 'text/xml'
end
private
- def update_options
+ def update_settings
@settings = Spree::GoogleFeedSetting.find_by(spree_store_id: current_store)
end
- def export_google_rss
+ def export_google_rss_service
+ update_settings
Spree::Dependencies.export_google_rss_service.constantize.new.call(@settings)
end
diff --git a/api/spec/controllers/spree/api/v2/storefront/store_controller_spec.rb b/api/spec/controllers/spree/api/v2/storefront/store_controller_spec.rb
deleted file mode 100644
index 07e8df11f8..0000000000
--- a/api/spec/controllers/spree/api/v2/storefront/store_controller_spec.rb
+++ /dev/null
@@ -1,2 +0,0 @@
-# frozen_string_literal: true
-
diff --git a/core/app/models/spree/google_feed_setting.rb b/core/app/models/spree/google_feed_setting.rb
index ce312be93c..ede387c2c5 100644
--- a/core/app/models/spree/google_feed_setting.rb
+++ b/core/app/models/spree/google_feed_setting.rb
@@ -8,7 +8,7 @@ module Spree
keys = []
attributes.each do |key, value|
- if value.instance_of?(TrueClass)
+ if value == true
keys.append(key.to_sym)
end
end
diff --git a/core/app/services/spree/export/google_rss.rb b/core/app/services/spree/export/google_rss.rb
index b0886eb7c0..6529f48110 100644
--- a/core/app/services/spree/export/google_rss.rb
+++ b/core/app/services/spree/export/google_rss.rb
@@ -3,15 +3,18 @@ require 'nokogiri'
module Spree
module Export
class GoogleRss
- def call(options)
- @store = Spree::Store.find(options.spree_store_id)
- @options = options
+ prepend Spree::ServiceModule::Base
+
+ def call(settings)
+ @settings = settings
+
+ return failure(store, error: "Store with id: #{settings.spree_store_id} does not exist.") if store.nil?
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|
+ store.products.active.each do |product|
product.variants.active.each do |variant|
add_variant_information_to_xml(xml, product, variant)
end
@@ -20,15 +23,21 @@ module Spree
end
end
- builder.to_xml
+ success(file: builder.to_xml)
end
private
+ def store
+ return @store if defined? @store
+
+ @store ||= Spree::Store.find_by(id: @settings.spree_store_id)
+ end
+
def add_store_information_to_xml(xml)
- xml.title @store.name
- xml.link @store.url
- xml.description @store.meta_description
+ xml.title store.name
+ xml.link store.url
+ xml.description store.meta_description
end
def add_variant_information_to_xml(xml, product, variant)
@@ -38,7 +47,7 @@ module Spree
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'].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)
@@ -88,14 +97,14 @@ module Spree
end
def add_optional_information(xml, product)
- @options.enabled_keys.each do |key|
- if @options.send(key) && !product.property(key.to_s).nil?
+ @settings.enabled_keys.each do |key|
+ if @settings.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
+ # example of modifying 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.
diff --git a/core/spec/services/spree/export/google_rss_spec.rb b/core/spec/services/spree/export/google_rss_spec.rb
index 0e12f04cde..7e3b9ba4e6 100644
--- a/core/spec/services/spree/export/google_rss_spec.rb
+++ b/core/spec/services/spree/export/google_rss_spec.rb
@@ -16,16 +16,16 @@ module Spree
allow(subject).to receive(:store).and_return(store)
end
- it 'store name' do
- expect(result).to include("<title>#{store.name}</title>").once
+ it 'include store name' do
+ expect(result.value[:file]).to include("<title>#{store.name}</title>").once
end
- it 'store url' do
- expect(result).to include("<link>#{store.url}</link>").once
+ it 'includes store url' do
+ expect(result.value[:file]).to include("<link>#{store.url}</link>").once
end
- it 'store description' do
- expect(result).to include("<description>#{store.meta_description}</description>").once
+ it 'includes store description' do
+ expect(result.value[:file]).to include("<description>#{store.meta_description}</description>").once
end
end
@@ -34,57 +34,57 @@ module Spree
allow(subject).to receive(:store).and_return(store)
end
- it 'id' do
- expect(result).to include("<g:id>#{variant.id}</g:id>").once
+ it 'includes id' do
+ expect(result.value[:file]).to include("<g:id>#{variant.id}</g:id>").once
end
- it 'title' do
- expect(result).to include("<g:title>#{product.name} - #{variant.option_values.first.name}</g:title>").once
+ it 'includes title' do
+ expect(result.value[:file]).to include("<g:title>#{product.name} - #{variant.option_values.first.name}</g:title>").once
end
- it 'description' do
- expect(result).to include("<g:description>#{product.description}</g:description>").once
+ it 'includes description' do
+ expect(result.value[:file]).to include("<g:description>#{product.description}</g:description>").once
end
- it 'link' do
- expect(result).to include("<g:link>#{store.url}/#{product.slug}</g:link>").once
+ it 'includes link' do
+ expect(result.value[:file]).to include("<g:link>#{store.url}/#{product.slug}</g:link>").once
end
- it 'image link' do
- expect(result).to include("<g:image_link>#{variant.images.first.plp_url}</g:image_link>").once
+ it 'includes image link' do
+ expect(result.value[:file]).to include("<g:image_link>#{variant.images.first.plp_url}</g:image_link>").once
end
- it 'price' do
- expect(result).to include("<g:price>#{variant.price} #{variant.cost_currency}</g:price>").once
+ it 'includes price' do
+ expect(result.value[:file]).to include("<g:price>#{variant.price} #{variant.cost_currency}</g:price>").once
end
context 'availability date is in the past' do
- it 'product is in stock' do
- expect(result).to include('<g:availability>in stock</g:availability>')
+ it 'shows that product is in stock' do
+ expect(result.value[:file]).to include('<g:availability>in stock</g:availability>')
end
- it 'product availability date is the same' do
- expect(result).to include("<g:availability_date>#{product.available_on.xmlschema}</g:availability_date>")
+ it 'shows that product availability date is the same' do
+ expect(result.value[:file]).to include("<g:availability_date>#{product.available_on.xmlschema}</g:availability_date>")
end
end
context 'availability date is in the future' do
let(:product) { create(:product, stores: [store], available_on: 1.year.from_now) }
- it 'product is on backorder' do
- expect(result).to include('<g:availability>backorder</g:availability>')
+ it 'shows that product is on backorder' do
+ expect(result.value[:file]).to include('<g:availability>backorder</g:availability>')
end
end
context 'availability date is nil' do
let(:product) { create(:product, stores: [store], available_on: nil) }
- it 'product is out of stock' do
- expect(result).to include('<g:availability>out of stock</g:availability>')
+ it 'shows that product is out of stock' do
+ expect(result.value[:file]).to include('<g:availability>out of stock</g:availability>')
end
- it 'product availability date is nil' do
- expect(result).not_to include('<g:availability_date>')
+ it 'shows that product availability date is nil' do
+ expect(result.value[:file]).not_to include('<g:availability_date>')
end
end
end
@@ -97,14 +97,14 @@ module Spree
end
it 'adds brand to item attributes' do
- expect(result).to include("<g:brand>#{product.property('brand')}</g:brand>")
+ expect(result.value[:file]).to include("<g:brand>#{product.property('brand')}</g:brand>")
end
context 'brand option is set to false' do
let(:setting) { create(:google_feed_setting, store: store, brand: false) }
it 'does not add brand to item attributes' do
- expect(result).not_to include('<g:brand>')
+ expect(result.value[:file]).not_to include('<g:brand>')
end
end
end