Skip to content

Commit

Permalink
Update taxons permalinks on save
Browse files Browse the repository at this point in the history
  • Loading branch information
jhawthorn committed Feb 12, 2016
1 parent 929ef1e commit e342ecf
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 6 deletions.
24 changes: 20 additions & 4 deletions core/app/models/spree/taxon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Taxon < Spree::Base
has_many :promotion_rules, through: :promotion_rule_taxons

before_create :set_permalink
after_update :update_permalinks

validates :name, presence: true
validates :meta_keywords, length: { maximum: 255 }
Expand Down Expand Up @@ -57,10 +58,15 @@ def seo_title
# Sets this taxons permalink to a valid url encoded string based on its
# name and its parents permalink (if present.)
def set_permalink
if parent.present?
self.permalink = [parent.permalink, (permalink.blank? ? name.to_url : permalink.split('/').last)].join('/')
else
self.permalink = name.to_url if permalink.blank?
self.permalink = build_permalink
end

# Update the permalink for this taxon and all children (if necessary)
def update_permalinks
new_permalink = build_permalink
if new_permalink != permalink
update_columns(permalink: build_permalink)
children.each(&:update_permalinks)
end
end

Expand Down Expand Up @@ -97,6 +103,16 @@ def child_index=(idx)

private

def build_permalink
permalink_tail = permalink.split('/').last if permalink.present?
permalink_tail ||= name.to_url
if parent.present?
[parent.permalink, permalink_tail].join('/')
else
permalink_tail
end
end

def touch_ancestors_and_taxonomy
# Touches all ancestors at once to avoid recursive taxonomy touch, and reduce queries.
self.class.where(id: ancestors.pluck(:id)).update_all(updated_at: Time.current)
Expand Down
56 changes: 54 additions & 2 deletions core/spec/models/spree/taxon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
require 'spec_helper'

describe Spree::Taxon, type: :model do
let(:taxon) { FactoryGirl.build(:taxon, name: "Ruby on Rails") }

describe '#to_param' do
let(:taxon) { FactoryGirl.build(:taxon, name: "Ruby on Rails") }

subject { super().to_param }
it { is_expected.to eql taxon.permalink }
end

context "set_permalink" do
let(:taxon) { FactoryGirl.build(:taxon, name: "Ruby on Rails") }

it "should set permalink correctly when no parent present" do
taxon.set_permalink
expect(taxon.permalink).to eql "ruby-on-rails"
Expand Down Expand Up @@ -61,6 +63,56 @@
end
end

context "updating permalink" do
let(:taxonomy) { create(:taxonomy, name: 't') }
let(:root) { taxonomy.root }
let(:taxon1) { create(:taxon, name: 't1', taxonomy: taxonomy, parent: root) }
let(:taxon2) { create(:taxon, name: 't2', taxonomy: taxonomy, parent: root) }
let(:taxon2_child) { create(:taxon, name: 't2_child', taxonomy: taxonomy, parent: taxon2) }

context "changing parent" do
subject do
-> { taxon2.update!(parent: taxon1) }
end

it "changes own permalink" do
is_expected.to change{ taxon2.reload.permalink }.from('t/t2').to('t/t1/t2')
end

it "changes child's permalink" do
is_expected.to change{ taxon2_child.reload.permalink }.from('t/t2/t2-child').to('t/t1/t2/t2-child')
end
end

context "changing own permalink" do
subject do
-> { taxon2.update!(permalink: 'foo') }
end

it "changes own permalink" do
is_expected.to change{ taxon2.reload.permalink }.from('t/t2').to('t/foo')
end

it "changes child's permalink" do
is_expected.to change{ taxon2_child.reload.permalink }.from('t/t2/t2-child').to('t/foo/t2-child')
end
end

context "changing parent and own permalink" do
subject do
-> { taxon2.update!(parent: taxon1, permalink: 'foo') }
end

it "changes own permalink" do
is_expected.to change{ taxon2.reload.permalink }.from('t/t2').to('t/t1/foo')
end

it "changes child's permalink" do
is_expected.to change{ taxon2_child.reload.permalink }.from('t/t2/t2-child').to('t/t1/foo/t2-child')
end
end
end

# Regression test for https://github.com/spree/spree/issues/2620
context "creating a child node using first_or_create" do
let(:taxonomy) { create(:taxonomy) }
Expand Down

0 comments on commit e342ecf

Please sign in to comment.