Add documentables helper
This commit is contained in:
19
app/helpers/documentables_helper.rb
Normal file
19
app/helpers/documentables_helper.rb
Normal 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
|
||||
@@ -8,8 +8,12 @@ module DocumentsHelper
|
||||
document.errors[:attachment].join(', ') if document.errors.key?(:attachment)
|
||||
end
|
||||
|
||||
def document_documentable_class(document)
|
||||
document.documentable.class.name.parameterize('_')
|
||||
def document_source_options
|
||||
Hash[Document.sources.map { |k,v| [k, Document.human_attribute_name("document.#{k}")] }]
|
||||
end
|
||||
|
||||
def bytesToMeg(bytes)
|
||||
bytes / Numeric::MEGABYTE
|
||||
end
|
||||
|
||||
end
|
||||
@@ -1,21 +1,24 @@
|
||||
class Document < ActiveRecord::Base
|
||||
include DocumentsHelper
|
||||
has_attached_file :attachment
|
||||
|
||||
belongs_to :user
|
||||
belongs_to :documentable, polymorphic: 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
|
||||
validate :validate_attachment_content_type
|
||||
|
||||
validate :validate_attachment_size
|
||||
validate :validate_attachment_content_type, if: -> { attachment.present? }
|
||||
validate :validate_attachment_size, if: -> { attachment.present? }
|
||||
validates :title, presence: true
|
||||
validates :user, presence: true
|
||||
validates :documentable_id, presence: true
|
||||
validates :documentable_type, presence: true
|
||||
|
||||
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",
|
||||
min: "0 Bytes",
|
||||
max: "#{bytesToMeg(documentable.class.max_file_size)} MB")
|
||||
@@ -23,17 +26,12 @@ class Document < ActiveRecord::Base
|
||||
end
|
||||
|
||||
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",
|
||||
content_type: attachment_content_type,
|
||||
accepted_content_types: documentable.class.accepted_content_types.join(", "))
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def bytesToMeg bytes
|
||||
bytes / (1024.0 * 1024.0)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<tr id="<%= dom_id(document)%>">
|
||||
<td><%= document.title %></td>
|
||||
<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 class="text-center">
|
||||
<% if can? :destroy, Document %>
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
<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' %>
|
||||
<%= f.label :attachment, t("documents.form.attachment_label"), class: 'button hollow' %>
|
||||
<p><%= document_attachment_file_name(@document) %></p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -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">
|
||||
<%= render "shared/back_link" %>
|
||||
@@ -10,9 +10,9 @@
|
||||
<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>
|
||||
<li><%= t("documents.recommendation_one_html", max_documents_allowed: @document.documentable.class.max_documents_allowed) %></li>
|
||||
<li><%= t("documents.recommendation_two_html", accepted_content_types: @document.documentable.class.accepted_content_types.join(", ")) %></li>
|
||||
<li><%= t("documents.recommendation_three_html", max_file_size: bytesToMeg(@document.documentable.class.max_file_size)) %></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -10,9 +10,9 @@ en:
|
||||
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>
|
||||
recommendation_one_html: You can upload up to a <strong>maximum of %{max_documents_allowed} documents</strong>.
|
||||
recommendation_two_html: You can upload <strong>%{accepted_content_types}</strong> files.
|
||||
recommendation_three_html: You can upload files up to <strong>%{max_file_size} MB</strong>.
|
||||
actions:
|
||||
create:
|
||||
notice: Document was created successfully.
|
||||
@@ -23,7 +23,6 @@ en:
|
||||
buttons:
|
||||
download_document: Download PDF
|
||||
destroy_document: Destroy
|
||||
link_document: Link
|
||||
errors:
|
||||
messages:
|
||||
in_between: must be in between %{min} and %{max}
|
||||
|
||||
@@ -10,9 +10,9 @@ es:
|
||||
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>
|
||||
recommendation_one_html: Puedes subir hasta un máximo de <strong>%{max_documents_allowed} documentos</strong>
|
||||
recommendation_two_html: Sólo puedes subir <strong>archivos %{accepted_content_types}</strong>.
|
||||
recommendation_three_html: Puedes subir archivos de hasta <strong>%{max_file_size} MB</strong>
|
||||
actions:
|
||||
create:
|
||||
notice: "El documento se ha creado correctamente."
|
||||
@@ -23,7 +23,6 @@ es:
|
||||
buttons:
|
||||
download_document: Descargar PDF
|
||||
destroy_document: Eliminar
|
||||
link_document: Enlace
|
||||
errors:
|
||||
messages:
|
||||
in_between: debe estar entre %{min} y %{max}
|
||||
|
||||
@@ -936,7 +936,7 @@ ActiveRecord::Schema.define(version: 20170720092638) do
|
||||
t.boolean "email_digest", default: true
|
||||
t.boolean "email_on_direct_message", default: true
|
||||
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.integer "failed_email_digests_count", default: 0
|
||||
t.text "former_users_data_log", default: ""
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
shared_examples "documentable" do |documentable_factory_name, documentable_path, documentable_path_arguments|
|
||||
include ActionView::Helpers
|
||||
include DocumentsHelper
|
||||
|
||||
let!(:administrator) { create(:user) }
|
||||
let!(:user) { create(:user) }
|
||||
@@ -82,7 +83,7 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
|
||||
visit send(documentable_path, arguments)
|
||||
|
||||
within "#tab-documents" do
|
||||
expect(page).to have_link("Download")
|
||||
expect(page).to have_link("Download PDF")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -125,7 +126,8 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
|
||||
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)
|
||||
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
|
||||
@@ -133,7 +135,8 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
|
||||
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)
|
||||
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
|
||||
@@ -141,11 +144,24 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
|
||||
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)
|
||||
visit new_document_path(documentable_type: documentable.class.name,
|
||||
documentable_id: documentable.id)
|
||||
|
||||
expect(page).to have_selector("h1", text: "Upload document")
|
||||
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
|
||||
|
||||
context "Create" do
|
||||
@@ -153,7 +169,8 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
|
||||
scenario "Should show validation errors" do
|
||||
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"
|
||||
|
||||
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
|
||||
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"
|
||||
|
||||
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
|
||||
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"
|
||||
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
|
||||
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"
|
||||
attach_file :document_attachment, "spec/fixtures/files/empty.pdf"
|
||||
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
|
||||
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"
|
||||
attach_file :document_attachment, "spec/fixtures/files/empty.pdf"
|
||||
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
|
||||
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"
|
||||
attach_file :document_attachment, "spec/fixtures/files/empty.pdf"
|
||||
click_on "Upload document"
|
||||
@@ -216,7 +242,7 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
|
||||
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 "Download PDF"
|
||||
expect(page).to have_link "Destroy"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
shared_examples "document validations" do |documentable_factory|
|
||||
include DocumentsHelper
|
||||
include DocumentablesHelper
|
||||
|
||||
let(:document) { build(:document, documentable_factory.to_sym) }
|
||||
let(:max_file_size) { document.documentable.class.max_file_size }
|
||||
let(:accepted_content_types) { document.documentable.class.accepted_content_types }
|
||||
let!(:document) { build(:document, documentable_factory.to_sym) }
|
||||
let!(:maxfilesize) { max_file_size(document.documentable) }
|
||||
let!(:acceptedcontenttypes) { accepted_content_types(document.documentable) }
|
||||
|
||||
it "should be valid" do
|
||||
expect(document).to be_valid
|
||||
@@ -21,7 +23,7 @@ shared_examples "document validations" do |documentable_factory|
|
||||
end
|
||||
|
||||
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
|
||||
document.attachment = File.new("spec/fixtures/files/empty.#{extension}")
|
||||
|
||||
@@ -30,10 +32,10 @@ shared_examples "document validations" do |documentable_factory|
|
||||
end
|
||||
|
||||
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.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
|
||||
|
||||
it "should not be valid without a user_id" do
|
||||
@@ -55,7 +57,3 @@ shared_examples "document validations" do |documentable_factory|
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def bytesToMeg(bytes)
|
||||
bytes / (1024.0 * 1024.0)
|
||||
end
|
||||
Reference in New Issue
Block a user