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,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