Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow admins to include images or documents to investments #148

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion app/controllers/admin/budget_investments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class Admin::BudgetInvestmentsController < Admin::BaseController
include FeatureFlags
include CommentableActions
include Translatable
include ImageAttributes
include DocumentAttributes

feature_flag :budgets

Expand Down Expand Up @@ -97,7 +99,9 @@ def budget_investment_params
def allowed_params
attributes = [:external_url, :heading_id, :administrator_id, :tag_list,
:valuation_tag_list, :incompatible, :visible_to_valuators, :selected,
:milestone_tag_list, valuator_ids: [], valuator_group_ids: []]
:milestone_tag_list, valuator_ids: [], valuator_group_ids: [],
image_attributes: image_attributes,
documents_attributes: document_attributes]
[*attributes, translation_params(Budget::Investment)]
end

Expand Down
14 changes: 14 additions & 0 deletions app/views/admin/budget_investments/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@
</div>
<% end %>

<div class="small-12 column">
<% if feature?(:allow_images) %>
<div class="images">
<%= render "images/nested_image", f: f %>
</div>
<% end %>

<% if feature?(:allow_attached_documents) %>
<div class="documents">
<%= render "documents/nested_documents", f: f %>
</div>
<% end %>
</div>

<div class="small-12 column">
<%= f.text_field :tag_list,
value: @investment.tag_list.sort.join(","),
Expand Down
94 changes: 94 additions & 0 deletions spec/shared/system/custom_admin_nested_documentable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
shared_examples "admin 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!(:user) { create(:user, :level_two) }
let!(:arguments) { {} }
let!(:documentable) { create(documentable_factory_name, author: user) }
let!(:user_to_login) { send(login_as_name) }
let(:management) { management }

before do
create(:administrator, user: administrator)

documentable_path_arguments&.each do |argument_name, path_to_value|
arguments.merge!("#{argument_name}": documentable.send(path_to_value))
end
end

describe "at #{path}" do
if path.include? "edit"
scenario "Should show persisted documents and remove nested_field" do
create(:document, documentable: documentable)
do_login_for user_to_login, management: management
visit send(path, arguments)

expect(page).to have_css ".document", count: 1
end

scenario "Should not show add document button when
documentable has reached maximum of documents allowed" do
create_list(:document, documentable.class.max_documents_allowed, documentable: documentable)
do_login_for user_to_login, management: management
visit send(path, arguments)

expect(page).not_to have_css "#new_document_link"
end

scenario "Should show add document button after destroy one document" do
create_list(:document, documentable.class.max_documents_allowed, documentable: documentable)
do_login_for user_to_login, management: management
visit send(path, arguments)
last_document = all("#nested-documents .document").last
within last_document do
click_on "Remove document"
end

expect(page).to have_css "#new_document_link"
end

scenario "Should remove nested field after remove document" do
create(:document, documentable: documentable)
do_login_for user_to_login, management: management
visit send(path, arguments)
click_on "Remove document"

expect(page).not_to have_css ".document"
end

scenario "Same attachment URL after editing the title" do
do_login_for user_to_login, management: management

visit send(path, arguments)
click_link "Add new document"
attach_file "Choose document", file_fixture("empty.pdf")
within_fieldset("Documents") { fill_in "Title", with: "Original" }
click_button submit_button

expect(page).to have_content documentable_success_notice

original_url = find_link("Download file")[:href]

visit send(path, arguments)
within_fieldset("Documents") { fill_in "Title", with: "Updated" }
click_button submit_button

expect(page).to have_content documentable_success_notice
expect(find_link("Download file")[:href]).to eq original_url
end
end

describe "When allow attached documents setting is disabled" do
before do
Setting["feature.allow_attached_documents"] = false
end

scenario "Add new document button should not be available" do
do_login_for user_to_login, management: management
visit send(path, arguments)

expect(page).not_to have_content("Add new document")
end
end
end
end
54 changes: 54 additions & 0 deletions spec/shared/system/custom_admin_nested_imageable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
shared_examples "admin nested imageable" do |imageable_factory_name, path, imageable_path_arguments,
fill_resource_method_name, submit_button, imageable_success_notice,
microweb10 marked this conversation as resolved.
Show resolved Hide resolved
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)

imageable_path_arguments&.each do |argument_name, path_to_value|
arguments.merge!("#{argument_name}": imageable.send(path_to_value))
end

imageable.update!(author: user) if imageable.respond_to?(:author)
end

describe "at #{path}" do
if path.include? "edit"
scenario "show persisted image" do
create(:image, imageable: imageable)
do_login_for user, management: management

visit send(path, arguments)

expect(page).to have_css ".image", count: 1
expect(page).not_to have_css "a#new_image_link"
end

scenario "remove nested field after removing the image" do
create(:image, imageable: imageable)
do_login_for user, management: management

visit send(path, arguments)
click_link "Remove image"

expect(page).not_to have_css ".image"
expect(page).to have_css "a#new_image_link"
end

scenario "don't duplicate fields after removing and adding an image" do
create(:image, imageable: imageable)
do_login_for user, management: management

visit send(path, arguments)
click_link "Remove image"
click_link "Add image"

expect(page).to have_css ".image", count: 1, visible: :all
end
end
end
end
17 changes: 17 additions & 0 deletions spec/system/custom/admin/budget_investments_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
require "rails_helper"

describe "Admin budget investments", :admin do
it_behaves_like "admin nested imageable",
"budget_investment",
"edit_admin_budget_budget_investment_path",
{ budget_id: "budget_id", id: "id" },
"imageable_fill_new_valid_budget_investment",
"Update",
"Investment project updated successfully."

it_behaves_like "admin nested documentable",
"administrator",
"budget_investment",
"edit_admin_budget_budget_investment_path",
{ budget_id: "budget_id", id: "id" },
"documentable_fill_new_valid_budget_investment",
"Update",
"Investment project updated successfully."

context "Show" do
scenario "Show feasible explanation" do
budget_investment = create(:budget_investment, :feasible, feasibility_explanation: "This is awesome!")
Expand Down
Loading