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.

Fixed Promotion Rule Option Values (#12916)

Damian Legawiec6586ae5d9e34
diff --git a/.circleci/config.yml b/.circleci/config.yml
index 27f376c3d6..1695482caf 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -30,9 +30,16 @@ commands:
command: bin/build-ci.rb install
install_libvips:
steps:
+ - run:
+ name: Fix APT mirrors
+ command: |
+ sudo sed -i 's|http://archive.ubuntu.com|http://ubuntu.cs.utah.edu|g' /etc/apt/sources.list
+ sudo sed -i 's|http://security.ubuntu.com|http://ubuntu.cs.utah.edu|g' /etc/apt/sources.list
+ sudo apt-get update
- run:
name: Install libvips
- command: sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 32EE5355A6BC6E42 && sudo apt-get update && sudo apt-get install libvips42
+ command: |
+ sudo apt-get install -y libvips-dev
store_all_artifacts:
steps:
- store_artifacts:
diff --git a/admin/app/assets/stylesheets/spree/admin/components/_modals.scss b/admin/app/assets/stylesheets/spree/admin/components/_modals.scss
index b56a7d8a31..feb3acf13d 100644
--- a/admin/app/assets/stylesheets/spree/admin/components/_modals.scss
+++ b/admin/app/assets/stylesheets/spree/admin/components/_modals.scss
@@ -18,6 +18,7 @@ body.modal-open {
.modal-title {
margin-bottom: 0;
+ font-weight: $font-weight-bold;
}
.btn-close {
diff --git a/admin/app/controllers/spree/admin/search_controller.rb b/admin/app/controllers/spree/admin/search_controller.rb
new file mode 100644
index 0000000000..b65cbf1643
--- /dev/null
+++ b/admin/app/controllers/spree/admin/search_controller.rb
@@ -0,0 +1,22 @@
+module Spree
+ module Admin
+ class SearchController < BaseController
+ def option_values
+ query = params[:q]&.strip
+
+ if query.present?
+ json = Spree::OptionValue.includes(:option_type).search_by_name(query).map do |ov|
+ {
+ id: ov.id,
+ name: ov.display_presentation
+ }
+ end
+
+ render json: json
+ else
+ render json: []
+ end
+ end
+ end
+ end
+end
diff --git a/admin/app/helpers/spree/admin/promotion_rules_helper.rb b/admin/app/helpers/spree/admin/promotion_rules_helper.rb
index 58372e05e1..935fc2db3f 100644
--- a/admin/app/helpers/spree/admin/promotion_rules_helper.rb
+++ b/admin/app/helpers/spree/admin/promotion_rules_helper.rb
@@ -10,7 +10,19 @@ module Spree
eligible_values = promotion_rule.preferred_eligible_values || []
return [] if eligible_values.empty?
- Spree::OptionValueVariant.where(id: eligible_values).to_tom_select_json
+ Spree::OptionValue.includes(:option_type).where(id: eligible_values).map do |ov|
+ {
+ id: ov.id,
+ name: ov.display_presentation
+ }
+ end
+ end
+
+ # Returns the promotion rule option values
+ # @param value_ids [Array<Integer>]
+ # @return [Array<String>]
+ def promotion_rule_option_values(value_ids)
+ Spree::OptionValue.includes(:option_type).where(id: value_ids).map(&:display_presentation)
end
end
end
diff --git a/admin/app/views/spree/admin/promotion_rules/_promotion_rule.html.erb b/admin/app/views/spree/admin/promotion_rules/_promotion_rule.html.erb
index f38d8a65e7..f73adac8e6 100644
--- a/admin/app/views/spree/admin/promotion_rules/_promotion_rule.html.erb
+++ b/admin/app/views/spree/admin/promotion_rules/_promotion_rule.html.erb
@@ -22,30 +22,47 @@
<%= render 'spree/admin/shared/preferences', object: promotion_rule %>
<% end %>
+ <% if promotion_rule.respond_to?(:preferred_eligible_values) %>
+ <div class="mt-3">
+ <span><%= Spree.t(:option_values) %>:</span>
+ <div class="d-flex flex-wrap gap-2">
+ <% promotion_rule_option_values(promotion_rule.preferred_eligible_values).each do |value| %>
+ <strong><%= value %></strong>
+ <% end %>
+ </div>
+ </div>
+ <% end %>
+
<% if promotion_rule.respond_to?(:products) %>
<div class="mt-3">
<span><%= Spree.t(:products) %>:</span>
- <% promotion_rule.products.pluck(:name).each do |product_name| %>
+ <div class="d-flex flex-wrap gap-2">
+ <% promotion_rule.products.pluck(:name).each do |product_name| %>
<strong><%= product_name %></strong>
- <% end %>
+ <% end %>
+ </div>
</div>
<% end %>
<% if promotion_rule.respond_to?(:taxons) %>
<div class="mt-3">
<span><%= Spree.t(:taxons) %>:</span>
- <% promotion_rule.taxons.pluck(:pretty_name).each do |taxon_name| %>
- <strong><%= taxon_name %></strong>
- <% end %>
+ <div class="d-flex flex-wrap gap-2">
+ <% promotion_rule.taxons.pluck(:pretty_name).each do |taxon_name| %>
+ <strong><%= taxon_name %></strong>
+ <% end %>
+ </div>
</div>
<% end %>
<% if promotion_rule.respond_to?(:users) %>
<div class="mt-3">
<span><%= Spree.t(:users) %>:</span>
- <% promotion_rule.users.pluck(:email).each do |user_email| %>
- <strong><%= user_email %></strong>
- <% end %>
+ <div class="d-flex flex-wrap gap-2">
+ <% promotion_rule.users.pluck(:email).each do |user_email| %>
+ <strong><%= user_email %></strong>
+ <% end %>
+ </div>
</div>
<% end %>
</div>
diff --git a/admin/app/views/spree/admin/promotion_rules/forms/_option_value.html.erb b/admin/app/views/spree/admin/promotion_rules/forms/_option_value.html.erb
index 28cf8c404c..0c52682802 100644
--- a/admin/app/views/spree/admin/promotion_rules/forms/_option_value.html.erb
+++ b/admin/app/views/spree/admin/promotion_rules/forms/_option_value.html.erb
@@ -1,7 +1,7 @@
<div class="form-group">
<%= tom_select_tag "promotion_rule[preferred_eligible_values][]",
placeholder: Spree.t(:type_to_search),
- url: spree.admin_promotion_rules_option_values_search_path,
+ url: spree.admin_search_option_values_path,
remote_search: true,
remote_search_active_option: active_options_for_option_value_promotion_rule(f.object),
multiple: true,
diff --git a/admin/app/views/spree/admin/shared/_preferences.html.erb b/admin/app/views/spree/admin/shared/_preferences.html.erb
index 262cdf74fb..61bdafb397 100644
--- a/admin/app/views/spree/admin/shared/_preferences.html.erb
+++ b/admin/app/views/spree/admin/shared/_preferences.html.erb
@@ -2,6 +2,7 @@
<p class="mb-0">
<% object.preferences.each do |key, value| %>
<% next if value.blank? %>
+ <% next if key == :eligible_values %>
<span><%= Spree.t(key) %>:</span>
<strong><%= value %></strong>
<% end %>
diff --git a/admin/config/routes.rb b/admin/config/routes.rb
index 4e88308b5e..528021f8dc 100644
--- a/admin/config/routes.rb
+++ b/admin/config/routes.rb
@@ -117,7 +117,7 @@ Spree::Core::Engine.add_routes do
resources :promotion_rules, as: :rules, except: [:index, :show]
resources :coupon_codes, only: :index
end
- get 'promotion_rules/option_values_search', defaults: { format: :json }
+ get 'search/option_values', defaults: { format: :json }, to: 'search#option_values'
# gift cards
resources :gift_cards
diff --git a/admin/spec/controllers/spree/admin/promotion_rules_controller_spec.rb b/admin/spec/controllers/spree/admin/promotion_rules_controller_spec.rb
index 825db3236e..361dff4d0b 100644
--- a/admin/spec/controllers/spree/admin/promotion_rules_controller_spec.rb
+++ b/admin/spec/controllers/spree/admin/promotion_rules_controller_spec.rb
@@ -9,10 +9,6 @@ RSpec.describe Spree::Admin::PromotionRulesController, type: :controller do
let(:store) { @default_store }
let(:promotion) { create(:promotion, stores: [store]) }
- before do
- allow(controller).to receive(:current_ability).and_call_original
- end
-
describe 'GET #new' do
it 'returns a successful response' do
get :new, params: { promotion_id: promotion.id }
@@ -97,6 +93,15 @@ RSpec.describe Spree::Admin::PromotionRulesController, type: :controller do
get :edit, params: { promotion_id: promotion.id, id: promotion_rule.id }
expect(assigns(:promotion_rule)).to eq(promotion_rule)
end
+
+ context 'with option value rule' do
+ let!(:promotion_rule) { create(:promotion_rule_option_value, promotion: promotion) }
+
+ it 'returns a successful response' do
+ get :edit, params: { promotion_id: promotion.id, id: promotion_rule.id }
+ expect(response).to be_successful
+ end
+ end
end
describe 'PATCH #update' do
@@ -117,6 +122,23 @@ RSpec.describe Spree::Admin::PromotionRulesController, type: :controller do
patch :update, params: { promotion_id: promotion.id, id: promotion_rule.id, promotion_rule: update_params }
expect(response).to redirect_to(spree.admin_promotion_path(promotion))
end
+
+ context 'with option value rule' do
+ let!(:promotion_rule) { create(:promotion_rule_option_value, promotion: promotion) }
+ let!(:option_value) { create(:option_value) }
+ let(:rule_params) do
+ {
+ preferred_match_policy: 'any',
+ preferred_eligible_values: [option_value.id]
+ }
+ end
+
+ it 'updates the promotion rule' do
+ patch :update, params: { promotion_id: promotion.id, id: promotion_rule.id, promotion_rule: rule_params }
+ promotion_rule.reload
+ expect(promotion_rule.preferred_eligible_values).to include(option_value.id.to_s)
+ end
+ end
end
describe 'DELETE #destroy' do
diff --git a/admin/spec/requests/spree/admin/search_spec.rb b/admin/spec/requests/spree/admin/search_spec.rb
new file mode 100644
index 0000000000..e09ab2cea6
--- /dev/null
+++ b/admin/spec/requests/spree/admin/search_spec.rb
@@ -0,0 +1,77 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Admin::SearchController, type: :request do
+ stub_authorization!
+
+ describe 'GET /admin/search/option_values' do
+ let!(:option_type) { create(:option_type, name: 'color', presentation: 'Color') }
+ let!(:option_type2) { create(:option_type, name: 'size', presentation: 'Size') }
+ let!(:option_value1) { create(:option_value, name: 'red', presentation: 'Red', option_type: option_type) }
+ let!(:option_value2) { create(:option_value, name: 'blue', presentation: 'Blue', option_type: option_type) }
+ let!(:option_value3) { create(:option_value, name: 'green', presentation: 'Green', option_type: option_type) }
+ let!(:option_value4) { create(:option_value, name: 'small', presentation: 'Small', option_type: option_type2) }
+ let!(:option_value5) { create(:option_value, name: 'medium', presentation: 'Medium', option_type: option_type2) }
+ let!(:option_value6) { create(:option_value, name: 'large', presentation: 'Large', option_type: option_type2) }
+
+ context 'when query is present' do
+ it 'returns matching option values with option type presentation' do
+ get '/admin/search/option_values', params: { q: 'red' }
+
+ expect(response).to have_http_status(:ok)
+ expect(JSON.parse(response.body)).to eq([
+ {
+ 'id' => option_value1.id,
+ 'name' => 'Color: Red'
+ }
+ ])
+ end
+
+ it 'returns multiple matching option values' do
+ get '/admin/search/option_values', params: { q: 'e' }
+
+ expect(response).to have_http_status(:ok)
+ json_response = JSON.parse(response.body)
+ expect(json_response.length).to eq(5)
+ expect(json_response.map { |ov| ov['name'] }).to match_array([
+ 'Color: Red',
+ 'Color: Blue',
+ 'Color: Green',
+ 'Size: Medium',
+ 'Size: Large'
+ ])
+ end
+
+ it 'returns empty array when no matches found' do
+ get '/admin/search/option_values', params: { q: 'yellow' }
+
+ expect(response).to have_http_status(:ok)
+ expect(JSON.parse(response.body)).to eq([])
+ end
+ end
+
+ context 'when query is empty' do
+ it 'returns empty array' do
+ get '/admin/search/option_values', params: { q: '' }
+
+ expect(response).to have_http_status(:ok)
+ expect(JSON.parse(response.body)).to eq([])
+ end
+
+ it 'returns empty array when query is only whitespace' do
+ get '/admin/search/option_values', params: { q: ' ' }
+
+ expect(response).to have_http_status(:ok)
+ expect(JSON.parse(response.body)).to eq([])
+ end
+ end
+
+ context 'when query parameter is missing' do
+ it 'returns empty array' do
+ get '/admin/search/option_values'
+
+ expect(response).to have_http_status(:ok)
+ expect(JSON.parse(response.body)).to eq([])
+ end
+ end
+ end
+end
diff --git a/api/spec/serializers/spree/api/v2/platform/option_value_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/option_value_serializer_spec.rb
index d49849eb82..86fb3ff0b9 100644
--- a/api/spec/serializers/spree/api/v2/platform/option_value_serializer_spec.rb
+++ b/api/spec/serializers/spree/api/v2/platform/option_value_serializer_spec.rb
@@ -15,6 +15,7 @@ describe Spree::Api::V2::Platform::OptionValueSerializer do
position: option_value.position,
name: option_value.name,
presentation: option_value.presentation,
+ display_presentation: option_value.display_presentation,
created_at: option_value.created_at,
updated_at: option_value.updated_at,
public_metadata: {},
diff --git a/core/app/models/concerns/spree/parameterizable_name.rb b/core/app/models/concerns/spree/parameterizable_name.rb
index 9464758d5a..3959f1246b 100644
--- a/core/app/models/concerns/spree/parameterizable_name.rb
+++ b/core/app/models/concerns/spree/parameterizable_name.rb
@@ -11,6 +11,15 @@ module Spree
before_validation :set_name_from_presentation, if: -> { name.blank? }
before_validation :normalize_name
+ #
+ # Scopes
+ #
+ scope :search_by_name, ->(query) do
+ i18n do
+ name.matches("%#{query.downcase}%").or(presentation.matches("%#{query.downcase}%"))
+ end
+ end
+
def set_name_from_presentation
self.name = presentation
end
diff --git a/core/app/models/spree/option_type.rb b/core/app/models/spree/option_type.rb
index 2a43ae729c..8f9ceb2fc7 100644
--- a/core/app/models/spree/option_type.rb
+++ b/core/app/models/spree/option_type.rb
@@ -45,14 +45,6 @@ module Spree
scope :colors, -> { where(name: COLOR_NAMES) }
scope :filterable, -> { where(filterable: true) }
- if defined?(PgSearch)
- # full text search
- include PgSearch::Model
- pg_search_scope :search_by_name, against: %i[name presentation]
- else
- scope :search_by_name, ->(query) { where('name LIKE ?', "%#{query}%") }
- end
-
#
# Attributes
#
diff --git a/core/app/models/spree/option_value.rb b/core/app/models/spree/option_value.rb
index 692a6af6d4..28f356e2c2 100644
--- a/core/app/models/spree/option_value.rb
+++ b/core/app/models/spree/option_value.rb
@@ -60,6 +60,7 @@ module Spree
delegate :name, :presentation, to: :option_type, prefix: true, allow_nil: true
# Using map here instead of pluck, as these values are translatable via Mobility gem
+ # @return [Array<Hash>]
def self.to_tom_select_json
all.map do |ov|
{
@@ -69,6 +70,12 @@ module Spree
end
end
+ # Returns the presentation with the option type presentation, eg. "Color: Red"
+ # @return [String]
+ def display_presentation
+ @display_presentation ||= "#{option_type.presentation}: #{presentation}"
+ end
+
private
def touch_all_variants
diff --git a/core/app/models/spree/shipping_method.rb b/core/app/models/spree/shipping_method.rb
index 7e75ec55ff..72b881a835 100644
--- a/core/app/models/spree/shipping_method.rb
+++ b/core/app/models/spree/shipping_method.rb
@@ -39,13 +39,7 @@ module Spree
joins(:calculator).where(spree_calculators: { type: Spree::Calculator::Shipping::DigitalDelivery.to_s })
}
- if defined?(PgSearch)
- # full text search
- include PgSearch::Model
- pg_search_scope :search_by_name, against: %i[name]
- else
- scope :search_by_name, ->(query) { where('name LIKE ?', "%#{query}%") }
- end
+ scope :search_by_name, ->(query) { where(arel_table[:name].lower.matches("%#{query}%")) }
def include?(address)
return true unless requires_zone_check?
diff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rb
index 2f5fdfa660..46c975d69d 100644
--- a/core/lib/spree/permitted_attributes.rb
+++ b/core/lib/spree/permitted_attributes.rb
@@ -170,7 +170,7 @@ module Spree
@@promotion_attributes = [:name, :description, :starts_at, :expires_at, :code, :usage_limit, :path, :match_policy,
:advertise, :promotion_category_id, :code_prefix, :kind, :number_of_codes, :multi_codes, store_ids: []]
- @@promotion_rule_attributes = [:type, :preferred_match_policy, user_ids_to_add: [], product_ids_to_add: [], taxon_ids_to_add: []]
+ @@promotion_rule_attributes = [:type, :preferred_match_policy, preferred_eligible_values: [], user_ids_to_add: [], product_ids_to_add: [], taxon_ids_to_add: []]
@@promotion_action_attributes = [:type, :calculator_type, calculator_attributes: {}, promotion_action_line_items_attributes: [:id, :promotion_action_id, :variant_id, :quantity, :_destroy]]
diff --git a/core/lib/spree/testing_support/authorization_helpers.rb b/core/lib/spree/testing_support/authorization_helpers.rb
index 1ac4162332..bf57ce9b41 100644
--- a/core/lib/spree/testing_support/authorization_helpers.rb
+++ b/core/lib/spree/testing_support/authorization_helpers.rb
@@ -68,4 +68,5 @@ end
RSpec.configure do |config|
config.extend Spree::TestingSupport::AuthorizationHelpers::Controller, type: :controller
config.extend Spree::TestingSupport::AuthorizationHelpers::Request, type: :feature
+ config.extend Spree::TestingSupport::AuthorizationHelpers::Request, type: :request
end
diff --git a/core/lib/spree/testing_support/factories/promotion_rule_factory.rb b/core/lib/spree/testing_support/factories/promotion_rule_factory.rb
index cf1e515feb..e6f9752ef1 100644
--- a/core/lib/spree/testing_support/factories/promotion_rule_factory.rb
+++ b/core/lib/spree/testing_support/factories/promotion_rule_factory.rb
@@ -14,4 +14,8 @@ FactoryBot.define do
factory :promotion_rule_taxon, class: Spree::Promotion::Rules::Taxon do
association :promotion
end
+
+ factory :promotion_rule_option_value, class: Spree::Promotion::Rules::OptionValue do
+ association :promotion
+ end
end
diff --git a/core/spec/models/spree/option_value_spec.rb b/core/spec/models/spree/option_value_spec.rb
index 80509ee200..9c5933a074 100644
--- a/core/spec/models/spree/option_value_spec.rb
+++ b/core/spec/models/spree/option_value_spec.rb
@@ -44,6 +44,15 @@ describe Spree::OptionValue, type: :model do
end
end
+ describe '#display_presentation' do
+ let(:option_value) { build(:option_value, name: 'red', presentation: 'Red', option_type: option_type) }
+ let(:option_type) { create(:option_type, name: 'Color', presentation: 'Color') }
+
+ it 'returns the presentation with the option type presentation' do
+ expect(option_value.display_presentation).to eq('Color: Red')
+ end
+ end
+
describe '.to_tom_select_json' do
let!(:option_value) { create(:option_value, name: 'red', presentation: 'Red') }
let!(:option_value2) { create(:option_value, name: 'blue', presentation: 'Blue') }
diff --git a/sample/db/samples/promotions.rb b/sample/db/samples/promotions.rb
index 4ce0673922..ed36e42418 100644
--- a/sample/db/samples/promotions.rb
+++ b/sample/db/samples/promotions.rb
@@ -1,10 +1,8 @@
Spree::Sample.load_sample('option_values')
Spree::Sample.load_sample('products')
-product = Spree::Product.find_by!(name: 'Denim Shirt')
size = Spree::OptionValue.find_by!(name: 'xs')
color = Spree::OptionValue.find_by!(name: 'red')
-eligible_values = "#{size.id},#{color.id}"
promotion = Spree::Promotion.where(
name: 'free shipping',
@@ -20,7 +18,7 @@ end
Spree::PromotionRule.where(
promotion: promotion,
type: 'Spree::Promotion::Rules::OptionValue',
- preferences: { match_policy: 'any', eligible_values: { product.id.to_s => eligible_values } }
+ preferences: { match_policy: 'any', eligible_values: [size.id, color.id] }
).first_or_create!
Spree::Promotion::Actions::FreeShipping.where(promotion: promotion).first_or_create!