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.

allows assigning Products to Stores

Łukasz Adamczyk24ac392075ea
diff --git a/backend/app/controllers/spree/admin/products_controller.rb b/backend/app/controllers/spree/admin/products_controller.rb
index 49102eeda1..ac69037808 100644
--- a/backend/app/controllers/spree/admin/products_controller.rb
+++ b/backend/app/controllers/spree/admin/products_controller.rb
@@ -4,6 +4,8 @@ module Spree
helper 'spree/products'
before_action :load_data, except: :index
+ before_action :load_stores, only: [:edit, :update]
+
create.before :create_before
update.before :update_before
helper_method :clone_object_url
@@ -105,6 +107,10 @@ module Spree
@shipping_categories = ShippingCategory.order(:name)
end
+ def load_stores
+ @stores = Spree::Store.pluck(:name, :id)
+ end
+
def collection
return @collection if @collection.present?
diff --git a/backend/app/views/spree/admin/products/_form.html.erb b/backend/app/views/spree/admin/products/_form.html.erb
index 7147e8fe95..d868ae31cd 100644
--- a/backend/app/views/spree/admin/products/_form.html.erb
+++ b/backend/app/views/spree/admin/products/_form.html.erb
@@ -249,6 +249,26 @@
<% end %>
</div>
+ <p data-hook="admin_product_form_stores" class="omega two columns">
+ <%= f.field_container :stores, class: ['form-group'] do %>
+ <%= f.label :store_ids, Spree.t(:stores) %>
+ <% if can? :modify, Spree::Store %>
+ <%= f.select :store_ids, @stores, { include_blank: true }, { class: 'select2-clear', multiple: true, "data-placeholder" => Spree.t(:select_stores) } %>
+ <% elsif @product.stores.any? %>
+ <ul class="text_list">
+ <% @product.stores.each do |store| %>
+ <li><%= store.name %></li>
+ <% end %>
+ </ul>
+ <% else %>
+ <div class="alert alert-info">
+ <%= Spree.t(:no_resource_found, resource: :stores) %>
+ </div>
+ <% end %>
+ <% end %>
+ </p>
+
+
<div data-hook="admin_product_form_additional_fields"></div>
</div>
diff --git a/backend/app/views/spree/admin/products/index.html.erb b/backend/app/views/spree/admin/products/index.html.erb
index 62ddae7bd1..4e30c48a7c 100644
--- a/backend/app/views/spree/admin/products/index.html.erb
+++ b/backend/app/views/spree/admin/products/index.html.erb
@@ -39,6 +39,12 @@
</label>
</div>
</div>
+ <div class="col-12 col-lg-6">
+ <div class="form-group">
+ <label for="stores_id_in"><%= Spree.t(:store) %></label>
+ <%= f.select :stores_id_in, Spree::Store.pluck(:name, :id), {include_blank: true}, class: 'select2-clear', multiple: true %></p>
+ </div>
+ </div>
</div>
<div data-hook="admin_products_index_search_buttons" class="form-actions">
<%= button Spree.t(:search), 'search.svg' %>
@@ -59,6 +65,7 @@
<th scope="col" class="text-center">
<%= sort_link @search, :master_default_price_amount, Spree.t(:master_price), {}, {title: 'admin_products_listing_price_title'} %>
</th>
+ <th><%= Spree.t(:stores) %></th>
<th scope="col" data-hook="admin_products_index_header_actions" class="actions"></th>
</tr>
</thead>
@@ -75,6 +82,12 @@
<td class="sku"><%= product.sku rescue '' %></td>
<td class="status"><%= active_badge(available_status == Spree.t(:available), label: available_status) %></td>
<td class="text-center"><%= display_price(product) %></td>
+ <td>
+ <% product.stores.pluck(:name).each do |store_name| %>
+ <dd><small><%= store_name %></small></dd>
+ <% end %>
+ </td>
+
<td class="actions " data-hook="admin_products_index_row_actions">
<span class="d-flex justify-content-end">
<%= link_to_edit product, no_text: true, class: 'edit' if can?(:edit, product) && !product.deleted? %>
diff --git a/backend/spec/features/admin/products/edit/stores_spec.rb b/backend/spec/features/admin/products/edit/stores_spec.rb
new file mode 100644
index 0000000000..263cf54b21
--- /dev/null
+++ b/backend/spec/features/admin/products/edit/stores_spec.rb
@@ -0,0 +1,27 @@
+require 'spec_helper'
+
+describe 'Product Stores', type: :feature, js: true do
+ stub_authorization!
+
+ before do
+ create(:product)
+ create(:store, name: 'First store', code: 'first')
+ create(:store, name: 'Second store', code: 'second')
+
+ visit spree.admin_products_path
+
+ within_row(1) { click_icon :edit }
+ end
+
+ it 'allows assigning products' do
+ select2 'First store', from: 'Stores'
+ select2 'Second store', from: 'Stores'
+
+ click_button 'Update'
+
+ expect(page).to have_content('successfully updated')
+
+ expect(page).to have_content('First store')
+ expect(page).to have_content('Second store')
+ end
+end
diff --git a/backend/spec/spec_helper.rb b/backend/spec/spec_helper.rb
index 0a3d4ced85..2869c822f5 100644
--- a/backend/spec/spec_helper.rb
+++ b/backend/spec/spec_helper.rb
@@ -31,6 +31,7 @@ require 'rspec/rails'
# in ./support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
+
require 'capybara-select-2'
require 'database_cleaner'
require 'ffaker'
@@ -51,6 +52,8 @@ require 'spree/testing_support/flatpickr_capybara'
require 'spree/core/controller_helpers/strong_parameters'
require 'webdrivers'
+Capybara.javascript_driver = :selenium_chrome
+
RSpec.configure do |config|
config.color = true
config.default_formatter = 'doc'
diff --git a/core/app/models/concerns/spree/product_scopes.rb b/core/app/models/concerns/spree/product_scopes.rb
index ff786eb3f3..7298b0a698 100644
--- a/core/app/models/concerns/spree/product_scopes.rb
+++ b/core/app/models/concerns/spree/product_scopes.rb
@@ -62,6 +62,10 @@ module Spree
where("#{price_table_name}.amount >= ?", price)
end
+ add_search_scope :by_store do |store|
+ joins(:stores).where(Spree::StoreProduct.table_name => { store_id: store.id })
+ end
+
# This scope selects products in taxon AND all its descendants
# If you need products only within one taxon use
#
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index 4fab91c901..fdfb77e652 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -79,6 +79,9 @@ module Spree
has_many :variant_images, -> { order(:position) }, source: :images, through: :variants_including_master
has_many :variant_images_without_master, -> { order(:position) }, source: :images, through: :variants
+ has_many :store_products, class_name: 'Spree::StoreProduct', dependent: :destroy
+ has_many :stores, through: :store_products, class_name: 'Spree::Store'
+
after_create :add_associations_from_prototype
after_create :build_variants_from_option_values_hash, if: :option_values_hash
diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rb
index c952c6a635..7a1cbfdf4c 100644
--- a/core/app/models/spree/store.rb
+++ b/core/app/models/spree/store.rb
@@ -8,6 +8,9 @@ module Spree
has_many :menus
+ has_many :store_products, class_name: 'Spree::StoreProduct', dependent: :destroy
+ has_many :products, through: :store_products, class_name: 'Spree::Product'
+
belongs_to :default_country, class_name: 'Spree::Country'
belongs_to :checkout_zone, class_name: 'Spree::Zone'
diff --git a/core/app/models/spree/store_product.rb b/core/app/models/spree/store_product.rb
new file mode 100644
index 0000000000..17f073eb29
--- /dev/null
+++ b/core/app/models/spree/store_product.rb
@@ -0,0 +1,11 @@
+module Spree
+ class StoreProduct < Spree::Base
+ self.table_name = 'spree_products_stores'
+
+ belongs_to :store, class_name: 'Spree::Store'
+ belongs_to :product, class_name: 'Spree::Product', touch: true
+
+ validates :store, :product, presence: true
+ validates :store_id, uniqueness: { scope: :product_id }
+ end
+end
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index cdad615440..50e2c8715c 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -1538,6 +1538,7 @@ en:
select_a_store_credit_reason: Select a reason for the store credit
select_an_option: Select an option
select_stock: Select stock
+ select_stores: Select Store(s)
selected_quantity_not_available: ! 'selected of %{item} is not available.'
send_copy_of_all_mails_to: Send Copy of All Mails To
send_mails_as: Send Mails As
diff --git a/core/db/migrate/20210527094055_create_spree_products_stores.rb b/core/db/migrate/20210527094055_create_spree_products_stores.rb
new file mode 100644
index 0000000000..e41100b376
--- /dev/null
+++ b/core/db/migrate/20210527094055_create_spree_products_stores.rb
@@ -0,0 +1,17 @@
+class CreateSpreeProductsStores < ActiveRecord::Migration[5.2]
+ def change
+ create_table :spree_products_stores, id: :serial do |t|
+ t.references :product, index: true
+ t.references :store, index: true
+ t.timestamps
+ end
+
+ add_index :spree_products_stores, [:product_id, :store_id], unique: true
+
+ default_store_id = Spree::Store.default.id
+ prepared_values = Spree::Product.with_deleted.order(:id).ids.map { |id| "(#{id}, #{default_store_id}, '#{Time.current.to_s(:db)}', '#{Time.current.to_s(:db)}')" }.join(', ')
+ return if prepared_values.empty?
+
+ execute "INSERT INTO spree_products_stores (product_id, store_id, created_at, updated_at) VALUES #{prepared_values};"
+ end
+end
diff --git a/core/spec/models/spree/product/scopes_spec.rb b/core/spec/models/spree/product/scopes_spec.rb
index 7d7e76822d..6c60f6aca1 100644
--- a/core/spec/models/spree/product/scopes_spec.rb
+++ b/core/spec/models/spree/product/scopes_spec.rb
@@ -309,4 +309,21 @@ describe 'Product scopes', type: :model do
)
end
end
+
+ context '#by_store' do
+ subject(:products_by_store) { Spree::Product.by_store(store) }
+
+ let(:store) { create(:store) }
+
+ let!(:product_1) { create(:product, stores: [store]) }
+ let!(:product_2) { create(:product, stores: [store]) }
+
+ before do
+ create_list(:product, 3, stores: [create(:store)])
+ end
+
+ it 'returns products assigned to a store' do
+ expect(products_by_store).to contain_exactly(product_1, product_2)
+ end
+ end
end
diff --git a/core/spec/models/spree/store_product_spec.rb b/core/spec/models/spree/store_product_spec.rb
new file mode 100644
index 0000000000..c174d8475a
--- /dev/null
+++ b/core/spec/models/spree/store_product_spec.rb
@@ -0,0 +1,33 @@
+require 'spec_helper'
+
+describe Spree::StoreProduct, type: :model do
+ let!(:store) { create(:store) }
+ let!(:store_2) { create(:store) }
+ let!(:product) { create(:product) }
+
+ context 'assigning store' do
+ it 'touches product' do
+ expect { product.stores << store }.to change { product.reload.updated_at }
+ end
+
+ it 'increases Store Product count' do
+ expect { product.stores << store }.to change(described_class, :count).from(0).to(1)
+ end
+ end
+
+ context 'unassignes store' do
+ before { product.stores << store }
+
+ it 'touches product' do
+ expect { product.stores.destroy(store) }.to change { product.reload.updated_at }
+ end
+
+ it 'decreases Store Product count' do
+ expect { product.stores.destroy(store) }.to change(described_class, :count).from(1).to(0)
+ end
+
+ it 'does not remove store' do
+ expect { product.stores.destroy(store) }.not_to change { Spree::Store.count }
+ end
+ end
+end
diff --git a/frontend/app/controllers/spree/products_controller.rb b/frontend/app/controllers/spree/products_controller.rb
index b0b7354b4a..500e6fbc14 100644
--- a/frontend/app/controllers/spree/products_controller.rb
+++ b/frontend/app/controllers/spree/products_controller.rb
@@ -7,6 +7,8 @@ module Spree
before_action :load_product, only: [:show, :related]
before_action :load_taxon, only: :index
+ before_action :can_show_product?, only: :show
+
respond_to :html
def index
@@ -59,6 +61,10 @@ module Spree
@taxon = Spree::Taxon.find(params[:taxon]) if params[:taxon].present?
end
+ def can_show_product?
+ raise ActiveRecord::RecordNotFound if @product.stores.empty? || @product.stores.exclude?(current_store)
+ end
+
def load_variants
@variants = @product.
variants_including_master.