Set maximum number of documents on proposal and budget investments. Show alert on documents tab when maximum reached. Hide upload button when maximum reached.

This commit is contained in:
Senén Rodero Rodríguez
2017-07-21 18:44:33 +02:00
parent 462931845b
commit 6b61cbe35c
10 changed files with 46 additions and 6 deletions

View File

@@ -1,6 +1,5 @@
class Budget
class Investment < ActiveRecord::Base
include Measurable
include Sanitizable
include Taggable
@@ -13,6 +12,8 @@ class Budget
acts_as_paranoid column: :hidden_at
include ActsAsParanoidAliases
MAX_DOCUMENTS_SIZE = 3
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
belongs_to :heading
belongs_to :group

View File

@@ -15,6 +15,7 @@ class Proposal < ActiveRecord::Base
acts_as_paranoid column: :hidden_at
include ActsAsParanoidAliases
MAX_DOCUMENTS_SIZE = 3
RETIRE_OPTIONS = %w(duplicated started unfeasible done other)
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'

View File

@@ -4,7 +4,7 @@
<div class="small-12 medium-9 column">
<%= back_link_to budget_investments_path(investment.budget, heading_id: investment.heading) %>
<% if can? :create, @document %>
<% if can?(:create, @document) && investment.documents.size < Budget::Investment::MAX_DOCUMENTS_SIZE %>
<%= link_to t("documents.upload_document"),
new_document_path(documentable_id:investment, documentable_type: investment.class.name, from: request.url),
class: 'button hollow float-right' %>

View File

@@ -21,7 +21,9 @@
</div>
<div class="tabs-panel" id="tab-documents">
<%= render 'documents/documents', documents: @investment.documents %>
<%= render 'documents/documents',
documents: @investment.documents,
max_documents_size: Budget::Investment::MAX_DOCUMENTS_SIZE %>
</div>
</div>

View File

@@ -1,4 +1,15 @@
<% if documents.any? %>
<% if documents.size == max_documents_size %>
<div class="row documents-list">
<div class="small-12 column">
<div class="callout primary text-center">
<%= t "documents.max_documents_size_reached" %>
</div>
</div>
</div>
<% end %>
<div class="row documents-list">
<div class="small-12 column">
<table>
@@ -10,7 +21,9 @@
</table>
</div>
</div>
<% else %>
<div class="row">
<div class="small-12 column">
<div class="callout primary text-center">
@@ -18,4 +31,5 @@
</div>
</div>
</div>
<% end %>

View File

@@ -16,7 +16,7 @@
<div class="small-12 medium-9 column">
<%= back_link_to %>
<% if can? :create, @document %>
<% if can?(:create, @document) && @proposal.documents.size < Proposal::MAX_DOCUMENTS_SIZE %>
<%= link_to t("documents.upload_document"),
new_document_path(documentable_id: @proposal, documentable_type: @proposal.class.name, from: request.url),
class: 'button hollow float-right' %>
@@ -164,6 +164,8 @@
</div>
<div class="tabs-panel" id="tab-documents">
<%= render 'documents/documents', documents: @proposal.documents %>
<%= render 'documents/documents',
documents: @proposal.documents,
max_documents_size: Proposal::MAX_DOCUMENTS_SIZE %>
</div>
</div>

View File

@@ -3,6 +3,7 @@ en:
tab: Documents
no_documents: Don't have uploaded documents
upload_document: Upload document
max_documents_size_reached: You have reached the maximum number of documents allowed! You have to delete one before you can upload another.
form:
attachment_label: Choose attachment file
submit_button: Upload document

View File

@@ -3,6 +3,7 @@ es:
tab: Documentos
no_documents: No hay documentos subidos
upload_document: Subir documento
max_documents_size_reached: ¡Has alcanzado el número máximo de documentos permitidos! Tienes que eliminar uno antes de poder subir otro.
form:
attachment_label: Selecciona un archivo
submit_button: Subir documento

View File

@@ -429,7 +429,7 @@ feature 'Budget Investments' do
end
end
it_behaves_like "followable", "budget_investment", "budget_investment_path", {"budget_id": "budget_id", "id": "id"}
it_behaves_like "followable", "budget_investment", "budget_investment_path", { "budget_id": "budget_id", "id": "id" }
it_behaves_like "documentable", "budget_investment", "budget_investment_path", {"budget_id": "budget_id", "id": "id"}

View File

@@ -25,6 +25,15 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
end
end
scenario "Should not display upload document button when maximum number of documents reached " do
create_list(:document, 3, documentable: documentable)
visit send(documentable_path, arguments)
within "##{dom_id(documentable)}" do
expect(page).not_to have_link("Upload document")
end
end
scenario "Should display upload document button when user is logged in and is documentable owner" do
login_as(user)
@@ -58,6 +67,15 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
let!(:document) { create(:document, documentable: documentable, user: documentable.author)}
scenario "Should display maximum number of documents alert when reached" do
create_list(:document, 2, documentable: documentable)
visit send(documentable_path, arguments)
within "#tab-documents" do
expect(page).to have_content "You have reached the maximum number of documents allowed! You have to delete one before you can upload another."
end
end
describe "Download action" do
scenario "Should be able to anyone" do