Use documentables helper method within specs and document model.

This commit is contained in:
Senén Rodero Rodríguez
2017-07-24 14:34:09 +02:00
parent 17ad148cd7
commit 92e8468e89
5 changed files with 28 additions and 10 deletions

View File

@@ -16,4 +16,10 @@ module DocumentablesHelper
documentable.class.accepted_content_types documentable.class.accepted_content_types
end end
def humanized_accepted_content_types(documentable)
documentable.class.accepted_content_types
.collect{ |content_type| content_type.split("/").last }
.join(", ")
end
end end

View File

@@ -1,5 +1,6 @@
class Document < ActiveRecord::Base class Document < ActiveRecord::Base
include DocumentsHelper include DocumentsHelper
include DocumentablesHelper
has_attached_file :attachment has_attached_file :attachment
belongs_to :user belongs_to :user
@@ -21,16 +22,16 @@ class Document < ActiveRecord::Base
attachment_file_size > documentable.class.max_file_size 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: "#{max_file_size(documentable)} MB")
end end
end end
def validate_attachment_content_type def validate_attachment_content_type
if documentable.present? && if documentable.present? &&
!documentable.class.accepted_content_types.include?(attachment_content_type) !accepted_content_types(documentable).include?(attachment_content_type)
errors[:attachment] = I18n.t("documents.errors.messages.wrong_content_type", errors[:attachment] = I18n.t("documents.errors.messages.wrong_content_type",
content_type: attachment_content_type, content_type: attachment_content_type,
accepted_content_types: documentable.class.accepted_content_types.join(", ")) accepted_content_types: humanized_accepted_content_types(documentable))
end end
end end

View File

@@ -10,9 +10,18 @@
<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", max_documents_allowed: @document.documentable.class.max_documents_allowed) %></li> <li>
<li><%= t("documents.recommendation_two_html", accepted_content_types: @document.documentable.class.accepted_content_types.join(", ")) %></li> <%= t "documents.recommendation_one_html",
<li><%= t("documents.recommendation_three_html", max_file_size: bytesToMeg(@document.documentable.class.max_file_size)) %></li> max_documents_allowed: max_documents_allowed(@document.documentable) %>
</li>
<li>
<%= t "documents.recommendation_two_html",
accepted_content_types: humanized_accepted_content_types(@document.documentable) %>
</li>
<li>
<%= t "documents.recommendation_three_html",
max_file_size: max_file_size(@document.documentable) %>
</li>
</ul> </ul>
</div> </div>
</div> </div>

View File

@@ -1,12 +1,13 @@
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 include DocumentsHelper
include DocumentablesHelper
let!(:administrator) { create(:user) } let!(:administrator) { create(:user) }
let!(:user) { create(:user) } let!(:user) { create(:user) }
let!(:arguments) { {} } let!(:arguments) { {} }
let!(:documentable) { create(documentable_factory_name, author: user) } let!(:documentable) { create(documentable_factory_name, author: user) }
let!(:documentable_dom_name) { documentable_factory_name.gsub('_', '-') } let!(:documentable_dom_name) { documentable_factory_name.parameterize }
before do before do
create(:administrator, user: administrator) create(:administrator, user: administrator)
@@ -157,9 +158,9 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
documentable_id: documentable.id, documentable_id: documentable.id,
from: send(documentable_path, arguments)) 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 up to a maximum of #{max_file_size(documentable)} documents."
expect(page).to have_content "You can upload #{documentable.class.accepted_content_types.join(", ")} files." expect(page).to have_content "You can upload #{humanized_accepted_content_types(documentable)} files."
expect(page).to have_content "You can upload files up to #{bytesToMeg(documentable.class.max_file_size)} MB." expect(page).to have_content "You can upload files up to #{max_file_size(documentable)} MB."
end end
end end

View File

@@ -3,6 +3,7 @@ shared_examples "document validations" do |documentable_factory|
include DocumentablesHelper include DocumentablesHelper
let!(:document) { build(:document, documentable_factory.to_sym) } let!(:document) { build(:document, documentable_factory.to_sym) }
let!(:documentable) { document.documentable }
let!(:maxfilesize) { max_file_size(document.documentable) } let!(:maxfilesize) { max_file_size(document.documentable) }
let!(:acceptedcontenttypes) { accepted_content_types(document.documentable) } let!(:acceptedcontenttypes) { accepted_content_types(document.documentable) }