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.

Open-source `Stores::Setup` and `Stores::Socials`

Damian Legawiece99938de9e0a
diff --git a/admin/config/locales/en.yml b/admin/config/locales/en.yml
index e692558a22..23c48437d3 100644
--- a/admin/config/locales/en.yml
+++ b/admin/config/locales/en.yml
@@ -55,6 +55,20 @@ en:
digital_asset_link_expire_time:
download_link_expire_time: Download Link Expire Time
link_expire_time: Set the expire time of each download link. (seconds).
+ store_setup_tasks:
+ add_billing_address: Add billing address
+ setup_taxes_collection: Setup taxes collection
+ add_products: Add products
+ set_customer_support_email: Set customer support email
+ payment_methods:
+ title: Please setup Payment Methods for your store
+ done: You're all set! You can always manage your Payment Methods on <a href="%{link}" data-turbo-frame="_top">this</a> page.
+ copy: In order to collect payments, please setup at least one online payment provider.
+ setup: Setup Payment Methods
+ taxes:
+ done: You're all set! You can always manage your taxes on the <a href="%{link}" data-turbo-frame="_top">settings</a> page.
+ copy: In order to collect taxes, please setup your tax rates.
+ setup: Setup Tax Rates
product_kinds:
physical: Physical
digital: Digital
diff --git a/core/app/models/concerns/spree/stores/setup.rb b/core/app/models/concerns/spree/stores/setup.rb
new file mode 100644
index 0000000000..e385ab403f
--- /dev/null
+++ b/core/app/models/concerns/spree/stores/setup.rb
@@ -0,0 +1,52 @@
+module Spree
+ module Stores
+ module Setup
+ extend ActiveSupport::Concern
+
+ def setup_task_done?(task)
+ case task
+ when :setup_payment_method
+ payment_method_setup?
+ when :setup_taxes_collection
+ Spree::TaxRate.any?
+ when :add_products
+ products.any?
+ when :set_customer_support_email
+ customer_support_email.present?
+ end
+ end
+
+ def setup_tasks_total
+ @setup_tasks_total = setup_tasks_list.count
+ end
+
+ def setup_tasks_list
+ return [] if deleted?
+
+ @setup_tasks_list = []
+ @setup_tasks_list << :setup_payment_method
+ @setup_tasks_list << :add_products
+ @setup_tasks_list << :set_customer_support_email
+ @setup_tasks_list << :setup_taxes_collection
+
+ @setup_tasks_list
+ end
+
+ def setup_tasks_done
+ @setup_tasks_done = setup_tasks_list.select { |task| setup_task_done?(task) }.count
+ end
+
+ def setup_completed?
+ @setup_completed ||= setup_tasks_done == setup_tasks_total
+ end
+
+ def setup_percentage
+ @setup_percentage ||= (setup_tasks_done / setup_tasks_total.to_f * 100).to_i
+ end
+
+ def payment_method_setup?
+ payment_methods.active.where.not(type: Spree::PaymentMethod::StoreCredit.to_s).any?
+ end
+ end
+ end
+end
diff --git a/core/app/models/concerns/spree/stores/socials.rb b/core/app/models/concerns/spree/stores/socials.rb
new file mode 100644
index 0000000000..5321b4b3a8
--- /dev/null
+++ b/core/app/models/concerns/spree/stores/socials.rb
@@ -0,0 +1,68 @@
+module Spree
+ module Stores
+ module Socials
+ extend ActiveSupport::Concern
+
+ SUPPORTED_SOCIAL_NETWORKS = %w[instagram facebook twitter pinterest tiktok youtube spotify].freeze
+
+ SOCIAL_NETWORKS_CONFIG = {
+ twitter: {
+ input_placeholder: 'https://twitter.com/your_handle',
+ profile_link: 'https://twitter.com/your_handle'
+ },
+ instagram: {
+ input_placeholder: 'https://www.instagram.com/your_handle',
+ profile_link: 'https://www.instagram.com/your_handle'
+ },
+ facebook: {
+ input_placeholder: 'https://www.facebook.com/your_page',
+ profile_link: 'https://www.facebook.com/your_page'
+ },
+ youtube: {
+ input_placeholder: 'https://www.youtube.com/@your_channel',
+ profile_link: 'https://www.youtube.com/@your_channel'
+ },
+ pinterest: {
+ input_placeholder: 'https://pinterest.com/your_handle',
+ profile_link: 'https://pinterest.com/your_handle'
+ },
+ tiktok: {
+ input_placeholder: 'your_handle',
+ profile_link: 'https://www.tiktok.com/@your_handle'
+ },
+ spotify: {
+ input_placeholder: 'https://open.spotify.com/user/your_handle',
+ profile_link: 'https://open.spotify.com/user/your_handle'
+ }
+ }.freeze
+
+ included do
+ # generate methods for social links
+ SUPPORTED_SOCIAL_NETWORKS.each do |social|
+ # store the social handle in the public metadata
+ store_accessor :public_metadata, social
+
+ define_method "#{social}_link" do
+ return if send(social).blank?
+
+ send(social).match(/http/) ? send(social) : SOCIAL_NETWORKS_CONFIG[social.to_sym][:profile_link].gsub(/your_handle|your_page|your_channel/, send(social).sub(/^\//, '').sub(/^@/, ''))
+ end
+
+ define_method "#{social}_handle" do
+ return if send(social).blank?
+
+ (send(social).match(/http/) ? send(social).split('/').last : send(social)).sub(/^\//, '').gsub('@', '').split('?').first
+ end
+ end
+ end
+
+ def social_handle
+ @social_handle ||= instagram_handle || youtube_handle || tiktok_handle
+ end
+
+ def social_links
+ @social_links ||= [instagram_link, facebook_link, twitter_link, pinterest_link, youtube_link, tiktok_link, spotify_link].compact_blank
+ end
+ end
+ end
+end
diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rb
index d0c73a85fb..14ca8af92a 100644
--- a/core/app/models/spree/store.rb
+++ b/core/app/models/spree/store.rb
@@ -3,6 +3,8 @@ module Spree
include FriendlyId
include Spree::TranslatableResource
include Spree::Metadata
+ include Spree::Stores::Setup
+ include Spree::Stores::Socials
include Spree::Webhooks::HasWebhooks if defined?(Spree::Webhooks::HasWebhooks)
include Spree::Security::Stores if defined?(Spree::Security::Stores)
diff --git a/core/lib/spree/core/controller_helpers/strong_parameters.rb b/core/lib/spree/core/controller_helpers/strong_parameters.rb
index 835750752f..cf83ae4167 100644
--- a/core/lib/spree/core/controller_helpers/strong_parameters.rb
+++ b/core/lib/spree/core/controller_helpers/strong_parameters.rb
@@ -37,6 +37,10 @@ module Spree
product_properties_attributes: permitted_product_properties_attributes
]
end
+
+ def permitted_store_attributes
+ permitted_attributes.store_attributes + Spree::Store::SUPPORTED_SOCIAL_NETWORKS.map { |social| "store_#{social}" }
+ end
end
end
end
diff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rb
index 40d81896c0..29558095e9 100644
--- a/core/lib/spree/permitted_attributes.rb
+++ b/core/lib/spree/permitted_attributes.rb
@@ -132,9 +132,8 @@ module Spree
@@store_attributes = [:name, :url, :seo_title, :code, :meta_keywords,
:meta_description, :default_currency, :mail_from_address,
- :customer_support_email, :facebook, :twitter, :instagram,
- :description, :address, :contact_phone, :supported_locales,
- :default_locale, :default_country_id, :supported_currencies,
+ :customer_support_email, :description, :address, :contact_phone,
+ :supported_locales, :default_locale, :default_country_id, :supported_currencies,
:new_order_notifications_email, :checkout_zone_id, :seo_robots,
:preferred_timezone, :preferred_weight_unit, :preferred_unit_system,
:preferred_digital_asset_authorized_clicks, :preferred_digital_asset_authorized_days,
diff --git a/core/spec/lib/spree/core/controller_helpers/strong_parameters_spec.rb b/core/spec/lib/spree/core/controller_helpers/strong_parameters_spec.rb
index f72f5fe733..384479cbc0 100644
--- a/core/spec/lib/spree/core/controller_helpers/strong_parameters_spec.rb
+++ b/core/spec/lib/spree/core/controller_helpers/strong_parameters_spec.rb
@@ -36,4 +36,10 @@ describe Spree::Core::ControllerHelpers::StrongParameters, type: :controller do
expect(controller.permitted_product_attributes.class).to eq Array
end
end
+
+ describe '#permitted_store_attributes' do
+ it 'returns Array class' do
+ expect(controller.permitted_store_attributes.class).to eq Array
+ end
+ end
end