class Refile::Attacher

@api private

Constants

Presence

Attributes

definition[R]
errors[R]
record[R]
remove[RW]

Public Class Methods

new(definition, record) click to toggle source
# File lib/refile/attacher.rb, line 9
def initialize(definition, record)
  @definition = definition
  @record = record
  @errors = []
  @metadata = {}
end

Public Instance Methods

basename() click to toggle source
# File lib/refile/attacher.rb, line 48
def basename
  if filename and extension
    ::File.basename(filename, "." << extension)
  else
    filename
  end
end
cache() click to toggle source
# File lib/refile/attacher.rb, line 20
def cache
  @definition.cache
end
cache!(uploadable) click to toggle source
# File lib/refile/attacher.rb, line 90
def cache!(uploadable)
  @metadata = {
    size: uploadable.size,
    content_type: Refile.extract_content_type(uploadable),
    filename: Refile.extract_filename(uploadable)
  }
  if valid?
    @metadata[:id] = cache.upload(uploadable).id
    write_metadata
  elsif @definition.raise_errors?
    raise Refile::Invalid, @errors.join(", ")
  end
end
cache_id() click to toggle source
# File lib/refile/attacher.rb, line 44
def cache_id
  Presence[@metadata[:id]]
end
content_type() click to toggle source
# File lib/refile/attacher.rb, line 40
def content_type
  Presence[@metadata[:content_type] || read(:content_type)]
end
data() click to toggle source
# File lib/refile/attacher.rb, line 152
def data
  @metadata if valid?
end
delete!() click to toggle source
# File lib/refile/attacher.rb, line 138
def delete!
  cache.delete(cache_id) if cache_id
  store.delete(id) if id
  @metadata = {}
end
download(url) click to toggle source
# File lib/refile/attacher.rb, line 104
def download(url)
  unless url.to_s.empty?
    response = RestClient::Request.new(method: :get, url: url, raw_response: true).execute
    @metadata = {
      size: response.file.size,
      filename: URI.parse(url).path.split("/").last,
      content_type: response.headers[:content_type]
    }
    if valid?
      response.file.open if response.file.closed? # https://github.com/refile/refile/pull/210
      @metadata[:id] = cache.upload(response.file).id
      write_metadata
    elsif @definition.raise_errors?
      raise Refile::Invalid, @errors.join(", ")
    end
  end
rescue RestClient::Exception
  @errors = [:download_failed]
  raise if @definition.raise_errors?
end
extension() click to toggle source
# File lib/refile/attacher.rb, line 56
def extension
  if filename
    Presence[::File.extname(filename).sub(/^\./, "")]
  elsif content_type
    type = MIME::Types[content_type][0]
    type.extensions[0] if type
  end
end
filename() click to toggle source
# File lib/refile/attacher.rb, line 36
def filename
  Presence[@metadata[:filename] || read(:filename)]
end
get() click to toggle source
# File lib/refile/attacher.rb, line 65
def get
  if cache_id
    cache.get(cache_id)
  elsif id
    store.get(id)
  end
end
id() click to toggle source
# File lib/refile/attacher.rb, line 28
def id
  Presence[read(:id, true)]
end
name() click to toggle source
# File lib/refile/attacher.rb, line 16
def name
  @definition.name
end
present?() click to toggle source
# File lib/refile/attacher.rb, line 148
def present?
  not @metadata.empty?
end
remove?() click to toggle source
# File lib/refile/attacher.rb, line 144
def remove?
  remove and remove != "" and remove !~ /\A0|false$\z/
end
retrieve!(value) click to toggle source
# File lib/refile/attacher.rb, line 81
def retrieve!(value)
  if value.is_a?(String)
    @metadata = Refile.parse_json(value, symbolize_names: true) || {}
  elsif value.is_a?(Hash)
    @metadata = value
  end
  write_metadata if cache_id
end
set(value) click to toggle source
# File lib/refile/attacher.rb, line 73
def set(value)
  if value.is_a?(String) or value.is_a?(Hash)
    retrieve!(value)
  else
    cache!(value)
  end
end
size() click to toggle source
# File lib/refile/attacher.rb, line 32
def size
  Presence[@metadata[:size] || read(:size)]
end
store() click to toggle source
# File lib/refile/attacher.rb, line 24
def store
  @definition.store
end
store!() click to toggle source
# File lib/refile/attacher.rb, line 125
def store!
  if remove?
    delete!
    write(:id, nil, true)
  elsif cache_id
    file = store.upload(get)
    delete!
    write(:id, file.id, true)
  end
  write_metadata
  @metadata = {}
end
valid?() click to toggle source
# File lib/refile/attacher.rb, line 156
def valid?
  @errors = @definition.validate(self)
  @errors.empty?
end

Private Instance Methods

read(column, strict = false) click to toggle source
# File lib/refile/attacher.rb, line 163
def read(column, strict = false)
  m = "#{name}_#{column}"
  value ||= record.send(m) if strict or record.respond_to?(m)
  value
end
write(column, value, strict = false) click to toggle source
# File lib/refile/attacher.rb, line 169
def write(column, value, strict = false)
  return if record.frozen?
  m = "#{name}_#{column}="
  record.send(m, value) if strict or record.respond_to?(m)
end
write_metadata() click to toggle source
# File lib/refile/attacher.rb, line 175
def write_metadata
  write(:size, size)
  write(:content_type, content_type)
  write(:filename, filename)
end