diff --git a/app/helpers/documentables_helper.rb b/app/helpers/documentables_helper.rb
new file mode 100644
index 000000000..fdde47f48
--- /dev/null
+++ b/app/helpers/documentables_helper.rb
@@ -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
\ No newline at end of file
diff --git a/app/helpers/documents_helper.rb b/app/helpers/documents_helper.rb
index df6666ab5..4aab7a57c 100644
--- a/app/helpers/documents_helper.rb
+++ b/app/helpers/documents_helper.rb
@@ -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
-end
\ No newline at end of file
+ def bytesToMeg(bytes)
+ bytes / Numeric::MEGABYTE
+ end
+
+end
diff --git a/app/models/document.rb b/app/models/document.rb
index 0c48edbba..ea978da21 100644
--- a/app/models/document.rb
+++ b/app/models/document.rb
@@ -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
diff --git a/app/views/documents/_document.html.erb b/app/views/documents/_document.html.erb
index 7c6017637..36fbfd378 100644
--- a/app/views/documents/_document.html.erb
+++ b/app/views/documents/_document.html.erb
@@ -1,7 +1,7 @@
| <%= document.title %> |
- <%= 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' %>
|
<% if can? :destroy, Document %>
diff --git a/app/views/documents/_form.html.erb b/app/views/documents/_form.html.erb
index 9ba07ec44..325917335 100644
--- a/app/views/documents/_form.html.erb
+++ b/app/views/documents/_form.html.erb
@@ -16,9 +16,9 @@
- <%= f.file_field :attachment, label: false, class:'show-for-sr' %>
+ <%= f.file_field :attachment, label: false, class: 'show-for-sr' %>
- <%= f.label :attachment, t("documents.form.attachment_label"), class:'button' %>
+ <%= f.label :attachment, t("documents.form.attachment_label"), class: 'button hollow' %>
<%= document_attachment_file_name(@document) %>
diff --git a/app/views/documents/new.html.erb b/app/views/documents/new.html.erb
index cf5c9fbab..1db7b6084 100644
--- a/app/views/documents/new.html.erb
+++ b/app/views/documents/new.html.erb
@@ -1,4 +1,4 @@
-
+
diff --git a/config/locales/en/documents.yml b/config/locales/en/documents.yml
index 8a81861ca..814eda091 100644
--- a/config/locales/en/documents.yml
+++ b/config/locales/en/documents.yml
@@ -10,9 +10,9 @@ en:
new:
title: Upload document
recommendations_title: File upload tips
- recommendation_one_html: You can upload up to a maximum of 3 files
- recommendation_two_html: You can upload .pdf files only
- recommendation_three_html: You can upload files up to 3 MB
+ recommendation_one_html: You can upload up to a maximum of %{max_documents_allowed} documents.
+ recommendation_two_html: You can upload %{accepted_content_types} files.
+ recommendation_three_html: You can upload files up to %{max_file_size} MB.
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}
diff --git a/config/locales/es/documents.yml b/config/locales/es/documents.yml
index 3fe544cee..0dffcfbd1 100644
--- a/config/locales/es/documents.yml
+++ b/config/locales/es/documents.yml
@@ -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 3 ficheros
- recommendation_two_html: Sólo puedes subir archivos pdf
- recommendation_three_html: Puedes subir archivos de hasta 3MB
+ recommendation_one_html: Puedes subir hasta un máximo de %{max_documents_allowed} documentos
+ recommendation_two_html: Sólo puedes subir archivos %{accepted_content_types}.
+ recommendation_three_html: Puedes subir archivos de hasta %{max_file_size} MB
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}
diff --git a/db/schema.rb b/db/schema.rb
index cccf10b9e..ed830c7d6 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -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: ""
diff --git a/spec/shared/features/documentable.rb b/spec/shared/features/documentable.rb
index 035682508..88dadcb14 100644
--- a/spec/shared/features/documentable.rb
+++ b/spec/shared/features/documentable.rb
@@ -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
diff --git a/spec/shared/models/document_validations.rb b/spec/shared/models/document_validations.rb
index 6f85705f4..e683fd5cc 100644
--- a/spec/shared/models/document_validations.rb
+++ b/spec/shared/models/document_validations.rb
@@ -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
@@ -54,8 +56,4 @@ shared_examples "document validations" do |documentable_factory|
expect(document).to_not be_valid
end
-end
-
-def bytesToMeg(bytes)
- bytes / (1024.0 * 1024.0)
end
\ No newline at end of file
|