Skip to content

Commit

Permalink
Fixes my hasty fix for the document batch POST
Browse files Browse the repository at this point in the history
Nested forms actually posts numerically indexed hashes, rather than
arrays. Tests updated to reflect this. Original code was working!
  • Loading branch information
adammulligan authored and agnessa committed Sep 10, 2014
1 parent 4e5105e commit dd48717
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/models/document_batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def common_attributes

def documents_attributes=(attributes)
@documents ||= []
attributes.each do |document_params|
attributes.each do |_, document_params|
destroy = ActiveRecord::ConnectionAdapters::Column.value_to_boolean(document_params.delete(:_destroy))
@documents.push(Document.new(common_attributes.merge(document_params))) unless destroy
end
Expand Down
25 changes: 21 additions & 4 deletions spec/controllers/admin/document_batches_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,40 @@
{
'type' => 'Document::Proposal',
filename: Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec', 'support', 'annual_report_upload_exporter.csv')),
number: 1,
_destroy: false
}
}

context "when no event" do
let(:document){ create(:document) }

it "creates a new Document" do
expect {
post :create, document_batch: {
date: Date.today, documents_attributes: { "0" => document_attrs }
}
}.to change(Document, :count).by(1)
end

it "redirects to index when successful" do
post :create, document_batch: {
date: Date.today, documents_attributes: [ document_attrs ]
date: Date.today, documents_attributes: { "0" => document_attrs }
}
response.should redirect_to(admin_documents_url)
end

it "does not create a new Document" do
expect {
post :create, document_batch: {
date: nil, documents_attributes: { "0" => document_attrs }
}
}.to change(Document, :count).by(0)
end

it "renders new when not successful" do
post :create, document_batch: {
date: nil, documents_attributes: [ document_attrs ]
date: nil, documents_attributes: { "0" => document_attrs }
}
response.should render_template('new')
end
Expand All @@ -54,14 +71,14 @@

it "redirects to index when successful" do
post :create, event_id: event.id, document_batch: {
date: Date.today, documents_attributes: [ document_attrs ]
date: Date.today, documents_attributes: { "0" => document_attrs }
}
response.should redirect_to(admin_event_documents_url(event))
end

it "renders new when not successful" do
post :create, event_id: event.id, document_batch: {
date: nil, documents_attributes: [document_attrs]
date: nil, documents_attributes: { "0" => document_attrs }
}
response.should render_template('new')
end
Expand Down

0 comments on commit dd48717

Please sign in to comment.