Restoring enterprise permalinks when they cause errors

This commit is contained in:
Rob Harrington
2015-01-16 18:17:15 +11:00
parent 4088bdc236
commit 6b10a4a775
2 changed files with 17 additions and 0 deletions

View File

@@ -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

View File

@@ -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