Skip to content

Commit 5d070ab

Browse files
committed
Add permalink history for taxon on friendly-id
1 parent ca1ae4d commit 5d070ab

File tree

8 files changed

+69
-14
lines changed

8 files changed

+69
-14
lines changed

.circleci/config.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ commands:
224224
mkdir -p /tmp/dummy_extension
225225
cd /tmp/dummy_extension
226226
bundle init
227-
bundle add rails -v "< 8.0" --skip-install
227+
bundle add rails -v "< 8.1" --skip-install
228228
bundle add sqlite3 -v "~> 2.0" --skip-install
229229
test -n "<<parameters.extra_gems>>" && bundle add <<parameters.extra_gems>> --skip-install
230230
bundle add solidus --path "$(ruby -e"puts File.expand_path ENV['CIRCLE_WORKING_DIRECTORY']")"
@@ -352,7 +352,7 @@ workflows:
352352
{
353353
parameters:
354354
{
355-
rails: ["7.0", "7.1", "7.2"],
355+
rails: ["7.0", "7.1", "7.2", "8.0"],
356356
ruby: ["3.1"],
357357
database: ["mysql"],
358358
paperclip: [true],
@@ -364,7 +364,7 @@ workflows:
364364
{
365365
parameters:
366366
{
367-
rails: ["7.0", "7.1", "7.2"],
367+
rails: ["7.0", "7.1", "7.2", "8.0"],
368368
ruby: ["3.1"],
369369
database: ["postgres"],
370370
paperclip: [false],
@@ -388,7 +388,7 @@ workflows:
388388
{
389389
parameters:
390390
{
391-
rails: ["7.2", "main"],
391+
rails: ["7.2", "8.0", "main"],
392392
ruby: ["3.3.5"],
393393
database: ["sqlite"],
394394
paperclip: [false],

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ gemspec require: false
88
if /(stable|main)/.match? ENV['RAILS_VERSION']
99
gem 'rails', github: 'rails', require: false, branch: ENV['RAILS_VERSION']
1010
else
11-
gem 'rails', ENV['RAILS_VERSION'] || ['> 7.0', '< 8.0.0.beta1'], require: false
11+
gem 'rails', ENV['RAILS_VERSION'] || ['> 7.0', '< 8.1.0.beta1'], require: false
1212
end
1313
# rubocop:enable Bundler/DuplicatedGem
1414

admin/app/components/solidus_admin/ui/forms/input/component.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,24 @@ def call
9393
with_content options_for_select(@attributes.delete(:choices), @attributes.delete(:value))
9494
end
9595

96-
tag.public_send(
97-
@tag,
98-
content,
96+
options = {
9997
"data-controller": stimulus_id,
10098
"data-#{stimulus_id}-custom-validity-value": @error.presence,
10199
"data-action": "#{stimulus_id}#clearCustomValidity",
102100
**@attributes
103-
)
101+
}
102+
103+
if tag.method(@tag).parameters.any? { |_type, name| name == :content }
104+
tag.public_send(
105+
@tag,
106+
content,
107+
**options
108+
)
109+
else
110+
tag.public_send(
111+
@tag,
112+
**options
113+
)
114+
end
104115
end
105116
end

api/spec/requests/spree/api/shipments_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ module Spree::Api
7575
it 'returns proper error' do
7676
subject
7777
expect(response.status).to eq(422)
78-
expect(json_response['exception']).to eq("param is missing or the value is empty: stock_location_id")
78+
expect(json_response['exception']).to match(/param is missing or the value is empty( or invalid)?: stock_location_id/)
7979
end
8080
end
8181
end

api/spec/requests/spree/api/stock_items_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ module Spree::Api
8686

8787
it 'requires a stock_location_id to be passed as a parameter' do
8888
get spree.api_stock_items_path
89-
expect(json_response['exception']).to eq('param is missing or the value is empty: stock_location_id')
89+
expect(json_response['exception']).to match(/param is missing or the value is empty( or invalid)?: stock_location_id/)
9090
expect(response.status).to eq(422)
9191
end
9292

core/app/models/spree/taxon.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
module Spree
66
class Taxon < Spree::Base
7+
extend FriendlyId
8+
friendly_id :permalink, use: :history, slug_column: :permalink
9+
710
acts_as_nested_set dependent: :destroy
811

912
belongs_to :taxonomy, class_name: 'Spree::Taxonomy', inverse_of: :taxons
@@ -13,8 +16,7 @@ class Taxon < Spree::Base
1316
has_many :promotion_rule_taxons
1417
has_many :promotion_rules, through: :promotion_rule_taxons
1518

16-
before_create :set_permalink
17-
before_update :set_permalink
19+
before_save :set_permalink
1820
after_update :update_child_permalinks, if: :saved_change_to_permalink?
1921

2022
validates :name, presence: true
@@ -121,6 +123,24 @@ def permalink_part=(value)
121123
end
122124
end
123125

126+
# override for {FriendlyId::Slugged#should_generate_new_friendly_id?} method,
127+
# to control exactly when new friendly ids are set or updated
128+
def should_generate_new_friendly_id?
129+
permalink_changed? || super
130+
end
131+
132+
# override for {FriendlyId::Slugged#normalize_friendly_id} method,
133+
# to control over the slug format
134+
def normalize_friendly_id(value)
135+
return '' if value.blank?
136+
137+
parts = value.to_s.split('/')
138+
last_word = parts.pop
139+
corrected_last_word = Spree::Config.taxon_url_parametizer_class.parameterize(last_word)
140+
141+
(parts << corrected_last_word).join('/')
142+
end
143+
124144
private
125145

126146
def touch_ancestors_and_taxonomy

core/solidus_core.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Gem::Specification.new do |s|
2727
actionmailer actionpack actionview activejob activemodel activerecord
2828
activestorage activesupport railties
2929
].each do |rails_dep|
30-
s.add_dependency rails_dep, ['>= 7.0', '< 8.0.0.beta1']
30+
s.add_dependency rails_dep, ['>= 7.0', '< 8.1.0.beta1']
3131
end
3232

3333
s.add_dependency 'activemerchant', '~> 1.66'

core/spec/models/spree/taxon_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,28 @@
269269
end
270270
end
271271
end
272+
273+
context 'stores history of permalinks' do
274+
let(:taxonomy) { create(:taxonomy, name: 'brands') }
275+
let(:root) { taxonomy.root }
276+
let(:taxon_name) { "ruby on rails" }
277+
let(:new_permalink) { "rails for ruby" }
278+
let(:expected_initial_slug) { "brands/ruby-on-rails" }
279+
let(:expected_updated_slug) { "brands/rails-for-ruby" }
280+
281+
let!(:taxon) { create(:taxon, name: taxon_name, parent: root) }
282+
283+
it 'should store the previous slug when permalink is updated' do
284+
expect(taxon.slugs.count).to eq(1)
285+
expect(expected_initial_slug).to eq(taxon.slugs.last.slug)
286+
287+
taxon.update(permalink: new_permalink)
288+
289+
expect(expected_updated_slug).to eq(taxon.permalink)
290+
expect(taxon.slugs.count).to eq(2)
291+
292+
expect(taxon.slugs.pluck(:slug)).to include(expected_initial_slug)
293+
expect(taxon.slugs.pluck(:slug)).to include(expected_updated_slug)
294+
end
295+
end
272296
end

0 commit comments

Comments
 (0)