Skip to content

Commit efe8b97

Browse files
authored
Merge pull request solidusio#6100 from gms-electronics/add-permalink-history-to-taxon
Add permalink history for taxon on friendly-id
2 parents dfe87ae + 5618cc6 commit efe8b97

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

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/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)