Replace harcoded images and documents settings

This commit is contained in:
Julian Herrero
2019-05-16 17:42:17 +02:00
parent 220bfb065a
commit 8e0bbf54f6
15 changed files with 77 additions and 60 deletions

View File

@@ -13,9 +13,6 @@ class Budget
include Imageable
include Mappable
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

View File

@@ -7,16 +7,17 @@ module Documentable
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]
def max_documents_allowed
Setting["uploads.documents.max_amount"].to_i
end
def max_file_size
Setting["uploads.documents.max_size"].to_i.megabytes
end
def accepted_content_types
Setting["uploads.documents.content_types"]&.split(" ") || [ "application/pdf" ]
end
end
end

View File

@@ -1,13 +1,5 @@
class Dashboard::Action < ApplicationRecord
include Documentable
documentable max_documents_allowed: 3,
max_file_size: 3.megabytes,
accepted_content_types: [ "application/pdf",
"image/jpeg",
"image/jpg",
"image/png",
"application/zip" ]
include Linkable
acts_as_paranoid column: :hidden_at

View File

@@ -2,12 +2,11 @@ class Image < ApplicationRecord
include ImagesHelper
include ImageablesHelper
TITLE_LENGTH_RANGE = 4..80
MIN_SIZE = 475
MAX_IMAGE_SIZE = 1.megabyte
ACCEPTED_CONTENT_TYPE = %w(image/jpeg image/jpg).freeze
has_attached_file :attachment, styles: { large: "x#{MIN_SIZE}", medium: "300x300#", thumb: "140x245#" },
has_attached_file :attachment, styles: {
large: "x#{Setting["uploads.images.min_height"]}",
medium: "300x300#",
thumb: "140x245#"
},
url: "/system/:class/:prefix/:style/:hash.:extension",
hash_data: ":class/:style",
use_timestamp: false,
@@ -23,7 +22,8 @@ class Image < ApplicationRecord
validate :attachment_presence
validate :validate_attachment_content_type, if: -> { attachment.present? }
validate :validate_attachment_size, if: -> { attachment.present? }
validates :title, presence: true, length: { in: TITLE_LENGTH_RANGE }
validates :title, presence: true
validate :validate_title_length
validates :user_id, presence: true
validates :imageable_id, presence: true, if: -> { persisted? }
validates :imageable_type, presence: true, if: -> { persisted? }
@@ -71,20 +71,38 @@ class Image < ApplicationRecord
return true if imageable_class == Widget::Card
dimensions = Paperclip::Geometry.from_file(attachment.queued_for_write[:original].path)
errors.add(:attachment, :min_image_width, required_min_width: MIN_SIZE) if dimensions.width < MIN_SIZE
errors.add(:attachment, :min_image_height, required_min_height: MIN_SIZE) if dimensions.height < MIN_SIZE
min_width = Setting["uploads.images.min_width"].to_i
min_height = Setting["uploads.images.min_height"].to_i
errors.add(:attachment, :min_image_width, required_min_width: min_width) if dimensions.width < min_width
errors.add(:attachment, :min_image_height, required_min_height: min_height) if dimensions.height < min_height
end
end
def validate_attachment_size
if imageable_class &&
attachment_file_size > 1.megabytes
attachment_file_size > Setting["uploads.images.max_size"].to_i.megabytes
errors.add(:attachment, I18n.t("images.errors.messages.in_between",
min: "0 Bytes",
max: "#{imageable_max_file_size} MB"))
end
end
def validate_title_length
if title.present?
title_min_length = Setting["uploads.images.title.min_length"].to_i
title_max_length = Setting["uploads.images.title.max_length"].to_i
if title.size < title_min_length
errors.add(:title, I18n.t("errors.messages.too_short", count: title_min_length))
end
if title.size > title_max_length
errors.add(:title, I18n.t("errors.messages.too_long", count: title_max_length))
end
end
end
def validate_attachment_content_type
if imageable_class && !attachment_of_valid_content_type?
message = I18n.t("images.errors.messages.wrong_content_type",

View File

@@ -4,9 +4,6 @@ class Legislation::Process < ApplicationRecord
include Milestoneable
include Imageable
include Documentable
documentable max_documents_allowed: 3,
max_file_size: 3.megabytes,
accepted_content_types: [ "application/pdf" ]
acts_as_paranoid column: :hidden_at
acts_as_taggable_on :customs

View File

@@ -14,9 +14,6 @@ class Legislation::Proposal < ApplicationRecord
include Imageable
include Randomizable
documentable max_documents_allowed: 3,
max_file_size: 3.megabytes,
accepted_content_types: [ "application/pdf" ]
accepts_nested_attributes_for :documents, allow_destroy: true
acts_as_votable

View File

@@ -1,9 +1,6 @@
class Milestone < ApplicationRecord
include Imageable
include Documentable
documentable max_documents_allowed: 3,
max_file_size: 3.megabytes,
accepted_content_types: [ "application/pdf" ]
translates :title, :description, touch: true
include Globalizable

View File

@@ -6,9 +6,6 @@ class Poll::Question::Answer < ApplicationRecord
translates :description, touch: true
include Globalizable
documentable max_documents_allowed: 3,
max_file_size: 3.megabytes,
accepted_content_types: [ "application/pdf" ]
accepts_nested_attributes_for :documents, allow_destroy: true
belongs_to :question, class_name: "Poll::Question", foreign_key: "question_id"

View File

@@ -15,9 +15,6 @@ class Proposal < ApplicationRecord
include Mappable
include Notifiable
include Documentable
documentable max_documents_allowed: 3,
max_file_size: 3.megabytes,
accepted_content_types: [ "application/pdf" ]
include EmbedVideosHelper
include Relationable
include Milestoneable

View File

@@ -52,6 +52,11 @@ class Setting < ApplicationRecord
setting.destroy if setting.present?
end
def accepted_content_types_for(group)
mime_content_types = Setting["uploads.#{group}.content_types"]&.split(" ") || []
Setting.mime_types[group].select { |_, content_type| mime_content_types.include?(content_type) }.keys
end
def mime_types
{
"images" => {