Class: Refile::AttachmentDefinition Private

Inherits:
Object
  • Object
show all
Defined in:
lib/refile/attachment_definition.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, cache:, store:, raise_errors: true, type: nil, extension: nil, content_type: nil) ⇒ AttachmentDefinition

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of AttachmentDefinition.



7
8
9
10
11
12
13
14
15
16
# 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

Instance Attribute Details

#cacheObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



4
5
6
# File 'lib/refile/attachment_definition.rb', line 4

def cache
  @cache
end

#nameObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



4
5
6
# File 'lib/refile/attachment_definition.rb', line 4

def name
  @name
end

#optionsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



4
5
6
# File 'lib/refile/attachment_definition.rb', line 4

def options
  @options
end

#recordObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



4
5
6
# File 'lib/refile/attachment_definition.rb', line 4

def record
  @record
end

#removeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



5
6
7
# File 'lib/refile/attachment_definition.rb', line 5

def remove
  @remove
end

#storeObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



4
5
6
# File 'lib/refile/attachment_definition.rb', line 4

def store
  @store
end

#typeObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



4
5
6
# File 'lib/refile/attachment_definition.rb', line 4

def type
  @type
end

#valid_content_typesObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



4
5
6
# File 'lib/refile/attachment_definition.rb', line 4

def valid_content_types
  @valid_content_types
end

Instance Method Details

#acceptObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
29
30
31
32
# 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?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


34
35
36
# File 'lib/refile/attachment_definition.rb', line 34

def raise_errors?
  @raise_errors
end

#valid_extensionsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



38
39
40
41
42
43
44
45
# 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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



47
48
49
50
51
52
53
54
55
56
57
58
# 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