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
)
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: ->
App.Forms.disableEnter()
App.Forms.submitOnChange()
App.Forms.toggleLink()
App.Forms.uploadButton()
false

View File

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

View File

@@ -44,6 +44,7 @@ module Budgets
set_comment_flags(@comment_tree.comments)
load_investment_votes(@investment)
@investment_ids = [@investment.id]
@document = Document.new(documentable: @investment)
end
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 Reclassification
include Followable
include Documentable
acts_as_votable
acts_as_paranoid column: :hidden_at

View File

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

View File

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

View File

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