Replace harcoded images and documents settings
This commit is contained in:
@@ -17,15 +17,11 @@ module DocumentablesHelper
|
|||||||
end
|
end
|
||||||
|
|
||||||
def accepted_content_types_extensions(documentable_class)
|
def accepted_content_types_extensions(documentable_class)
|
||||||
documentable_class.accepted_content_types
|
Setting.accepted_content_types_for("documents").map { |content_type| ".#{content_type}" }.join(",")
|
||||||
.collect{ |content_type| ".#{content_type.split("/").last}" }
|
|
||||||
.join(",")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def documentable_humanized_accepted_content_types(documentable_class)
|
def documentable_humanized_accepted_content_types(documentable_class)
|
||||||
documentable_class.accepted_content_types
|
Setting.accepted_content_types_for("documents").join(", ")
|
||||||
.collect{ |content_type| content_type.split("/").last }
|
|
||||||
.join(", ")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def documentables_note(documentable)
|
def documentables_note(documentable)
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ module ImageablesHelper
|
|||||||
end
|
end
|
||||||
|
|
||||||
def imageable_max_file_size
|
def imageable_max_file_size
|
||||||
bytes_to_megabytes(Image::MAX_IMAGE_SIZE)
|
bytes_to_megabytes(Setting["uploads.images.max_size"].to_i.megabytes)
|
||||||
end
|
end
|
||||||
|
|
||||||
def bytes_to_megabytes(bytes)
|
def bytes_to_megabytes(bytes)
|
||||||
@@ -17,19 +17,21 @@ module ImageablesHelper
|
|||||||
end
|
end
|
||||||
|
|
||||||
def imageable_accepted_content_types
|
def imageable_accepted_content_types
|
||||||
Image::ACCEPTED_CONTENT_TYPE
|
Setting["uploads.images.content_types"]&.split(" ") || [ "image/jpeg" ]
|
||||||
end
|
end
|
||||||
|
|
||||||
def imageable_accepted_content_types_extensions
|
def imageable_accepted_content_types_extensions
|
||||||
Image::ACCEPTED_CONTENT_TYPE
|
Setting.accepted_content_types_for("images").map do |content_type|
|
||||||
.collect{ |content_type| ".#{content_type.split("/").last}" }
|
if content_type == "jpg"
|
||||||
.join(",")
|
".jpg,.jpeg"
|
||||||
|
else
|
||||||
|
".#{content_type}"
|
||||||
|
end
|
||||||
|
end.join(",")
|
||||||
end
|
end
|
||||||
|
|
||||||
def imageable_humanized_accepted_content_types
|
def imageable_humanized_accepted_content_types
|
||||||
Image::ACCEPTED_CONTENT_TYPE
|
Setting.accepted_content_types_for("images").join(", ")
|
||||||
.collect{ |content_type| content_type.split("/").last }
|
|
||||||
.join(", ")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def imageables_note(_imageable)
|
def imageables_note(_imageable)
|
||||||
|
|||||||
@@ -13,9 +13,6 @@ class Budget
|
|||||||
include Imageable
|
include Imageable
|
||||||
include Mappable
|
include Mappable
|
||||||
include Documentable
|
include Documentable
|
||||||
documentable max_documents_allowed: 3,
|
|
||||||
max_file_size: 3.megabytes,
|
|
||||||
accepted_content_types: [ "application/pdf" ]
|
|
||||||
|
|
||||||
acts_as_votable
|
acts_as_votable
|
||||||
acts_as_paranoid column: :hidden_at
|
acts_as_paranoid column: :hidden_at
|
||||||
|
|||||||
@@ -7,16 +7,17 @@ module Documentable
|
|||||||
end
|
end
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
attr_reader :max_documents_allowed, :max_file_size, :accepted_content_types
|
def max_documents_allowed
|
||||||
|
Setting["uploads.documents.max_amount"].to_i
|
||||||
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
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,13 +1,5 @@
|
|||||||
class Dashboard::Action < ApplicationRecord
|
class Dashboard::Action < ApplicationRecord
|
||||||
include Documentable
|
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
|
include Linkable
|
||||||
|
|
||||||
acts_as_paranoid column: :hidden_at
|
acts_as_paranoid column: :hidden_at
|
||||||
|
|||||||
@@ -2,12 +2,11 @@ class Image < ApplicationRecord
|
|||||||
include ImagesHelper
|
include ImagesHelper
|
||||||
include ImageablesHelper
|
include ImageablesHelper
|
||||||
|
|
||||||
TITLE_LENGTH_RANGE = 4..80
|
has_attached_file :attachment, styles: {
|
||||||
MIN_SIZE = 475
|
large: "x#{Setting["uploads.images.min_height"]}",
|
||||||
MAX_IMAGE_SIZE = 1.megabyte
|
medium: "300x300#",
|
||||||
ACCEPTED_CONTENT_TYPE = %w(image/jpeg image/jpg).freeze
|
thumb: "140x245#"
|
||||||
|
},
|
||||||
has_attached_file :attachment, styles: { large: "x#{MIN_SIZE}", medium: "300x300#", thumb: "140x245#" },
|
|
||||||
url: "/system/:class/:prefix/:style/:hash.:extension",
|
url: "/system/:class/:prefix/:style/:hash.:extension",
|
||||||
hash_data: ":class/:style",
|
hash_data: ":class/:style",
|
||||||
use_timestamp: false,
|
use_timestamp: false,
|
||||||
@@ -23,7 +22,8 @@ class Image < ApplicationRecord
|
|||||||
validate :attachment_presence
|
validate :attachment_presence
|
||||||
validate :validate_attachment_content_type, if: -> { attachment.present? }
|
validate :validate_attachment_content_type, if: -> { attachment.present? }
|
||||||
validate :validate_attachment_size, 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 :user_id, presence: true
|
||||||
validates :imageable_id, presence: true, if: -> { persisted? }
|
validates :imageable_id, presence: true, if: -> { persisted? }
|
||||||
validates :imageable_type, 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
|
return true if imageable_class == Widget::Card
|
||||||
|
|
||||||
dimensions = Paperclip::Geometry.from_file(attachment.queued_for_write[:original].path)
|
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
|
min_width = Setting["uploads.images.min_width"].to_i
|
||||||
errors.add(:attachment, :min_image_height, required_min_height: MIN_SIZE) if dimensions.height < MIN_SIZE
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_attachment_size
|
def validate_attachment_size
|
||||||
if imageable_class &&
|
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",
|
errors.add(:attachment, I18n.t("images.errors.messages.in_between",
|
||||||
min: "0 Bytes",
|
min: "0 Bytes",
|
||||||
max: "#{imageable_max_file_size} MB"))
|
max: "#{imageable_max_file_size} MB"))
|
||||||
end
|
end
|
||||||
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
|
def validate_attachment_content_type
|
||||||
if imageable_class && !attachment_of_valid_content_type?
|
if imageable_class && !attachment_of_valid_content_type?
|
||||||
message = I18n.t("images.errors.messages.wrong_content_type",
|
message = I18n.t("images.errors.messages.wrong_content_type",
|
||||||
|
|||||||
@@ -4,9 +4,6 @@ class Legislation::Process < ApplicationRecord
|
|||||||
include Milestoneable
|
include Milestoneable
|
||||||
include Imageable
|
include Imageable
|
||||||
include Documentable
|
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_paranoid column: :hidden_at
|
||||||
acts_as_taggable_on :customs
|
acts_as_taggable_on :customs
|
||||||
|
|||||||
@@ -14,9 +14,6 @@ class Legislation::Proposal < ApplicationRecord
|
|||||||
include Imageable
|
include Imageable
|
||||||
include Randomizable
|
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
|
accepts_nested_attributes_for :documents, allow_destroy: true
|
||||||
|
|
||||||
acts_as_votable
|
acts_as_votable
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
class Milestone < ApplicationRecord
|
class Milestone < ApplicationRecord
|
||||||
include Imageable
|
include Imageable
|
||||||
include Documentable
|
include Documentable
|
||||||
documentable max_documents_allowed: 3,
|
|
||||||
max_file_size: 3.megabytes,
|
|
||||||
accepted_content_types: [ "application/pdf" ]
|
|
||||||
|
|
||||||
translates :title, :description, touch: true
|
translates :title, :description, touch: true
|
||||||
include Globalizable
|
include Globalizable
|
||||||
|
|||||||
@@ -6,9 +6,6 @@ class Poll::Question::Answer < ApplicationRecord
|
|||||||
translates :description, touch: true
|
translates :description, touch: true
|
||||||
include Globalizable
|
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
|
accepts_nested_attributes_for :documents, allow_destroy: true
|
||||||
|
|
||||||
belongs_to :question, class_name: "Poll::Question", foreign_key: "question_id"
|
belongs_to :question, class_name: "Poll::Question", foreign_key: "question_id"
|
||||||
|
|||||||
@@ -15,9 +15,6 @@ class Proposal < ApplicationRecord
|
|||||||
include Mappable
|
include Mappable
|
||||||
include Notifiable
|
include Notifiable
|
||||||
include Documentable
|
include Documentable
|
||||||
documentable max_documents_allowed: 3,
|
|
||||||
max_file_size: 3.megabytes,
|
|
||||||
accepted_content_types: [ "application/pdf" ]
|
|
||||||
include EmbedVideosHelper
|
include EmbedVideosHelper
|
||||||
include Relationable
|
include Relationable
|
||||||
include Milestoneable
|
include Milestoneable
|
||||||
|
|||||||
@@ -52,6 +52,11 @@ class Setting < ApplicationRecord
|
|||||||
setting.destroy if setting.present?
|
setting.destroy if setting.present?
|
||||||
end
|
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
|
def mime_types
|
||||||
{
|
{
|
||||||
"images" => {
|
"images" => {
|
||||||
|
|||||||
@@ -115,6 +115,8 @@ ignore_missing:
|
|||||||
- "activerecord.errors.models.direct_message.*"
|
- "activerecord.errors.models.direct_message.*"
|
||||||
- "errors.messages.blank"
|
- "errors.messages.blank"
|
||||||
- "errors.messages.taken"
|
- "errors.messages.taken"
|
||||||
|
- "errors.messages.too_short"
|
||||||
|
- "errors.messages.too_long"
|
||||||
- "devise.failure.invalid"
|
- "devise.failure.invalid"
|
||||||
- "devise.registrations.destroyed"
|
- "devise.registrations.destroyed"
|
||||||
- "devise.password_expired.*"
|
- "devise.password_expired.*"
|
||||||
|
|||||||
@@ -148,6 +148,24 @@ describe Setting do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe ".accepted_content_types_for" do
|
||||||
|
it "returns the formats accepted according to the setting value" do
|
||||||
|
Setting["uploads.images.content_types"] = "image/jpeg image/gif"
|
||||||
|
Setting["uploads.documents.content_types"] = "application/pdf application/msword"
|
||||||
|
|
||||||
|
expect(Setting.accepted_content_types_for("images")).to eq ["jpg", "gif"]
|
||||||
|
expect(Setting.accepted_content_types_for("documents")).to eq ["pdf", "doc"]
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns empty array if setting does't exist" do
|
||||||
|
Setting.remove("uploads.images.content_types")
|
||||||
|
Setting.remove("uploads.documents.content_types")
|
||||||
|
|
||||||
|
expect(Setting.accepted_content_types_for("images")).to be_empty
|
||||||
|
expect(Setting.accepted_content_types_for("documents")).to be_empty
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe ".add_new_settings" do
|
describe ".add_new_settings" do
|
||||||
context "default settings with strings" do
|
context "default settings with strings" do
|
||||||
before do
|
before do
|
||||||
|
|||||||
@@ -41,7 +41,8 @@ shared_examples "image validations" do |imageable_factory|
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "is not valid for attachments larger than imageable max_file_size definition" do
|
it "is not valid for attachments larger than imageable max_file_size definition" do
|
||||||
allow(image).to receive(:attachment_file_size).and_return(Image::MAX_IMAGE_SIZE + 1.byte)
|
larger_size = Setting["uploads.images.max_size"].to_i.megabytes + 1.byte
|
||||||
|
allow(image).to receive(:attachment_file_size).and_return(larger_size)
|
||||||
|
|
||||||
expect(image).not_to be_valid
|
expect(image).not_to be_valid
|
||||||
expect(image.errors[:attachment]).to include "must be in between 0 Bytes and 1 MB"
|
expect(image.errors[:attachment]).to include "must be in between 0 Bytes and 1 MB"
|
||||||
@@ -67,4 +68,4 @@ shared_examples "image validations" do |imageable_factory|
|
|||||||
expect(image).not_to be_valid
|
expect(image).not_to be_valid
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user