diff --git a/app/controllers/budgets/investments_controller.rb b/app/controllers/budgets/investments_controller.rb index 2170caa50..799d1f76d 100644 --- a/app/controllers/budgets/investments_controller.rb +++ b/app/controllers/budgets/investments_controller.rb @@ -49,6 +49,7 @@ module Budgets def create @investment.author = current_user + recover_documents_from_cache(@investment) if @investment.save Mailer.budget_investment_created(@investment).deliver_later @@ -105,7 +106,8 @@ module Budgets def investment_params params.require(:budget_investment).permit(:title, :description, :external_url, :heading_id, :tag_list, - :organization_name, :location, :terms_of_service) + :organization_name, :location, :terms_of_service, + documents_attributes: [:id, :title, :attachment, :cached_attachment, :user_id]) end def load_ballot diff --git a/app/helpers/documents_helper.rb b/app/helpers/documents_helper.rb index ec2541aaa..819d4a00f 100644 --- a/app/helpers/documents_helper.rb +++ b/app/helpers/documents_helper.rb @@ -23,13 +23,13 @@ module DocumentsHelper end def document_nested_field_name(document, index, field) - parent = document.documentable_type.constantize.name.downcase - "#{parent}[documents_attributes][#{index}][#{field}]" + parent = document.documentable_type.parameterize.underscore + "#{parent.parameterize}[documents_attributes][#{index}][#{field}]" end def document_nested_field_id(document, index, field) - parent = document.documentable_type.constantize.name.downcase - "#{parent}_documents_attributes_#{index}_#{field}" + parent = document.documentable_type.parameterize.underscore + "#{parent.parameterize}_documents_attributes_#{index}_#{field}" end def document_nested_field_wrapper_id(index) diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index 1fb0c1b26..0dfd7836c 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -10,6 +10,7 @@ class Budget documentable max_documents_allowed: 3, max_file_size: 3.megabytes, accepted_content_types: [ "application/pdf" ] + accepts_nested_attributes_for :documents, allow_destroy: true acts_as_votable acts_as_paranoid column: :hidden_at diff --git a/app/views/budgets/investments/_form.html.erb b/app/views/budgets/investments/_form.html.erb index 94f6283e9..40685dff7 100644 --- a/app/views/budgets/investments/_form.html.erb +++ b/app/views/budgets/investments/_form.html.erb @@ -21,6 +21,10 @@ <%= f.text_field :external_url %> +
+ <%= render 'documents/nested_documents', resource: @investment %> +
+
<%= f.text_field :location %>
diff --git a/spec/features/budgets/investments_spec.rb b/spec/features/budgets/investments_spec.rb index 6660e5300..73a6f3677 100644 --- a/spec/features/budgets/investments_spec.rb +++ b/spec/features/budgets/investments_spec.rb @@ -433,6 +433,14 @@ feature 'Budget Investments' do it_behaves_like "documentable", "budget_investment", "budget_investment_path", {"budget_id": "budget_id", "id": "id"} + it_behaves_like "nested documentable", + "budget_investment", + "new_budget_investment_path", + { "budget_id": "budget_id" }, + "fill_new_valid_budget_investment", + "Create Investment", + "Budget Investment created successfully." + context "Destroy" do scenario "Admin cannot destroy budget investments" do diff --git a/spec/shared/features/nested_documentable.rb b/spec/shared/features/nested_documentable.rb index 734175dff..f7eab0875 100644 --- a/spec/shared/features/nested_documentable.rb +++ b/spec/shared/features/nested_documentable.rb @@ -4,7 +4,7 @@ shared_examples "nested documentable" do |documentable_factory_name, path, docum include DocumentablesHelper let!(:administrator) { create(:user) } - let!(:user) { create(:user) } + let!(:user) { create(:user, :level_two) } let!(:arguments) { {} } let!(:documentable) { create(documentable_factory_name, author: user) } @@ -16,7 +16,6 @@ shared_examples "nested documentable" do |documentable_factory_name, path, docum arguments.merge!("#{argument_name}": documentable.send(path_to_value)) end end - end scenario "Should show new document link when max documents allowed limit is not reached" do @@ -263,7 +262,13 @@ def fill_new_valid_proposal fill_in :proposal_title, with: "Proposal title" fill_in :proposal_summary, with: "Proposal summary" fill_in :proposal_question, with: "Proposal question?" - fill_in :proposal_responsible_name, with: 'John Snow' check :proposal_terms_of_service end +def fill_new_valid_budget_investment + page.select documentable.heading.name_scoped_by_group, from: :budget_investment_heading_id + fill_in :budget_investment_title, with: "Budget investment title" + fill_in_ckeditor "budget_investment_description", with: "Budget investment description" + check :budget_investment_terms_of_service +end +