Permalink
Browse files

Allow creation of new document editions when the parent is invalid

The parent can become invalid if a validation has been added since it was published.
  • Loading branch information...
tomafro committed Feb 3, 2012
1 parent 27ba5f7 commit bfd0cb17e75bfaeb67d6a1e923378bb7d072acb9
@@ -72,7 +72,7 @@ def submit
def revise
document = @document.create_draft(current_user)
- if document.valid?
+ if document.persisted?
redirect_to edit_admin_document_path(document)
else
redirect_to edit_admin_document_path(@document.document_identity.unpublished_edition),
View
@@ -106,8 +106,10 @@ def create_draft(user)
draft_attributes = attributes.except('state', 'created_at', 'updated_at', 'change_note')
self.class.new(draft_attributes.merge('state' => 'draft', 'creator' => user)).tap do |draft|
traits.each { |t| t.process_associations_before_save(draft) }
- if draft.save
- traits.each { |t| t.process_associations_after_save(draft) }
+ if draft.valid? || !draft.errors.keys.include?(:base)
+ if draft.save(validate: false)
+ traits.each { |t| t.process_associations_after_save(draft) }
+ end
end
end
end
@@ -76,7 +76,6 @@ class Admin::DocumentsControllerTest < ActionController::TestCase
Document.stubs(:find).returns(published_document)
draft_document = create(:draft_policy)
published_document.expects(:create_draft).with(current_user).returns(draft_document)
- draft_document.expects(:valid?).returns(true)
post :revise, id: published_document
end
@@ -478,6 +478,14 @@ class DocumentTest < ActiveSupport::TestCase
assert_equal 31, new_association.ordering
end
+ test "should build a draft copy even if parent is invalid" do
+ published_policy = create(:published_policy)
+ published_policy.update_attribute(:title, nil)
+ refute published_policy.valid?
+ draft_policy = published_policy.create_draft(create(:policy_writer))
+ assert draft_policy.persisted?
+ end
+
test "when initially created" do
document = create(:document)
assert document.draft?

0 comments on commit bfd0cb1

Please sign in to comment.