Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameterize taxon's permalink also on update #3090

Merged
merged 1 commit into from
Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions core/app/models/spree/taxon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ 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
permalink_tail = permalink.split('/').last if permalink.present?
permalink_tail ||= Spree::Config.taxon_url_parametizer_class.parameterize(name)
self.permalink_part = permalink_tail
permalink_tail = permalink.present? ? permalink.split('/').last : name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we can use permalink_part_changed? here to change the permalink_part only when it is changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll change that, it's better yes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, we cannot really use that condition because this method is called also for all children via:

after_update :update_child_permalinks, if: :saved_change_to_permalink?
...

def update_permalinks
  set_permalink

  # This will trigger update_child_permalinks if permalink has changed
  save!
end

it breaks current test, I will leave it this way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, can't see any clean solution here. I agree it's better to keep it as is.

self.permalink_part = Spree::Config.taxon_url_parametizer_class.parameterize(permalink_tail)
end

# Update the permalink for this taxon and all children (if necessary)
Expand Down
28 changes: 28 additions & 0 deletions core/spec/models/spree/taxon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
expect(taxon.permalink).to eql "ruby-on-rails"
end

context "updating a taxon permalink" do
it 'parameterizes permalink correctly' do
taxon.save!
taxon.update_attributes(permalink: 'spécial&charactèrs')
expect(taxon.permalink).to eql "special-characters"
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change also has effects on permalinks of children. Can we test that it works with children as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I'll add this test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test added

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the test you added is testing that the permalink is correctly saved when it has a parent taxon. I mean to test when the taxon with special chars has children instead (like spécial&charactèrs/child for example) so we can see the parameterization reflected on those taxons as well. Anyway the test you added can stay, better to have that as well. 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added 2 more tests


context "with parent taxon" do
let(:parent) { FactoryBot.build(:taxon, permalink: "brands") }
before { allow(taxon).to receive_messages parent: parent }
Expand All @@ -41,6 +49,12 @@
expect(taxon.permalink).to eql "brands/rubyonrails"
end

it 'parameterizes permalink correctly' do
taxon.save!
taxon.update_attributes(permalink_part: 'spécial&charactèrs')
expect(taxon.reload.permalink).to eql "brands/special-characters"
end

# Regression test for https://github.com/spree/spree/issues/3390
context "setting a new node sibling position via :child_index=" do
let(:idx) { rand(0..100) }
Expand Down Expand Up @@ -121,6 +135,20 @@
is_expected.to change{ taxon2_child.reload.permalink }.from('t/t2/t2_child').to('t/t1/foo/t2_child')
end
end

context 'changing parent permalink with special characters ' do
subject do
-> { taxon2.update!(permalink: 'spécial&charactèrs') }
end

it 'changes own permalink with parameterized characters' do
is_expected.to change{ taxon2.reload.permalink }.from('t/t2').to('t/special-characters')
end

it 'changes child permalink with parameterized characters' do
is_expected.to change{ taxon2_child.reload.permalink }.from('t/t2/t2_child').to('t/special-characters/t2_child')
end
end
end

# Regression test for https://github.com/spree/spree/issues/2620
Expand Down