Create documentable concern and add it to budget investment model. Create documents controller, documents helper, routes, translations and specs

This commit is contained in:
Senén Rodero Rodríguez
2017-07-21 13:46:13 +02:00
parent c92827e89e
commit a2130689ed
22 changed files with 505 additions and 1 deletions

View File

@@ -23,8 +23,19 @@ App.Forms =
false false
) )
uploadButton: ->
element = $('input[type=file]')
i = 0
while i < element.length
element[i].addEventListener 'change', ->
idButton = $(this)
idButton.closest('.file-name').find('p').text(@files[0].name)
return
i++
initialize: -> initialize: ->
App.Forms.disableEnter() App.Forms.disableEnter()
App.Forms.submitOnChange() App.Forms.submitOnChange()
App.Forms.toggleLink() App.Forms.toggleLink()
App.Forms.uploadButton()
false false

View File

@@ -97,6 +97,10 @@
content: '\72'; content: '\72';
} }
.icon-documents::before {
content: '\68';
}
.icon-proposals::before { .icon-proposals::before {
content: '\68'; content: '\68';
} }

View File

@@ -44,6 +44,7 @@ module Budgets
set_comment_flags(@comment_tree.comments) set_comment_flags(@comment_tree.comments)
load_investment_votes(@investment) load_investment_votes(@investment)
@investment_ids = [@investment.id] @investment_ids = [@investment.id]
@document = Document.new(documentable: @investment)
end end
def create def create

View File

@@ -0,0 +1,47 @@
class DocumentsController < ApplicationController
before_action :authenticate_user!
before_filter :find_documentable, except: :destroy
before_filter :prepare_new_document, only: :new
before_filter :prepare_document_for_creation, only: :create
load_and_authorize_resource
def new
end
def create
if @document.save
flash[:notice] = t "documents.actions.create.notice"
redirect_to params[:from]
else
flash[:alert] = t "documents.actions.create.alert"
render :new
end
end
def destroy
@document.destroy
flash[:notice] = t "documents.actions.destroy.notice"
redirect_to params[:from]
end
private
def find_documentable
@documentable = params[:documentable_type].constantize.find(params[:documentable_id])
end
def prepare_new_document
@document = Document.new(documentable: @documentable, user_id: @documentable.author_id)
end
def prepare_document_for_creation
@document = Document.new(document_params)
@document.documentable = @documentable
@document.user = current_user
end
def document_params
params.require(:document).permit(:title, :documentable_type, :documentable_id, :attachment)
end
end

View File

@@ -0,0 +1,15 @@
module DocumentsHelper
def document_attachment_file_name(document)
document.attachment.attachment_file_name if document.attachment.exists?
end
def errors_on_attachment(document)
document.errors[:attachment].join(', ') if document.errors.key?(:attachment)
end
def document_documentable_class(document)
document.documentable.class.name.parameterize('_')
end
end

View File

@@ -7,6 +7,7 @@ class Budget
include Searchable include Searchable
include Reclassification include Reclassification
include Followable include Followable
include Documentable
acts_as_votable acts_as_votable
acts_as_paranoid column: :hidden_at acts_as_paranoid column: :hidden_at

View File

@@ -17,6 +17,15 @@
</h3> </h3>
<% end %> <% end %>
</li> </li>
<li class="tabs-title">
<%= link_to "#tab-documents" do %>
<h3>
<%= t("documents.tab") %>
(<%= @investment.documents.count %>)
</h3>
<% end %>
</li>
</ul> </ul>
</div> </div>
</div> </div>

View File

@@ -4,6 +4,12 @@
<div class="small-12 medium-9 column"> <div class="small-12 medium-9 column">
<%= back_link_to budget_investments_path(investment.budget, heading_id: investment.heading) %> <%= back_link_to budget_investments_path(investment.budget, heading_id: investment.heading) %>
<% if can? :create, @document %>
<%= 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' %>
<% end %>
<h1><%= investment.title %></h1> <h1><%= investment.title %></h1>
<div class="budget-investment-info"> <div class="budget-investment-info">
@@ -14,7 +20,6 @@
<span class="bullet">&nbsp;&bull;&nbsp;</span> <span class="bullet">&nbsp;&bull;&nbsp;</span>
<%= investment.heading.name %> <%= investment.heading.name %>
</div> </div>
<br> <br>
<p id="investment_code"> <p id="investment_code">
<%= t("budgets.investments.show.code_html", code: investment.id) %> <%= t("budgets.investments.show.code_html", code: investment.id) %>
@@ -51,6 +56,7 @@
<h2><%= t('budgets.investments.show.price_explanation') %></h2> <h2><%= t('budgets.investments.show.price_explanation') %></h2>
<p><%= investment.price_explanation %></p> <p><%= investment.price_explanation %></p>
<% end %> <% end %>
</div> </div>
<aside class="small-12 medium-3 column"> <aside class="small-12 medium-3 column">

View File

@@ -19,4 +19,9 @@
comment_flags: @comment_flags, comment_flags: @comment_flags,
display_comments_count: false } %> display_comments_count: false } %>
</div> </div>
<div class="tabs-panel" id="tab-documents">
<%= render 'documents/documents', documents: @investment.documents %>
</div>
</div> </div>

View File

@@ -0,0 +1,14 @@
<tr id="<%= dom_id(document)%>">
<td><%#= icon %></td>
<td><%= document.title %></td>
<td>
<%= link_to "Download", document.attachment.url %>
<% if can? :destroy, Document %>
<%= link_to "Destroy",
document_path(document, from: request.url), method: :delete,
data: { confirm: "¿Está segurto de que desea eliminar el documento?. Esta acción no se puede deshacer." } %>
<% end %>
</td>
</tr>

View File

@@ -0,0 +1,17 @@
<% if documents.any? %>
<table>
<tbody>
<% documents.each do |document| %>
<%= render "documents/document", document: document %>
<% end %>
</tbody>
</table>
<% else %>
<div class="callout primary text-center">
<%= t('documents.no_documents') %>
</div>
<% end %>

View File

@@ -0,0 +1,38 @@
<%= form_for @document,
url: documents_path(
documentable_type: @document.documentable_type,
documentable_id: @document.documentable_id,
from: params[:from]
),
html: { multipart: true } do |f| %>
<%= render 'shared/errors', resource: @document %>
<div class="row">
<div class="small-12 column">
<%= f.text_field :title %>
</div>
<div class="small-12 column">
<div class="file-name">
<%= f.file_field :attachment, label: false, class:'show-for-sr' %>
<br>
<%= f.label :attachment, t("documents.form.attachment_label"), class:'button' %>
<p><%= document_attachment_file_name(@document) %></p>
</div>
</div>
<% if @document.errors.has_key?(:attachment) %>
<div class="small-12 column">
<div class="attachment-errors">
<small class="error"><%= errors_on_attachment(@document)%></small>
</div>
</div>
<% end %>
<div class="actions small-12 medium-6 large-4 end column">
<%= f.submit(t("documents.form.submit_button"), class: "button expanded") %>
</div>
</div>
<% end %>

View File

@@ -0,0 +1,20 @@
<div class="document-form <%= @document.documentable.class.name.parameterize('_') %> row">
<div class="small-12 medium-9 column">
<%= render "shared/back_link" %>
<h1><%= t("documents.new.title") %></h1>
<%= render "documents/form", form_url: documents_url %>
</div>
<div class="small-12 medium-3 column">
<span class="icon-documents float-right"></span>
<h2><%= t("documents.recommendations_title") %></h2>
<ul class="recommendations">
<li><%= t("documents.recommendation_one_html") %></li>
<li><%= t("documents.recommendation_two_html") %></li>
<li><%= t("documents.recommendation_three_html") %></li>
</ul>
</div>
</div>

View File

@@ -76,6 +76,9 @@ en:
legislation/answers: legislation/answers:
one: "Answer" one: "Answer"
other: "Answers" other: "Answers"
documents:
one: "Document"
other: "Documents"
attributes: attributes:
budget: budget:
name: "Name" name: "Name"
@@ -197,6 +200,9 @@ en:
value: Value value: Value
legislation/annotation: legislation/annotation:
text: Comment text: Comment
document:
title: Title
attachment: Attachment
errors: errors:
models: models:
user: user:

View File

@@ -0,0 +1,21 @@
en:
documents:
tab: Documents
no_documents: Don't have uploaded documents
upload_document: Upload document
form:
attachment_label: Choose attachment file
submit_button: Upload document
new:
title: Upload document
recommendations_title: File upload tips
recommendation_one_html: You can upload up to a <strong>maximum of 3 files</strong>
recommendation_two_html: You can upload <strong>.pdf</strong> files only
recommendation_three_html: You can upload files up to <strong>3 MB</strong>
actions:
create:
notice: Document was created successfully.
alert: Cannot create document. Check form errors and try again.
destroy:
notice: Document was deleted successfully.
alert: Cannot destroy document.

View File

@@ -165,6 +165,7 @@ en:
user: Account user: Account
verification/sms: phone verification/sms: phone
signature_sheet: Signature sheet signature_sheet: Signature sheet
document: Document
geozones: geozones:
none: All city none: All city
all: All scopes all: All scopes

View File

@@ -76,6 +76,9 @@ es:
legislation/answers: legislation/answers:
one: "Respuesta" one: "Respuesta"
other: "Respuestas" other: "Respuestas"
documents:
one: "Documento"
other: "Documentos"
attributes: attributes:
budget: budget:
name: "Nombre" name: "Nombre"
@@ -192,6 +195,9 @@ es:
value: Valor value: Valor
legislation/annotation: legislation/annotation:
text: Comentario text: Comentario
document:
title: Título
attachment: Archivo adjunto
errors: errors:
models: models:
user: user:

View File

@@ -0,0 +1,21 @@
es:
documents:
tab: Documentos
no_documents: No hay documentos subidos
upload_document: Subir documento
form:
attachment_label: Selecciona un archivo
submit_button: Subir documento
new:
title: Subir un documento
recommendations_title: Consejos para subir archivos
recommendation_one_html: Puedes subir hasta un máximo de <strong>3 ficheros</strong>
recommendation_two_html: Sólo puedes subir archivos <strong>pdf</strong>
recommendation_three_html: Puedes subir archivos de hasta <strong>3MB</strong>
actions:
create:
notice: "El documento se ha creado correctamente."
alert: "El documento no se ha podido crear. Revise los errores del formulario."
destroy:
notice: "El documento se ha eliminado correctamente."
alert: "El documento no se ha podido eliminar."

View File

@@ -165,6 +165,7 @@ es:
user: la cuenta user: la cuenta
verification/sms: el teléfono verification/sms: el teléfono
signature_sheet: la hoja de firmas signature_sheet: la hoja de firmas
document: el documento
geozones: geozones:
none: Toda la ciudad none: Toda la ciudad
all: Todos los ámbitos de actuación all: Todos los ámbitos de actuación

View File

@@ -95,6 +95,8 @@ Rails.application.routes.draw do
resources :follows, only: [:create, :destroy] resources :follows, only: [:create, :destroy]
resources :documents, only: [:new, :create, :destroy]
resources :stats, only: [:index] resources :stats, only: [:index]
resources :legacy_legislations, only: [:show], path: 'legislations' resources :legacy_legislations, only: [:show], path: 'legislations'

View File

@@ -431,6 +431,8 @@ feature 'Budget Investments' do
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"}
context "Destroy" do context "Destroy" do
scenario "Admin cannot destroy budget investments" do scenario "Admin cannot destroy budget investments" do

View File

@@ -0,0 +1,256 @@
shared_examples "documentable" do |documentable_factory_name, documentable_path, documentable_path_arguments|
include ActionView::Helpers
let!(:administrator) { create(:user) }
let!(:user) { create(:user) }
let!(:arguments) { {} }
let!(:documentable) { create(documentable_factory_name, author: user) }
let!(:documentable_dom_name) { documentable_factory_name.gsub('_', '-') }
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
context "Show" do
scenario "Should not display upload document button when there is no logged user" do
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)
visit send(documentable_path, arguments)
within "##{dom_id(documentable)}" do
expect(page).to have_link("Upload document")
end
end
scenario "Should display upload document button when admin is logged in" do
login_as(administrator)
visit send(documentable_path, arguments)
within "##{dom_id(documentable)}" do
expect(page).to have_link("Upload document")
end
end
scenario "Should navigate to new document page when click un upload button" do
login_as(user)
visit send(documentable_path, arguments)
click_link "Upload document"
expect(page).to have_selector("h1", text: "Upload document")
end
describe "Documents tab" do
let!(:document) { create(:document, documentable: documentable, user: documentable.author)}
describe "Download action" do
scenario "Should be able to anyone" do
visit send(documentable_path, arguments)
within "#tab-documents" do
expect(page).to have_link("Download")
end
end
end
describe "Destroy action" do
scenario "Should not be able when no user logged in" do
visit send(documentable_path, arguments)
within "#tab-documents" do
expect(page).not_to have_link("Destroy")
end
end
scenario "Should be able when documentable author is logged in" do
login_as documentable.author
visit send(documentable_path, arguments)
within "#tab-documents" do
expect(page).to have_link("Destroy")
end
end
scenario "Should be able when any administrator logged in" do
login_as administrator
visit send(documentable_path, arguments)
within "#tab-documents" do
expect(page).to have_link("Destroy")
end
end
end
end
end
context "New" do
scenario "Should not be able for unathenticated users" do
visit new_document_path(documentable_type: documentable.class.name, documentable_id: documentable.id)
expect(page).to have_content("You must sign in or register to continue.")
end
scenario "Should not be able for other users" do
login_as create(:user)
visit new_document_path(documentable_type: documentable.class.name, documentable_id: documentable.id)
expect(page).to have_content("You do not have permission to carry out the action 'new' on document. ")
end
scenario "Should be able to documentable author" do
login_as documentable.author
visit new_document_path(documentable_type: documentable.class.name, documentable_id: documentable.id)
expect(page).to have_selector("h1", text: "Upload document")
end
end
context "Create" do
scenario "Should show validation errors" do
login_as documentable.author
visit new_document_path(documentable_type: documentable.class.name, documentable_id: documentable.id)
click_on "Upload document"
expect(page).to have_content "2 errors prevented this Document from being saved: "
expect(page).to have_selector "small.error:not(.show-for-sr)", text: "can't be blank", count: 2
expect(page).to have_selector "small.show-for-sr", text: "can't be blank", count: 1
end
scenario "Should display file name after file selection", :js do
login_as documentable.author
visit new_document_path(documentable_type: documentable.class.name, documentable_id: documentable.id)
attach_file :document_attachment, "spec/fixtures/files/empty.pdf"
expect(page).to have_content "empty.pdf"
end
scenario "Should show error notice after unsuccessfull document upload" do
login_as documentable.author
visit new_document_path(documentable_type: documentable.class.name, documentable_id: documentable.id, from: send(documentable_path, arguments))
attach_file :document_attachment, "spec/fixtures/files/empty.pdf"
click_on "Upload document"
expect(page).to have_content "Cannot create document. Check form errors and try again."
end
scenario "Should show success notice after successfull document upload" do
login_as documentable.author
visit new_document_path(documentable_type: documentable.class.name, documentable_id: documentable.id, from: send(documentable_path, arguments))
fill_in :document_title, with: "Document title"
attach_file :document_attachment, "spec/fixtures/files/empty.pdf"
click_on "Upload document"
expect(page).to have_content "Document was created successfully."
end
scenario "Should redirect to documentable path after successfull document upload" do
login_as documentable.author
visit new_document_path(documentable_type: documentable.class.name, documentable_id: documentable.id, from: send(documentable_path, arguments))
fill_in :document_title, with: "Document title"
attach_file :document_attachment, "spec/fixtures/files/empty.pdf"
click_on "Upload document"
within "##{dom_id(documentable)}" do
expect(page).to have_selector "h1", text: documentable.title
end
end
scenario "Should show new document on documentable documents tab after successfull document upload" do
login_as documentable.author
visit new_document_path(documentable_type: documentable.class.name, documentable_id: documentable.id, from: send(documentable_path, arguments))
fill_in :document_title, with: "Document title"
attach_file :document_attachment, "spec/fixtures/files/empty.pdf"
click_on "Upload document"
expect(page).to have_link "Documents (1)"
within "#tab-documents" do
within "#document_#{Document.last.id}" do
expect(page).to have_content "Document title"
expect(page).to have_link "Download"
expect(page).to have_link "Destroy"
end
end
end
end
context "Destroy" do
let!(:document) { create(:document, documentable: documentable, user: documentable.author) }
scenario "Should show success notice after successfull document upload" do
login_as documentable.author
visit send(documentable_path, arguments)
within "#tab-documents" do
within "#document_#{document.id}" do
click_on "Destroy"
end
end
expect(page).to have_content "Document was deleted successfully."
end
scenario "Should update documents tab count after successful deletion" do
login_as documentable.author
visit send(documentable_path, arguments)
within "#tab-documents" do
within "#document_#{document.id}" do
click_on "Destroy"
end
end
expect(page).to have_link "Documents (0)"
end
scenario "Should redirect to documentable path after successful deletion" do
login_as documentable.author
visit send(documentable_path, arguments)
within "#tab-documents" do
within "#document_#{document.id}" do
click_on "Destroy"
end
end
within "##{dom_id(documentable)}" do
expect(page).to have_selector "h1", text: documentable.title
end
end
end
end