Add documentables helper

This commit is contained in:
Senén Rodero Rodríguez
2017-07-22 01:31:36 +02:00
parent 38d4d59241
commit 3a7c9d9f83
11 changed files with 94 additions and 51 deletions

View File

@@ -0,0 +1,19 @@
module DocumentablesHelper
def documentable_class(documentable)
documentable.class.name.parameterize('_')
end
def max_documents_allowed(documentable)
documentable.class.max_documents_allowed
end
def max_file_size(documentable)
bytesToMeg(documentable.class.max_file_size)
end
def accepted_content_types(documentable)
documentable.class.accepted_content_types
end
end

View File

@@ -8,8 +8,12 @@ module DocumentsHelper
document.errors[:attachment].join(', ') if document.errors.key?(:attachment) document.errors[:attachment].join(', ') if document.errors.key?(:attachment)
end end
def document_documentable_class(document) def document_source_options
document.documentable.class.name.parameterize('_') Hash[Document.sources.map { |k,v| [k, Document.human_attribute_name("document.#{k}")] }]
end
def bytesToMeg(bytes)
bytes / Numeric::MEGABYTE
end end
end end

View File

@@ -1,21 +1,24 @@
class Document < ActiveRecord::Base class Document < ActiveRecord::Base
include DocumentsHelper
has_attached_file :attachment has_attached_file :attachment
belongs_to :user belongs_to :user
belongs_to :documentable, polymorphic: true belongs_to :documentable, polymorphic: true
validates_attachment :attachment, presence: true validates_attachment :attachment, presence: true
# Disable paperclip security validation due to polymorphic configuration
# Paperclip do not allow to user Procs on valiations definition
do_not_validate_attachment_file_type :attachment do_not_validate_attachment_file_type :attachment
validate :validate_attachment_content_type validate :validate_attachment_content_type, if: -> { attachment.present? }
validate :validate_attachment_size, if: -> { attachment.present? }
validate :validate_attachment_size
validates :title, presence: true validates :title, presence: true
validates :user, presence: true validates :user, presence: true
validates :documentable_id, presence: true validates :documentable_id, presence: true
validates :documentable_type, presence: true validates :documentable_type, presence: true
def validate_attachment_size def validate_attachment_size
if attachment.file? && documentable.present? && attachment_file_size > documentable.class.max_file_size if documentable.present? &&
attachment_file_size > documentable.class.max_file_size
errors[:attachment] = I18n.t("documents.errors.messages.in_between", errors[:attachment] = I18n.t("documents.errors.messages.in_between",
min: "0 Bytes", min: "0 Bytes",
max: "#{bytesToMeg(documentable.class.max_file_size)} MB") max: "#{bytesToMeg(documentable.class.max_file_size)} MB")
@@ -23,17 +26,12 @@ class Document < ActiveRecord::Base
end end
def validate_attachment_content_type def validate_attachment_content_type
if attachment.file? && documentable.present? && !documentable.class.accepted_content_types.include?(attachment_content_type) if documentable.present? &&
!documentable.class.accepted_content_types.include?(attachment_content_type)
errors[:attachment] = I18n.t("documents.errors.messages.content_type", errors[:attachment] = I18n.t("documents.errors.messages.content_type",
content_type: attachment_content_type, content_type: attachment_content_type,
accepted_content_types: documentable.class.accepted_content_types.join(", ")) accepted_content_types: documentable.class.accepted_content_types.join(", "))
end end
end end
private
def bytesToMeg bytes
bytes / (1024.0 * 1024.0)
end
end end

View File

@@ -1,7 +1,7 @@
<tr id="<%= dom_id(document)%>"> <tr id="<%= dom_id(document)%>">
<td><%= document.title %></td> <td><%= document.title %></td>
<td class="text-center"> <td class="text-center">
<%= link_to t('documents.buttons.download_document'), document.attachment.url, class: 'button hollow' %> <%= link_to t('documents.buttons.download_document'), document.attachment.url, target: :blank, class: 'button hollow' %>
</td> </td>
<td class="text-center"> <td class="text-center">
<% if can? :destroy, Document %> <% if can? :destroy, Document %>

View File

@@ -16,9 +16,9 @@
<div class="small-12 column"> <div class="small-12 column">
<div class="file-name"> <div class="file-name">
<%= f.file_field :attachment, label: false, class:'show-for-sr' %> <%= f.file_field :attachment, label: false, class: 'show-for-sr' %>
<br> <br>
<%= f.label :attachment, t("documents.form.attachment_label"), class:'button' %> <%= f.label :attachment, t("documents.form.attachment_label"), class: 'button hollow' %>
<p><%= document_attachment_file_name(@document) %></p> <p><%= document_attachment_file_name(@document) %></p>
</div> </div>
</div> </div>

View File

@@ -1,4 +1,4 @@
<div class="document-form <%= @document.documentable.class.name.parameterize('_') %> row"> <div class="document-form <%= documentable_class(@document.documentable) %> row">
<div class="small-12 medium-9 column"> <div class="small-12 medium-9 column">
<%= render "shared/back_link" %> <%= render "shared/back_link" %>
@@ -10,9 +10,9 @@
<span class="icon-documents float-right"></span> <span class="icon-documents float-right"></span>
<h2><%= t("documents.recommendations_title") %></h2> <h2><%= t("documents.recommendations_title") %></h2>
<ul class="recommendations"> <ul class="recommendations">
<li><%= t("documents.recommendation_one_html") %></li> <li><%= t("documents.recommendation_one_html", max_documents_allowed: @document.documentable.class.max_documents_allowed) %></li>
<li><%= t("documents.recommendation_two_html") %></li> <li><%= t("documents.recommendation_two_html", accepted_content_types: @document.documentable.class.accepted_content_types.join(", ")) %></li>
<li><%= t("documents.recommendation_three_html") %></li> <li><%= t("documents.recommendation_three_html", max_file_size: bytesToMeg(@document.documentable.class.max_file_size)) %></li>
</ul> </ul>
</div> </div>
</div> </div>

View File

@@ -10,9 +10,9 @@ en:
new: new:
title: Upload document title: Upload document
recommendations_title: File upload tips recommendations_title: File upload tips
recommendation_one_html: You can upload up to a <strong>maximum of 3 files</strong> recommendation_one_html: You can upload up to a <strong>maximum of %{max_documents_allowed} documents</strong>.
recommendation_two_html: You can upload <strong>.pdf</strong> files only recommendation_two_html: You can upload <strong>%{accepted_content_types}</strong> files.
recommendation_three_html: You can upload files up to <strong>3 MB</strong> recommendation_three_html: You can upload files up to <strong>%{max_file_size} MB</strong>.
actions: actions:
create: create:
notice: Document was created successfully. notice: Document was created successfully.
@@ -23,7 +23,6 @@ en:
buttons: buttons:
download_document: Download PDF download_document: Download PDF
destroy_document: Destroy destroy_document: Destroy
link_document: Link
errors: errors:
messages: messages:
in_between: must be in between %{min} and %{max} in_between: must be in between %{min} and %{max}

View File

@@ -10,9 +10,9 @@ es:
new: new:
title: Subir un documento title: Subir un documento
recommendations_title: Consejos para subir archivos recommendations_title: Consejos para subir archivos
recommendation_one_html: Puedes subir hasta un máximo de <strong>3 ficheros</strong> recommendation_one_html: Puedes subir hasta un máximo de <strong>%{max_documents_allowed} documentos</strong>
recommendation_two_html: Sólo puedes subir archivos <strong>pdf</strong> recommendation_two_html: Sólo puedes subir <strong>archivos %{accepted_content_types}</strong>.
recommendation_three_html: Puedes subir archivos de hasta <strong>3MB</strong> recommendation_three_html: Puedes subir archivos de hasta <strong>%{max_file_size} MB</strong>
actions: actions:
create: create:
notice: "El documento se ha creado correctamente." notice: "El documento se ha creado correctamente."
@@ -23,7 +23,6 @@ es:
buttons: buttons:
download_document: Descargar PDF download_document: Descargar PDF
destroy_document: Eliminar destroy_document: Eliminar
link_document: Enlace
errors: errors:
messages: messages:
in_between: debe estar entre %{min} y %{max} in_between: debe estar entre %{min} y %{max}

View File

@@ -936,7 +936,7 @@ ActiveRecord::Schema.define(version: 20170720092638) do
t.boolean "email_digest", default: true t.boolean "email_digest", default: true
t.boolean "email_on_direct_message", default: true t.boolean "email_on_direct_message", default: true
t.boolean "official_position_badge", default: false t.boolean "official_position_badge", default: false
t.datetime "password_changed_at", default: '2017-07-20 09:31:51', null: false t.datetime "password_changed_at", default: '2017-07-27 22:09:12', null: false
t.boolean "created_from_signature", default: false t.boolean "created_from_signature", default: false
t.integer "failed_email_digests_count", default: 0 t.integer "failed_email_digests_count", default: 0
t.text "former_users_data_log", default: "" t.text "former_users_data_log", default: ""

View File

@@ -1,5 +1,6 @@
shared_examples "documentable" do |documentable_factory_name, documentable_path, documentable_path_arguments| shared_examples "documentable" do |documentable_factory_name, documentable_path, documentable_path_arguments|
include ActionView::Helpers include ActionView::Helpers
include DocumentsHelper
let!(:administrator) { create(:user) } let!(:administrator) { create(:user) }
let!(:user) { create(:user) } let!(:user) { create(:user) }
@@ -82,7 +83,7 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
visit send(documentable_path, arguments) visit send(documentable_path, arguments)
within "#tab-documents" do within "#tab-documents" do
expect(page).to have_link("Download") expect(page).to have_link("Download PDF")
end end
end end
@@ -125,7 +126,8 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
context "New" do context "New" do
scenario "Should not be able for unathenticated users" do scenario "Should not be able for unathenticated users" do
visit new_document_path(documentable_type: documentable.class.name, documentable_id: documentable.id) 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.") expect(page).to have_content("You must sign in or register to continue.")
end end
@@ -133,7 +135,8 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
scenario "Should not be able for other users" do scenario "Should not be able for other users" do
login_as create(:user) login_as create(:user)
visit new_document_path(documentable_type: documentable.class.name, documentable_id: documentable.id) 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. ") expect(page).to have_content("You do not have permission to carry out the action 'new' on document. ")
end end
@@ -141,11 +144,24 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
scenario "Should be able to documentable author" do scenario "Should be able to documentable author" do
login_as documentable.author login_as documentable.author
visit new_document_path(documentable_type: documentable.class.name, documentable_id: documentable.id) visit new_document_path(documentable_type: documentable.class.name,
documentable_id: documentable.id)
expect(page).to have_selector("h1", text: "Upload document") expect(page).to have_selector("h1", text: "Upload document")
end end
scenario "Should show documentable custom recomentations" do
login_as documentable.author
visit new_document_path(documentable_type: documentable.class.name,
documentable_id: documentable.id,
from: send(documentable_path, arguments))
expect(page).to have_content "You can upload up to a maximum of #{documentable.class.max_documents_allowed} documents."
expect(page).to have_content "You can upload #{documentable.class.accepted_content_types.join(", ")} files."
expect(page).to have_content "You can upload files up to #{bytesToMeg(documentable.class.max_file_size)} MB."
end
end end
context "Create" do context "Create" do
@@ -153,7 +169,8 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
scenario "Should show validation errors" do scenario "Should show validation errors" do
login_as documentable.author login_as documentable.author
visit new_document_path(documentable_type: documentable.class.name, documentable_id: documentable.id) visit new_document_path(documentable_type: documentable.class.name,
documentable_id: documentable.id)
click_on "Upload document" click_on "Upload document"
expect(page).to have_content "2 errors prevented this Document from being saved: " expect(page).to have_content "2 errors prevented this Document from being saved: "
@@ -164,7 +181,8 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
scenario "Should display file name after file selection", :js do scenario "Should display file name after file selection", :js do
login_as documentable.author login_as documentable.author
visit new_document_path(documentable_type: documentable.class.name, documentable_id: documentable.id) visit new_document_path(documentable_type: documentable.class.name,
documentable_id: documentable.id)
attach_file :document_attachment, "spec/fixtures/files/empty.pdf" attach_file :document_attachment, "spec/fixtures/files/empty.pdf"
expect(page).to have_content "empty.pdf" expect(page).to have_content "empty.pdf"
@@ -173,7 +191,9 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
scenario "Should show error notice after unsuccessfull document upload" do scenario "Should show error notice after unsuccessfull document upload" do
login_as documentable.author login_as documentable.author
visit new_document_path(documentable_type: documentable.class.name, documentable_id: documentable.id, from: send(documentable_path, arguments)) 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" attach_file :document_attachment, "spec/fixtures/files/empty.pdf"
click_on "Upload document" click_on "Upload document"
@@ -183,7 +203,9 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
scenario "Should show success notice after successfull document upload" do scenario "Should show success notice after successfull document upload" do
login_as documentable.author login_as documentable.author
visit new_document_path(documentable_type: documentable.class.name, documentable_id: documentable.id, from: send(documentable_path, arguments)) 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" fill_in :document_title, with: "Document title"
attach_file :document_attachment, "spec/fixtures/files/empty.pdf" attach_file :document_attachment, "spec/fixtures/files/empty.pdf"
click_on "Upload document" click_on "Upload document"
@@ -194,7 +216,9 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
scenario "Should redirect to documentable path after successfull document upload" do scenario "Should redirect to documentable path after successfull document upload" do
login_as documentable.author login_as documentable.author
visit new_document_path(documentable_type: documentable.class.name, documentable_id: documentable.id, from: send(documentable_path, arguments)) 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" fill_in :document_title, with: "Document title"
attach_file :document_attachment, "spec/fixtures/files/empty.pdf" attach_file :document_attachment, "spec/fixtures/files/empty.pdf"
click_on "Upload document" click_on "Upload document"
@@ -207,7 +231,9 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
scenario "Should show new document on documentable documents tab after successfull document upload" do scenario "Should show new document on documentable documents tab after successfull document upload" do
login_as documentable.author login_as documentable.author
visit new_document_path(documentable_type: documentable.class.name, documentable_id: documentable.id, from: send(documentable_path, arguments)) 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" fill_in :document_title, with: "Document title"
attach_file :document_attachment, "spec/fixtures/files/empty.pdf" attach_file :document_attachment, "spec/fixtures/files/empty.pdf"
click_on "Upload document" click_on "Upload document"
@@ -216,7 +242,7 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
within "#tab-documents" do within "#tab-documents" do
within "#document_#{Document.last.id}" do within "#document_#{Document.last.id}" do
expect(page).to have_content "Document title" expect(page).to have_content "Document title"
expect(page).to have_link "Download" expect(page).to have_link "Download PDF"
expect(page).to have_link "Destroy" expect(page).to have_link "Destroy"
end end
end end

View File

@@ -1,8 +1,10 @@
shared_examples "document validations" do |documentable_factory| shared_examples "document validations" do |documentable_factory|
include DocumentsHelper
include DocumentablesHelper
let(:document) { build(:document, documentable_factory.to_sym) } let!(:document) { build(:document, documentable_factory.to_sym) }
let(:max_file_size) { document.documentable.class.max_file_size } let!(:maxfilesize) { max_file_size(document.documentable) }
let(:accepted_content_types) { document.documentable.class.accepted_content_types } let!(:acceptedcontenttypes) { accepted_content_types(document.documentable) }
it "should be valid" do it "should be valid" do
expect(document).to be_valid expect(document).to be_valid
@@ -21,7 +23,7 @@ shared_examples "document validations" do |documentable_factory|
end end
it "should be valid for all accepted content types" do it "should be valid for all accepted content types" do
accepted_content_types.each do |content_type| acceptedcontenttypes.each do |content_type|
extension = content_type.split("/").last extension = content_type.split("/").last
document.attachment = File.new("spec/fixtures/files/empty.#{extension}") document.attachment = File.new("spec/fixtures/files/empty.#{extension}")
@@ -30,10 +32,10 @@ shared_examples "document validations" do |documentable_factory|
end end
it "should not be valid for attachments larger than documentable max_file_size definition" do it "should not be valid for attachments larger than documentable max_file_size definition" do
document.stub(:attachment_file_size).and_return(max_file_size.bytes + 1.byte) document.stub(:attachment_file_size).and_return(maxfilesize.megabytes + 1.byte)
expect(document).to_not be_valid expect(document).to_not be_valid
expect(document.errors[:attachment]).to include "must be in between 0 Bytes and #{bytesToMeg(max_file_size)} MB" expect(document.errors[:attachment]).to include "must be in between 0 Bytes and #{maxfilesize} MB"
end end
it "should not be valid without a user_id" do it "should not be valid without a user_id" do
@@ -55,7 +57,3 @@ shared_examples "document validations" do |documentable_factory|
end end
end end
def bytesToMeg(bytes)
bytes / (1024.0 * 1024.0)
end