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 `ParameterizableName` concern (#12157)

Damian Legawiecff6ebe9ef34f
diff --git a/api/spec/integration/api/v2/platform/option_types_spec.rb b/api/spec/integration/api/v2/platform/option_types_spec.rb
index f98173bad2..38ef4fa812 100644
--- a/api/spec/integration/api/v2/platform/option_types_spec.rb
+++ b/api/spec/integration/api/v2/platform/option_types_spec.rb
@@ -27,6 +27,7 @@ describe 'Option Types API', swagger: true do
let(:invalid_param_value) do
{
name: '',
+ presentation: ''
}
end
diff --git a/api/spec/integration/api/v2/platform/option_values_spec.rb b/api/spec/integration/api/v2/platform/option_values_spec.rb
index 55be093fc0..8aac1e8490 100644
--- a/api/spec/integration/api/v2/platform/option_values_spec.rb
+++ b/api/spec/integration/api/v2/platform/option_values_spec.rb
@@ -29,6 +29,7 @@ describe 'Option Values API', swagger: true do
let(:invalid_param_value) do
{
name: '',
+ presentation: ''
}
end
diff --git a/core/app/models/concerns/spree/parameterizable_name.rb b/core/app/models/concerns/spree/parameterizable_name.rb
new file mode 100644
index 0000000000..1aa24e36cc
--- /dev/null
+++ b/core/app/models/concerns/spree/parameterizable_name.rb
@@ -0,0 +1,21 @@
+module Spree
+ module ParameterizableName
+ extend ActiveSupport::Concern
+
+ included do
+ #
+ # Callbacks
+ #
+ before_validation :set_name_from_presentation, if: -> { name.blank? }
+ before_validation :normalize_name
+
+ def set_name_from_presentation
+ self.name = presentation
+ end
+
+ def normalize_name
+ self.name = name.to_s.parameterize.strip
+ end
+ end
+ end
+end
diff --git a/core/app/models/spree/option_type.rb b/core/app/models/spree/option_type.rb
index 42d5aadff2..0bfa05a95e 100644
--- a/core/app/models/spree/option_type.rb
+++ b/core/app/models/spree/option_type.rb
@@ -1,5 +1,8 @@
module Spree
class OptionType < Spree::Base
+ COLOR_NAMES = %w[color colour].freeze
+
+ include Spree::ParameterizableName
include Spree::UniqueName
include Spree::Metadata
include Spree::TranslatableResource
@@ -15,41 +18,64 @@ module Spree
translates(*TRANSLATABLE_FIELDS, column_fallback: true)
end
+ #
+ # Magic methods
+ #
+ self.whitelisted_ransackable_scopes = %w[search_by_name]
acts_as_list
auto_strip_attributes :name, :presentation
+ #
+ # Associations
with_options dependent: :destroy, inverse_of: :option_type do
has_many :option_values, -> { order(:position) }
has_many :product_option_types
end
-
has_many :products, through: :product_option_types
-
has_many :option_type_prototypes, class_name: 'Spree::OptionTypePrototype'
has_many :prototypes, through: :option_type_prototypes, class_name: 'Spree::Prototype'
+ #
+ # Validations
+ #
validates :presentation, presence: true
+ #
+ # Scopes
+ #
default_scope { order(:position) }
-
+ 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
+
accepts_nested_attributes_for :option_values, reject_if: ->(ov) { ov[:name].blank? || ov[:presentation].blank? }, allow_destroy: true
after_touch :touch_all_products
+ # legacy, name itself is now parameterized before saving
def filter_param
name.parameterize
end
def self.color
- find_by(name: 'color')
+ colors.first
+ end
+
+ def color?
+ name.in?(COLOR_NAMES)
end
private
def touch_all_products
- products.update_all(updated_at: Time.current)
+ products.touch_all
end
end
end
diff --git a/core/app/models/spree/option_value.rb b/core/app/models/spree/option_value.rb
index 7a18d6150d..8d790893a2 100644
--- a/core/app/models/spree/option_value.rb
+++ b/core/app/models/spree/option_value.rb
@@ -1,5 +1,6 @@
module Spree
class OptionValue < Spree::Base
+ include Spree::ParameterizableName
include Spree::Metadata
include Spree::TranslatableResource
if defined?(Spree::Webhooks::HasWebhooks)
@@ -14,19 +15,32 @@ module Spree
translates(*TRANSLATABLE_FIELDS, column_fallback: true)
end
- belongs_to :option_type, class_name: 'Spree::OptionType', touch: true, inverse_of: :option_values
-
+ #
+ # Magic methods
+ #
acts_as_list scope: :option_type
auto_strip_attributes :name, :presentation
+ self.whitelisted_ransackable_attributes = ['presentation']
+ #
+ # Associations
+ #
+ belongs_to :option_type, class_name: 'Spree::OptionType', touch: true, inverse_of: :option_values
has_many :option_value_variants, class_name: 'Spree::OptionValueVariant'
has_many :variants, through: :option_value_variants, class_name: 'Spree::Variant'
+ has_many :products, through: :variants, class_name: 'Spree::Product'
+ #
+ # Validations
+ #
with_options presence: true do
validates :name, uniqueness: { scope: :option_type_id, case_sensitive: false }
validates :presentation
end
+ #
+ # Scopes
+ #
scope :filterable, lambda {
joins(:option_type).
where(OptionType.table_name => { filterable: true }).
@@ -38,16 +52,23 @@ module Spree
where(Variant.table_name => { product_id: products.map(&:id) })
}
+ #
+ # Callbacks
+ #
after_touch :touch_all_variants
+ after_update :touch_products, if: -> { saved_change_to_presentation? }
+ after_touch :touch_products
delegate :name, :presentation, to: :option_type, prefix: true, allow_nil: true
- self.whitelisted_ransackable_attributes = ['presentation']
-
private
def touch_all_variants
- variants.update_all(updated_at: Time.current)
+ variants.touch_all
+ end
+
+ def touch_products
+ products.touch_all
end
end
end
diff --git a/core/spec/models/spree/option_type_spec.rb b/core/spec/models/spree/option_type_spec.rb
index 20123738fb..3845c7abd9 100644
--- a/core/spec/models/spree/option_type_spec.rb
+++ b/core/spec/models/spree/option_type_spec.rb
@@ -3,6 +3,18 @@ require 'spec_helper'
describe Spree::OptionType, type: :model do
it_behaves_like 'metadata'
+ describe 'callbacks' do
+ describe '#normalize_name' do
+ let!(:option_type) { build(:option_type, name: 'Shirt Size') }
+
+ it 'should parameterize the name' do
+ option_type.name = 'Shirt Size'
+ option_type.save!
+ expect(option_type.name).to eq('shirt-size')
+ end
+ end
+ end
+
describe 'translations' do
let!(:option_type) { create(:option_type, name: 'size', presentation: 'Size') }
@@ -22,6 +34,27 @@ describe Spree::OptionType, type: :model do
end
end
+ describe 'color methods' do
+ let!(:option_type) { create(:option_type, name: 'Color') }
+
+ describe '.color' do
+ it 'should return the first option type with name "color"' do
+ expect(described_class.color).to eq(option_type)
+ end
+ end
+
+ describe '#color?' do
+ it 'should return true if the name is "color" or "colour"' do
+ expect(option_type.color?).to be_truthy
+ end
+
+ it 'should return false if the name is not "color" or "colour"' do
+ option_type.update(name: 'Size')
+ expect(option_type.color?).to be_falsy
+ end
+ end
+ end
+
context 'touching' do
it 'touches a product' do
product_option_type = create(:product_option_type)
diff --git a/core/spec/models/spree/option_value_spec.rb b/core/spec/models/spree/option_value_spec.rb
index 31aa53ca36..491f1467b1 100644
--- a/core/spec/models/spree/option_value_spec.rb
+++ b/core/spec/models/spree/option_value_spec.rb
@@ -3,6 +3,47 @@ require 'spec_helper'
describe Spree::OptionValue, type: :model do
it_behaves_like 'metadata'
+ describe 'callbacks' do
+ describe '#normalize_name' do
+ let!(:option_value) { build(:option_value, name: 'Red Color') }
+
+ it 'should parameterize the name' do
+ option_value.name = 'Red Color'
+ option_value.valid?
+ expect(option_value.name).to eq('red-color')
+ end
+ end
+
+ describe '#touch_all_variants' do
+ let!(:option_value) { create(:option_value) }
+
+ it 'touches all variants associated with the option value' do
+ variant1 = create(:variant, option_values: [option_value])
+ variant2 = create(:variant, option_values: [option_value])
+ variant3 = create(:variant)
+
+ expect { option_value.send(:touch_all_variants) }.to change { [variant1.reload.updated_at, variant2.reload.updated_at, variant3.reload.updated_at] }
+ end
+ end
+
+ describe '#touch_products' do
+ let!(:option_value) { create(:option_value) }
+ let!(:product1) { create(:product) }
+ let!(:product2) { create(:product) }
+ let!(:product3) { create(:product) }
+
+ before do
+ create(:variant, product: product1, option_values: [option_value])
+ create(:variant, product: product2, option_values: [option_value])
+ create(:variant, product: product3)
+ end
+
+ it 'touches all products associated with the option value' do
+ expect { option_value.send(:touch_products) }.to change { [product1.reload.updated_at, product2.reload.updated_at, product3.reload.updated_at] }
+ end
+ end
+ end
+
describe 'translations' do
let!(:option_value) { create(:option_value, name: 'red', presentation: 'Red') }
@@ -21,14 +62,4 @@ describe Spree::OptionValue, type: :model do
expect(option_value_pl_translation.presentation).to eq('Czerwony')
end
end
-
- context 'touching' do
- it 'touches a variant' do
- variant = create(:variant)
- option_value = variant.option_values.first
- variant.update_column(:updated_at, 1.day.ago)
- option_value.touch
- expect(variant.reload.updated_at).to be_within(3.seconds).of(Time.current)
- end
- end
end