mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-04-03 06:59:14 +00:00
Restoring enterprise permalinks when they cause errors
This commit is contained in:
@@ -72,6 +72,8 @@ class Enterprise < ActiveRecord::Base
|
||||
after_update :welcome_after_confirm, if: lambda { confirmation_token_changed? && confirmation_token.nil? }
|
||||
after_create :send_welcome_email, if: lambda { email_is_known? }
|
||||
|
||||
after_rollback :restore_permalink
|
||||
|
||||
scope :by_name, order('name')
|
||||
scope :visible, where(:visible => true)
|
||||
scope :confirmed, where('confirmed_at IS NOT NULL')
|
||||
@@ -363,4 +365,9 @@ class Enterprise < ActiveRecord::Base
|
||||
errors.add(:shopfront_category_ordering, "must contain a list of taxons.")
|
||||
end
|
||||
end
|
||||
|
||||
def restore_permalink
|
||||
# If the permalink has errors, reset it to it's original value, so we can update the form
|
||||
self.permalink = permalink_was if permalink_changed? && errors[:permalink].present?
|
||||
end
|
||||
end
|
||||
|
||||
@@ -223,6 +223,16 @@ describe Enterprise do
|
||||
it { should delegate(:state_name).to(:address) }
|
||||
end
|
||||
|
||||
describe "callbacks" do
|
||||
it "restores permalink to original value when it is changed and invalid" do
|
||||
e1 = create(:enterprise, permalink: "taken")
|
||||
e2 = create(:enterprise, permalink: "not_taken")
|
||||
e2.permalink = "taken"
|
||||
e2.save
|
||||
expect(e2.permalink).to eq "not_taken"
|
||||
end
|
||||
end
|
||||
|
||||
describe "scopes" do
|
||||
describe 'visible' do
|
||||
it 'find visible enterprises' do
|
||||
|
||||
Reference in New Issue
Block a user