diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index fbb525cfe8..31d306f123 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -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 diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index 9edc1d2cee..8b3a8ab443 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -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