class Refile::AttachmentDefinition
@api private
Attributes
cache[R]
name[R]
options[R]
record[R]
remove[RW]
store[R]
type[R]
valid_content_types[R]
Public Class Methods
new(name, cache:, store:, raise_errors: true, type: nil, extension: nil, content_type: nil)
click to toggle source
# File lib/refile/attachment_definition.rb, line 7 def initialize(name, cache:, store:, raise_errors: true, type: nil, extension: nil, content_type: nil) @name = name @raise_errors = raise_errors @cache_name = cache @store_name = store @type = type @extension = extension @valid_content_types = [content_type].flatten if content_type @valid_content_types ||= Refile.types.fetch(type).content_type if type end
Public Instance Methods
accept()
click to toggle source
# File lib/refile/attachment_definition.rb, line 26 def accept if valid_content_types valid_content_types.join(",") elsif valid_extensions valid_extensions.map { |e| ".#{e}" }.join(",") end end
raise_errors?()
click to toggle source
# File lib/refile/attachment_definition.rb, line 34 def raise_errors? @raise_errors end
valid_extensions()
click to toggle source
# File lib/refile/attachment_definition.rb, line 38 def valid_extensions return unless @extension if @extension.is_a?(Proc) Array(@extension.call) else Array(@extension) end end
validate(attacher)
click to toggle source
# File lib/refile/attachment_definition.rb, line 47 def validate(attacher) extension = attacher.extension.to_s.downcase content_type = attacher.content_type.to_s.downcase content_type = content_type.split(";").first unless content_type.empty? errors = [] errors << extension_error_params(extension) if invalid_extension?(extension) errors << content_type_error_params(content_type) if invalid_content_type?(content_type) errors << :too_large if cache.max_size and attacher.size and attacher.size >= cache.max_size errors << :zero_byte_detected if attacher.size.to_i.zero? errors end
Private Instance Methods
content_type_error_params(content_type)
click to toggle source
# File lib/refile/attachment_definition.rb, line 66 def content_type_error_params(content_type) [:invalid_content_type, content: format_param(content_type), permitted: valid_content_types.to_sentence] end
extension_error_params(extension)
click to toggle source
# File lib/refile/attachment_definition.rb, line 62 def extension_error_params(extension) [:invalid_extension, extension: format_param(extension), permitted: valid_extensions.to_sentence] end
format_param(param)
click to toggle source
# File lib/refile/attachment_definition.rb, line 79 def format_param(param) param.empty? ? I18n.t("refile.empty_param") : param end
invalid_content_type?(content_type)
click to toggle source
# File lib/refile/attachment_definition.rb, line 75 def invalid_content_type?(content_type) valid_content_types and not valid_content_types.include?(content_type) end
invalid_extension?(extension)
click to toggle source
# File lib/refile/attachment_definition.rb, line 70 def invalid_extension?(extension) extension_included = valid_extensions && valid_extensions.map(&:downcase).include?(extension) valid_extensions and not extension_included end