From 82cd019b4089aa5b66450a64555d0d3d9d539980 Mon Sep 17 00:00:00 2001 From: taitus Date: Mon, 29 Mar 2021 12:27:15 +0200 Subject: [PATCH] Allow to create an investment with images In the Management section when creating an investment we were not passing the images attributes, so we were never able to associate images. Make the nested_imageable spec compatible with the Management section. --- .../budgets/investments_controller.rb | 2 + spec/shared/system/nested_imageable.rb | 45 +++++++++++-------- .../management/budget_investments_spec.rb | 9 ++++ 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/app/controllers/management/budgets/investments_controller.rb b/app/controllers/management/budgets/investments_controller.rb index b8baaf1a4..93ca840a2 100644 --- a/app/controllers/management/budgets/investments_controller.rb +++ b/app/controllers/management/budgets/investments_controller.rb @@ -1,5 +1,6 @@ class Management::Budgets::InvestmentsController < Management::BaseController include Translatable + include ImageAttributes include MapLocationAttributes include FeatureFlags feature_flag :budgets @@ -59,6 +60,7 @@ class Management::Budgets::InvestmentsController < Management::BaseController def investment_params attributes = [:external_url, :heading_id, :tag_list, :organization_name, :location, :skip_map, + image_attributes: image_attributes, map_location_attributes: map_location_attributes] params.require(:budget_investment).permit(attributes, translation_params(Budget::Investment)) end diff --git a/spec/shared/system/nested_imageable.rb b/spec/shared/system/nested_imageable.rb index 8f1048a2b..06ba5f63e 100644 --- a/spec/shared/system/nested_imageable.rb +++ b/spec/shared/system/nested_imageable.rb @@ -1,7 +1,10 @@ -shared_examples "nested imageable" do |imageable_factory_name, path, imageable_path_arguments, fill_resource_method_name, submit_button, imageable_success_notice, has_many_images = false| +shared_examples "nested imageable" do |imageable_factory_name, path, imageable_path_arguments, + fill_resource_method_name, submit_button, imageable_success_notice, + has_many_images = false, management: false| let!(:user) { create(:user, :level_two) } let!(:arguments) { {} } let!(:imageable) { create(imageable_factory_name) } + let(:management) { management } before do create(:administrator, user: user) @@ -15,14 +18,14 @@ shared_examples "nested imageable" do |imageable_factory_name, path, imageable_p describe "at #{path}" do scenario "Should show new image link when imageable has not an associated image defined" do - login_as user + do_login_for user visit send(path, arguments) expect(page).to have_selector "#new_image_link" end scenario "Should hide new image link after adding one image" do - login_as user + do_login_for user visit send(path, arguments) click_on "Add image" @@ -31,7 +34,7 @@ shared_examples "nested imageable" do |imageable_factory_name, path, imageable_p end scenario "Should update nested image file name after choosing any file" do - login_as user + do_login_for user visit send(path, arguments) click_link "Add image" @@ -46,7 +49,7 @@ shared_examples "nested imageable" do |imageable_factory_name, path, imageable_p end scenario "Should update nested image file title with file name after choosing a file when no title defined" do - login_as user + do_login_for user visit send(path, arguments) imageable_attach_new_file( @@ -58,7 +61,7 @@ shared_examples "nested imageable" do |imageable_factory_name, path, imageable_p end scenario "Should not update nested image file title with file name after choosing a file when title already defined" do - login_as user + do_login_for user visit send(path, arguments) click_link "Add image" @@ -79,7 +82,7 @@ shared_examples "nested imageable" do |imageable_factory_name, path, imageable_p end scenario "Should update loading bar style after valid file upload" do - login_as user + do_login_for user visit send(path, arguments) imageable_attach_new_file( @@ -91,7 +94,7 @@ shared_examples "nested imageable" do |imageable_factory_name, path, imageable_p end scenario "Should update loading bar style after invalid file upload" do - login_as user + do_login_for user visit send(path, arguments) imageable_attach_new_file( @@ -104,7 +107,7 @@ shared_examples "nested imageable" do |imageable_factory_name, path, imageable_p end scenario "Should update image cached_attachment field after valid file upload" do - login_as user + do_login_for user visit send(path, arguments) imageable_attach_new_file( @@ -116,7 +119,7 @@ shared_examples "nested imageable" do |imageable_factory_name, path, imageable_p end scenario "Should not update image cached_attachment field after invalid file upload" do - login_as user + do_login_for user visit send(path, arguments) imageable_attach_new_file( @@ -129,7 +132,7 @@ shared_examples "nested imageable" do |imageable_factory_name, path, imageable_p end scenario "Should show nested image errors after invalid form submit" do - login_as user + do_login_for user visit send(path, arguments) click_link "Add image" @@ -145,7 +148,7 @@ shared_examples "nested imageable" do |imageable_factory_name, path, imageable_p end scenario "Should remove nested image after valid file upload and click on remove button" do - login_as user + do_login_for user visit send(path, arguments) imageable_attach_new_file( @@ -164,7 +167,7 @@ shared_examples "nested imageable" do |imageable_factory_name, path, imageable_p if has_many_images skip "no need to test, there are no attributes for the parent resource" else - login_as user + do_login_for user visit send(path, arguments) send(fill_resource_method_name) if fill_resource_method_name @@ -174,7 +177,7 @@ shared_examples "nested imageable" do |imageable_factory_name, path, imageable_p end scenario "Should show successful notice when resource filled correctly and after valid file uploads" do - login_as user + do_login_for user visit send(path, arguments) send(fill_resource_method_name) if fill_resource_method_name @@ -191,7 +194,7 @@ shared_examples "nested imageable" do |imageable_factory_name, path, imageable_p end scenario "Should show new image after successful creation with one uploaded file" do - login_as user + do_login_for user visit send(path, arguments) send(fill_resource_method_name) if fill_resource_method_name @@ -216,7 +219,7 @@ shared_examples "nested imageable" do |imageable_factory_name, path, imageable_p if path.include? "edit" scenario "Should show persisted image" do create(:image, imageable: imageable) - login_as user + do_login_for user visit send(path, arguments) expect(page).to have_css ".image", count: 1 @@ -224,7 +227,7 @@ shared_examples "nested imageable" do |imageable_factory_name, path, imageable_p scenario "Should not show add image button when image already exists" do create(:image, imageable: imageable) - login_as user + do_login_for user visit send(path, arguments) expect(page).not_to have_css "a#new_image_link" @@ -232,7 +235,7 @@ shared_examples "nested imageable" do |imageable_factory_name, path, imageable_p scenario "Should remove nested field after remove image" do create(:image, imageable: imageable) - login_as user + do_login_for user visit send(path, arguments) click_on "Remove image" @@ -241,7 +244,7 @@ shared_examples "nested imageable" do |imageable_factory_name, path, imageable_p scenario "Should show add image button after remove image" do create(:image, imageable: imageable) - login_as user + do_login_for user visit send(path, arguments) click_on "Remove image" @@ -251,6 +254,10 @@ shared_examples "nested imageable" do |imageable_factory_name, path, imageable_p end end +def do_login_for(user) + common_do_login_for(user, management: management) +end + def imageable_redirected_to_resource_show_or_navigate_to find("a", text: "Not now, go to my proposal") click_on "Not now, go to my proposal" diff --git a/spec/system/management/budget_investments_spec.rb b/spec/system/management/budget_investments_spec.rb index 53bf0f87a..e6cc6e5d0 100644 --- a/spec/system/management/budget_investments_spec.rb +++ b/spec/system/management/budget_investments_spec.rb @@ -7,6 +7,15 @@ describe "Budget Investments" do let(:heading) { create(:budget_heading, group: group, name: "Health") } let(:user) { create(:user, :level_two) } + it_behaves_like "nested imageable", + "budget_investment", + "new_management_budget_investment_path", + { "budget_id": "budget_id" }, + "imageable_fill_new_valid_budget_investment", + "Create Investment", + "Investment created successfully.", + management: true + it_behaves_like "mappable", "budget_investment", "investment",