Class: Refile::Attacher Private

Inherits:
Object
  • Object
show all
Defined in:
lib/refile/attacher.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(record, name, cache:, store:, raise_errors: true, type: nil, extension: nil, content_type: nil) ⇒ Attacher

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 Attacher.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/refile/attacher.rb', line 7

def initialize(record, name, cache:, store:, raise_errors: true, type: nil, extension: nil, content_type: nil)
  @record = record
  @name = name
  @raise_errors = raise_errors
  @cache = Refile.backends.fetch(cache.to_s)
  @store = Refile.backends.fetch(store.to_s)
  @type = type
  @extensions = [extension].flatten if extension
  @content_types = [content_type].flatten if content_type
  @content_types ||= %w[image/jpeg image/gif image/png] if type == :image
  @errors = []
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/attacher.rb', line 4

def cache
  @cache
end

#cache_idObject

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/attacher.rb', line 4

def cache_id
  @cache_id
end

#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/attacher.rb', line 4

def content_types
  @content_types
end

#errorsObject (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/attacher.rb', line 4

def errors
  @errors
end

#extensionsObject (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/attacher.rb', line 4

def extensions
  @extensions
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/attacher.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/attacher.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/attacher.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/attacher.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/attacher.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/attacher.rb', line 4

def type
  @type
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.



90
91
92
93
94
95
96
# File 'lib/refile/attacher.rb', line 90

def accept
  if content_types
    content_types.join(",")
  elsif extensions
    extensions.map { |e| ".#{e}" }.join(",")
  end
end

#cache!(uploadable) ⇒ 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.



44
45
46
47
48
49
50
51
# File 'lib/refile/attacher.rb', line 44

def cache!(uploadable)
  if valid?(uploadable)
    @cache_file = cache.upload(uploadable)
    @cache_id = @cache_file.id
  elsif @raise_errors
    raise Refile::Invalid, @errors.join(", ")
  end
end

#delete!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.



76
77
78
79
80
81
82
83
84
# File 'lib/refile/attacher.rb', line 76

def delete!
  if cached?
    cache.delete(cache_id)
    @cache_id = nil
    @cache_file = nil
  end
  store.delete(id) if id
  self.id = nil
end

#download(url) ⇒ 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.



53
54
55
56
57
58
59
60
# File 'lib/refile/attacher.rb', line 53

def download(url)
  if url and not url == ""
    cache!(RestClient::Request.new(method: :get, url: url, raw_response: true).execute.file)
  end
rescue RestClient::Exception
  @errors = [:download_failed]
  raise if @raise_errors
end

#getObject

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.



28
29
30
31
32
33
34
# File 'lib/refile/attacher.rb', line 28

def get
  if cached?
    cache.get(cache_id)
  elsif id and not id == ""
    store.get(id)
  end
end

#idObject

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.



20
21
22
# File 'lib/refile/attacher.rb', line 20

def id
  record.send(:"#{name}_id")
end

#id=(id) ⇒ 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.



24
25
26
# File 'lib/refile/attacher.rb', line 24

def id=(id)
  record.send(:"#{name}_id=", id) unless record.frozen?
end

#remove?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)


86
87
88
# File 'lib/refile/attacher.rb', line 86

def remove?
  remove and remove != "" and remove !~ /\A0|false$\z/
end

#store!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.



66
67
68
69
70
71
72
73
74
# File 'lib/refile/attacher.rb', line 66

def store!
  if remove?
    delete!
  elsif cached?
    file = store.upload(cache.get(cache_id))
    delete!
    self.id = file.id
  end
end

#valid?(uploadable) ⇒ 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)


36
37
38
39
40
41
42
# File 'lib/refile/attacher.rb', line 36

def valid?(uploadable)
  @errors = []
  @errors << :invalid_extension if @extensions and not valid_extension?(uploadable)
  @errors << :invalid_content_type if @content_types and not valid_content_type?(uploadable)
  @errors << :too_large if cache.max_size and uploadable.size >= cache.max_size
  @errors.empty?
end