Allow to create an investment with documents

In the Management section when creating an investment we were not passing the
document attributes, so we were never able to associate documents.

Make the nested_documentable spec compatible with the Management section.
This commit is contained in:
taitus
2021-03-29 13:13:11 +02:00
parent ac144d172d
commit be6390cc71
3 changed files with 43 additions and 24 deletions

View File

@@ -1,6 +1,7 @@
class Management::Budgets::InvestmentsController < Management::BaseController class Management::Budgets::InvestmentsController < Management::BaseController
include Translatable include Translatable
include ImageAttributes include ImageAttributes
include DocumentAttributes
include MapLocationAttributes include MapLocationAttributes
include FeatureFlags include FeatureFlags
feature_flag :budgets feature_flag :budgets
@@ -61,6 +62,7 @@ class Management::Budgets::InvestmentsController < Management::BaseController
def investment_params def investment_params
attributes = [:external_url, :heading_id, :tag_list, :organization_name, :location, :skip_map, attributes = [:external_url, :heading_id, :tag_list, :organization_name, :location, :skip_map,
image_attributes: image_attributes, image_attributes: image_attributes,
documents_attributes: document_attributes,
map_location_attributes: map_location_attributes] map_location_attributes: map_location_attributes]
params.require(:budget_investment).permit(attributes, translation_params(Budget::Investment)) params.require(:budget_investment).permit(attributes, translation_params(Budget::Investment))
end end

View File

@@ -1,4 +1,6 @@
shared_examples "nested documentable" do |login_as_name, documentable_factory_name, path, documentable_path_arguments, fill_resource_method_name, submit_button, documentable_success_notice| shared_examples "nested documentable" do |login_as_name, documentable_factory_name, path,
documentable_path_arguments, fill_resource_method_name,
submit_button, documentable_success_notice, management: false|
let!(:administrator) { create(:user) } let!(:administrator) { create(:user) }
let!(:user) { create(:user, :level_two) } let!(:user) { create(:user, :level_two) }
let!(:arguments) { {} } let!(:arguments) { {} }
@@ -8,6 +10,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
let!(:documentable) { create(documentable_factory_name, author: user) } let!(:documentable) { create(documentable_factory_name, author: user) }
end end
let!(:user_to_login) { send(login_as_name) } let!(:user_to_login) { send(login_as_name) }
let(:management) { management }
before do before do
create(:administrator, user: administrator) create(:administrator, user: administrator)
@@ -19,7 +22,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
describe "at #{path}" do describe "at #{path}" do
scenario "Should show new document link when max documents allowed limit is not reached" do scenario "Should show new document link when max documents allowed limit is not reached" do
login_as user_to_login do_login_for user_to_login
visit send(path, arguments) visit send(path, arguments)
expect(page).to have_css "#new_document_link" expect(page).to have_css "#new_document_link"
@@ -27,7 +30,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
scenario "Should not show new document link when scenario "Should not show new document link when
documentable max documents allowed limit is reached" do documentable max documents allowed limit is reached" do
login_as user_to_login do_login_for user_to_login
visit send(path, arguments) visit send(path, arguments)
documentable.class.max_documents_allowed.times.each do documentable.class.max_documents_allowed.times.each do
@@ -38,14 +41,14 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
end end
scenario "Should not show max documents warning when no documents added" do scenario "Should not show max documents warning when no documents added" do
login_as user_to_login do_login_for user_to_login
visit send(path, arguments) visit send(path, arguments)
expect(page).not_to have_css ".max-documents-notice" expect(page).not_to have_css ".max-documents-notice"
end end
scenario "Should show max documents warning when max documents allowed limit is reached" do scenario "Should show max documents warning when max documents allowed limit is reached" do
login_as user_to_login do_login_for user_to_login
visit send(path, arguments) visit send(path, arguments)
documentable.class.max_documents_allowed.times.each do documentable.class.max_documents_allowed.times.each do
documentable_attach_new_file(Rails.root.join("spec/fixtures/files/empty.pdf")) documentable_attach_new_file(Rails.root.join("spec/fixtures/files/empty.pdf"))
@@ -56,7 +59,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
end end
scenario "Should hide max documents warning after any document removal" do scenario "Should hide max documents warning after any document removal" do
login_as user_to_login do_login_for user_to_login
visit send(path, arguments) visit send(path, arguments)
documentable.class.max_documents_allowed.times.each do documentable.class.max_documents_allowed.times.each do
@@ -69,7 +72,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
end end
scenario "Should update nested document file name after choosing a file" do scenario "Should update nested document file name after choosing a file" do
login_as user_to_login do_login_for user_to_login
visit send(path, arguments) visit send(path, arguments)
click_link "Add new document" click_link "Add new document"
@@ -89,7 +92,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
scenario "Should update nested document file title with scenario "Should update nested document file title with
file name after choosing a file when no title defined" do file name after choosing a file when no title defined" do
login_as user_to_login do_login_for user_to_login
visit send(path, arguments) visit send(path, arguments)
documentable_attach_new_file(Rails.root.join("spec/fixtures/files/empty.pdf")) documentable_attach_new_file(Rails.root.join("spec/fixtures/files/empty.pdf"))
@@ -99,7 +102,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
scenario "Should not update nested document file title with scenario "Should not update nested document file title with
file name after choosing a file when title already defined" do file name after choosing a file when title already defined" do
login_as user_to_login do_login_for user_to_login
visit send(path, arguments) visit send(path, arguments)
click_link "Add new document" click_link "Add new document"
@@ -120,7 +123,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
end end
scenario "Should update loading bar style after valid file upload" do scenario "Should update loading bar style after valid file upload" do
login_as user_to_login do_login_for user_to_login
visit send(path, arguments) visit send(path, arguments)
documentable_attach_new_file(Rails.root.join("spec/fixtures/files/empty.pdf")) documentable_attach_new_file(Rails.root.join("spec/fixtures/files/empty.pdf"))
@@ -129,7 +132,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
end end
scenario "Should update loading bar style after invalid file upload" do scenario "Should update loading bar style after invalid file upload" do
login_as user_to_login do_login_for user_to_login
visit send(path, arguments) visit send(path, arguments)
documentable_attach_new_file( documentable_attach_new_file(
@@ -141,7 +144,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
end end
scenario "Should update document cached_attachment field after valid file upload" do scenario "Should update document cached_attachment field after valid file upload" do
login_as user_to_login do_login_for user_to_login
visit send(path, arguments) visit send(path, arguments)
documentable_attach_new_file(Rails.root.join("spec/fixtures/files/empty.pdf")) documentable_attach_new_file(Rails.root.join("spec/fixtures/files/empty.pdf"))
@@ -150,7 +153,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
end end
scenario "Should not update document cached_attachment field after invalid file upload" do scenario "Should not update document cached_attachment field after invalid file upload" do
login_as user_to_login do_login_for user_to_login
visit send(path, arguments) visit send(path, arguments)
documentable_attach_new_file( documentable_attach_new_file(
@@ -163,7 +166,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
scenario "Should show document errors after documentable submit with scenario "Should show document errors after documentable submit with
empty document fields" do empty document fields" do
login_as user_to_login do_login_for user_to_login
visit send(path, arguments) visit send(path, arguments)
click_link "Add new document" click_link "Add new document"
@@ -175,7 +178,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
end end
scenario "Should delete document after valid file upload and click on remove button" do scenario "Should delete document after valid file upload and click on remove button" do
login_as user_to_login do_login_for user_to_login
visit send(path, arguments) visit send(path, arguments)
documentable_attach_new_file(Rails.root.join("spec/fixtures/files/empty.pdf")) documentable_attach_new_file(Rails.root.join("spec/fixtures/files/empty.pdf"))
@@ -186,7 +189,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
scenario "Should show successful notice when scenario "Should show successful notice when
resource filled correctly without any nested documents" do resource filled correctly without any nested documents" do
login_as user_to_login do_login_for user_to_login
visit send(path, arguments) visit send(path, arguments)
send(fill_resource_method_name) if fill_resource_method_name send(fill_resource_method_name) if fill_resource_method_name
@@ -197,7 +200,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
scenario "Should show successful notice when scenario "Should show successful notice when
resource filled correctly and after valid file uploads" do resource filled correctly and after valid file uploads" do
login_as user_to_login do_login_for user_to_login
visit send(path, arguments) visit send(path, arguments)
send(fill_resource_method_name) if fill_resource_method_name send(fill_resource_method_name) if fill_resource_method_name
@@ -211,7 +214,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
if documentable_factory_name == "dashboard_action" if documentable_factory_name == "dashboard_action"
skip("Not render Documents count on dashboard_actions") skip("Not render Documents count on dashboard_actions")
end end
login_as user_to_login do_login_for user_to_login
visit send(path, arguments) visit send(path, arguments)
send(fill_resource_method_name) if fill_resource_method_name send(fill_resource_method_name) if fill_resource_method_name
@@ -233,7 +236,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
if documentable_factory_name == "dashboard_action" if documentable_factory_name == "dashboard_action"
skip("Not render Documents count on dashboard_actions") skip("Not render Documents count on dashboard_actions")
end end
login_as user_to_login do_login_for user_to_login
visit send(path, arguments) visit send(path, arguments)
send(fill_resource_method_name) if fill_resource_method_name send(fill_resource_method_name) if fill_resource_method_name
@@ -251,7 +254,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
if path.include? "edit" if path.include? "edit"
scenario "Should show persisted documents and remove nested_field" do scenario "Should show persisted documents and remove nested_field" do
create(:document, documentable: documentable) create(:document, documentable: documentable)
login_as user_to_login do_login_for user_to_login
visit send(path, arguments) visit send(path, arguments)
expect(page).to have_css ".document", count: 1 expect(page).to have_css ".document", count: 1
@@ -260,7 +263,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
scenario "Should not show add document button when scenario "Should not show add document button when
documentable has reached maximum of documents allowed" do documentable has reached maximum of documents allowed" do
create_list(:document, documentable.class.max_documents_allowed, documentable: documentable) create_list(:document, documentable.class.max_documents_allowed, documentable: documentable)
login_as user_to_login do_login_for user_to_login
visit send(path, arguments) visit send(path, arguments)
expect(page).not_to have_css "#new_document_link" expect(page).not_to have_css "#new_document_link"
@@ -268,7 +271,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
scenario "Should show add document button after destroy one document" do scenario "Should show add document button after destroy one document" do
create_list(:document, documentable.class.max_documents_allowed, documentable: documentable) create_list(:document, documentable.class.max_documents_allowed, documentable: documentable)
login_as user_to_login do_login_for user_to_login
visit send(path, arguments) visit send(path, arguments)
last_document = all("#nested-documents .document").last last_document = all("#nested-documents .document").last
within last_document do within last_document do
@@ -280,7 +283,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
scenario "Should remove nested field after remove document" do scenario "Should remove nested field after remove document" do
create(:document, documentable: documentable) create(:document, documentable: documentable)
login_as user_to_login do_login_for user_to_login
visit send(path, arguments) visit send(path, arguments)
click_on "Remove document" click_on "Remove document"
@@ -294,7 +297,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
end end
scenario "Add new document button should not be available" do scenario "Add new document button should not be available" do
login_as user_to_login do_login_for user_to_login
visit send(path, arguments) visit send(path, arguments)
expect(page).not_to have_content("Add new document") expect(page).not_to have_content("Add new document")
@@ -303,6 +306,10 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
end end
end end
def do_login_for(user)
common_do_login_for(user, management: management)
end
def documentable_redirected_to_resource_show_or_navigate_to def documentable_redirected_to_resource_show_or_navigate_to
find("a", text: "Not now, go to my proposal") find("a", text: "Not now, go to my proposal")
click_on "Not now, go to my proposal" click_on "Not now, go to my proposal"

View File

@@ -7,6 +7,16 @@ describe "Budget Investments" do
let(:heading) { create(:budget_heading, group: group, name: "Health") } let(:heading) { create(:budget_heading, group: group, name: "Health") }
let(:user) { create(:user, :level_two) } let(:user) { create(:user, :level_two) }
it_behaves_like "nested documentable",
"user",
"budget_investment",
"new_management_budget_investment_path",
{ "budget_id": "budget_id" },
"documentable_fill_new_valid_budget_investment",
"Create Investment",
"Investment created successfully.",
management: true
it_behaves_like "nested imageable", it_behaves_like "nested imageable",
"budget_investment", "budget_investment",
"new_management_budget_investment_path", "new_management_budget_investment_path",