Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit bfd0cb17e75bfaeb67d6a1e923378bb7d072acb9 1 parent 27ba5f7
@tomafro tomafro authored
View
2  app/controllers/admin/documents_controller.rb
@@ -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
6 app/models/document.rb
@@ -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
View
1  test/functional/admin/documents_controller_test.rb
@@ -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
View
8 test/unit/document_test.rb
@@ -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?
Please sign in to comment.
Something went wrong with that request. Please try again.