Add arguments to documentable concern to make it configurable for any recipient model.
This commit is contained in:
@@ -7,13 +7,14 @@ class Budget
|
||||
include Reclassification
|
||||
include Followable
|
||||
include Documentable
|
||||
documentable max_documents_allowed: 3,
|
||||
max_file_size: 3.megabytes,
|
||||
accepted_content_types: [ "application/pdf" ]
|
||||
|
||||
acts_as_votable
|
||||
acts_as_paranoid column: :hidden_at
|
||||
include ActsAsParanoidAliases
|
||||
|
||||
MAX_DOCUMENTS_SIZE = 3
|
||||
|
||||
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
|
||||
belongs_to :heading
|
||||
belongs_to :group
|
||||
|
||||
@@ -5,4 +5,16 @@ module Documentable
|
||||
has_many :documents, as: :documentable, dependent: :destroy
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
attr_reader :max_documents_allowed, :max_file_size, :accepted_content_types
|
||||
|
||||
private
|
||||
|
||||
def documentable(options= {})
|
||||
@max_documents_allowed = options[:max_documents_allowed]
|
||||
@max_file_size = options[:max_file_size]
|
||||
@accepted_content_types = options[:accepted_content_types]
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -4,12 +4,36 @@ class Document < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
belongs_to :documentable, polymorphic: true
|
||||
|
||||
validates_attachment :attachment, presence: true,
|
||||
content_type: { content_type: "application/pdf" },
|
||||
size: { in: 0..3.megabytes }
|
||||
validates_attachment :attachment, presence: true
|
||||
do_not_validate_attachment_file_type :attachment
|
||||
validate :validate_attachment_content_type
|
||||
|
||||
validate :validate_attachment_size
|
||||
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
|
||||
errors[:attachment] = I18n.t("documents.errors.messages.in_between",
|
||||
min: "0 Bytes",
|
||||
max: "#{bytesToMeg(documentable.class.max_file_size)} MB")
|
||||
end
|
||||
end
|
||||
|
||||
def validate_attachment_content_type
|
||||
if attachment.file? && 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
|
||||
|
||||
@@ -10,12 +10,14 @@ class Proposal < ActiveRecord::Base
|
||||
include Graphqlable
|
||||
include Followable
|
||||
include Documentable
|
||||
documentable max_documents_allowed: 3,
|
||||
max_file_size: 3.megabytes,
|
||||
accepted_content_types: [ "application/pdf" ]
|
||||
|
||||
acts_as_votable
|
||||
acts_as_paranoid column: :hidden_at
|
||||
include ActsAsParanoidAliases
|
||||
|
||||
MAX_DOCUMENTS_SIZE = 3
|
||||
RETIRE_OPTIONS = %w(duplicated started unfeasible done other)
|
||||
|
||||
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<div class="small-12 medium-9 column">
|
||||
<%= back_link_to budget_investments_path(investment.budget, heading_id: investment.heading) %>
|
||||
|
||||
<% if can?(:create, @document) && investment.documents.size < Budget::Investment::MAX_DOCUMENTS_SIZE %>
|
||||
<% if can?(:create, @document) && investment.documents.size < Budget::Investment.max_documents_allowed %>
|
||||
<%= link_to t("documents.upload_document"),
|
||||
new_document_path(documentable_id:investment, documentable_type: investment.class.name, from: request.url),
|
||||
class: 'button hollow float-right' %>
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
<div class="tabs-panel" id="tab-documents">
|
||||
<%= render 'documents/documents',
|
||||
documents: @investment.documents,
|
||||
max_documents_size: Budget::Investment::MAX_DOCUMENTS_SIZE %>
|
||||
max_documents_allowed: Budget::Investment.max_documents_allowed %>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
<% if documents.any? %>
|
||||
|
||||
<% if documents.size == max_documents_size %>
|
||||
<% if documents.size == max_documents_allowed %>
|
||||
<div class="row documents-list">
|
||||
<div class="small-12 column">
|
||||
<div class="callout primary text-center">
|
||||
<%= t "documents.max_documents_size_reached" %>
|
||||
<%= t "documents.max_documents_allowed_reached" %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
<div class="small-12 medium-9 column">
|
||||
<%= back_link_to %>
|
||||
|
||||
<% if can?(:create, @document) && @proposal.documents.size < Proposal::MAX_DOCUMENTS_SIZE %>
|
||||
<% if can?(:create, @document) && @proposal.documents.size < Proposal.max_documents_allowed %>
|
||||
<%= link_to t("documents.upload_document"),
|
||||
new_document_path(documentable_id: @proposal, documentable_type: @proposal.class.name, from: request.url),
|
||||
class: 'button hollow float-right' %>
|
||||
@@ -166,6 +166,6 @@
|
||||
<div class="tabs-panel" id="tab-documents">
|
||||
<%= render 'documents/documents',
|
||||
documents: @proposal.documents,
|
||||
max_documents_size: Proposal::MAX_DOCUMENTS_SIZE %>
|
||||
max_documents_allowed: Proposal.max_documents_allowed %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user