Add missing image model spec. Add shared specs to check image validations at any imageable model

This commit is contained in:
Senén Rodero Rodríguez
2017-09-15 19:02:49 +02:00
parent bb57c1a7f5
commit c6dabedb4a
13 changed files with 208 additions and 16 deletions

View File

@@ -0,0 +1,40 @@
class DirectUploadsController < ApplicationController
def destroy_upload
@document = Document.new(cached_attachment: params[:path])
@document.set_attachment_from_cached_attachment
@document.cached_attachment = nil
@document.documentable = @documentable
if @document.attachment.destroy
flash.now[:notice] = t "documents.actions.destroy.notice"
else
flash.now[:alert] = t "documents.actions.destroy.alert"
end
render :destroy
end
def upload
@document = Document.new(document_params.merge(user: current_user))
@document.documentable = @documentable
@document.valid?
if @document.valid?
@document.attachment.save
@document.set_cached_attachment_from_attachment(URI(request.url))
else
@document.attachment.destroy
end
end
private
def set_attachment_container_resource
@container_resource = params[:resource_type]
end
def find_attachment_container_resource
@uplo = params[:documentable_type].constantize.find_or_initialize_by(id: params[:documentable_id])
end
end

View File

@@ -12,8 +12,8 @@ module DocumentablesHelper
bytes_to_mega(documentable.class.max_file_size)
end
def accepted_content_types(documentable)
documentable.class.accepted_content_types
def accepted_content_types(documentable_class)
documentable_class.accepted_content_types
end
def accepted_content_types_extensions(documentable_class)
@@ -22,15 +22,15 @@ module DocumentablesHelper
.join(",")
end
def documentable_humanized_accepted_content_types(documentable)
documentable.class.accepted_content_types
def documentable_humanized_accepted_content_types(documentable_class)
documentable_class.accepted_content_types
.collect{ |content_type| content_type.split("/").last }
.join(", ")
end
def documentables_note(documentable)
t "documents.form.note", max_documents_allowed: max_documents_allowed(documentable),
accepted_content_types: documentable_humanized_accepted_content_types(documentable),
accepted_content_types: documentable_humanized_accepted_content_types(documentable.class),
max_file_size: max_file_size(documentable)
end

View File

@@ -0,0 +1,28 @@
class DirectUpload
include ActiveModel::Validations
attr_accessor :resource, :resource_type, :resource_id, :resource_relation,
:attachment, :cached_attachment
validates_presence_of :attachment, :resource_type, :resource_relation
validate :parent_resource_attachment_validations,
if: -> { attachment.present? && resource_type.present? && resource_relation.present? }
def parent_resource_attachment_validations
# Proposal or Budget::Investment
resource = resource_type.constantize.find_or_initialize_by(id: resource_id)
# Document or Image
relation = if resource.class.reflections[resource_relation].macro == :has_one
resource.send("build_#{resource_relation}", attachment: attachment)
else
resource.send(resource_relation).build(attachment: attachment)
end
relation.valid?
if relation.errors.has_key? :attachment
errors[:attachment] = relation.errors[:attachment]
end
end
end

View File

@@ -50,9 +50,13 @@ class Document < ActiveRecord::Base
private
def documentable_class
documentable_type.constantize if documentable_type.present?
end
def validate_attachment_size
if documentable.present? &&
attachment_file_size > documentable.class.max_file_size
if documentable_class.present? &&
attachment_file_size > documentable_class.max_file_size
errors[:attachment] = I18n.t("documents.errors.messages.in_between",
min: "0 Bytes",
max: "#{max_file_size(documentable)} MB")
@@ -60,11 +64,11 @@ class Document < ActiveRecord::Base
end
def validate_attachment_content_type
if documentable.present? &&
!accepted_content_types(documentable).include?(attachment_content_type)
if documentable_class &&
!accepted_content_types(documentable_class).include?(attachment_content_type)
errors[:attachment] = I18n.t("documents.errors.messages.wrong_content_type",
content_type: attachment_content_type,
accepted_content_types: documentable_humanized_accepted_content_types(documentable))
accepted_content_types: documentable_humanized_accepted_content_types(documentable_class))
end
end

View File

@@ -66,6 +66,10 @@ class Image < ActiveRecord::Base
attachment.reprocess!
end
def imageable_class
imageable_type.constantize if imageable_type.present?
end
def validate_image_dimensions
if attachment_of_valid_content_type?
dimensions = Paperclip::Geometry.from_file(attachment.queued_for_write[:original].path)
@@ -75,7 +79,7 @@ class Image < ActiveRecord::Base
end
def validate_attachment_size
if imageable.present? &&
if imageable_class &&
attachment_file_size > 1.megabytes
errors[:attachment] = I18n.t("images.errors.messages.in_between",
min: "0 Bytes",
@@ -84,7 +88,7 @@ class Image < ActiveRecord::Base
end
def validate_attachment_content_type
if imageable.present? && !attachment_of_valid_content_type?
if imageable_class && !attachment_of_valid_content_type?
errors[:attachment] = I18n.t("images.errors.messages.wrong_content_type",
content_type: attachment_content_type,
accepted_content_types: imageable_humanized_accepted_content_types)

View File

@@ -1,4 +1,4 @@
<div class="document-form <%= documentable_class(@document.documentable) %> row">
<div class="document-form <%= documentable_class(@document.documentable.class) %> row">
<div class="small-12 medium-9 column">
<%= back_link_to params[:from] %>
@@ -16,7 +16,7 @@
</li>
<li>
<%= t "documents.recommendation_two_html",
accepted_content_types: documentable_humanized_accepted_content_types(@document.documentable) %>
accepted_content_types: documentable_humanized_accepted_content_types(@document.documentable.class) %>
</li>
<li>
<%= t "documents.recommendation_three_html",