class Stratosphere::Attachment

Attributes

base_path[RW]
config[RW]
file_name[RW]
file_store[RW]
mime_type[RW]
name[RW]
owner[RW]
size[RW]
type[RW]

Public Class Methods

create_config(options={}) click to toggle source
# File lib/stratosphere/attachment.rb, line 5
def self.create_config(options={})
  config = Stratosphere.config.to_hash

  unless options.nil?
    unless options[:aws].nil?
      options[:aws] = config[:aws].merge(options[:aws])
    end
    config.merge!(options)
  end
  Stratosphere::Config.new(config)
end
new(owner, name, options={}) click to toggle source
# File lib/stratosphere/attachment.rb, line 17
def initialize(owner, name, options={})
  @config     = self.class.create_config(options[:config])
  @name       = name
  @owner      = owner
  @file_name  = @owner["#{@name}_file"]
  @file_size  = @owner["#{@name}_content_length"]
  @mime_type  = @owner["#{@name}_content_type"]
  @type       = :attachment
  @file_store = Stratosphere::AWS::S3.new(@config)
  set_base_path
end

Public Instance Methods

destroy!() click to toggle source
# File lib/stratosphere/attachment.rb, line 59
def destroy!
  file_store.delete_objects base_path
end
exists?() click to toggle source
# File lib/stratosphere/attachment.rb, line 41
def exists?
  !file_name.nil?
end
has_default?() click to toggle source
# File lib/stratosphere/attachment.rb, line 45
def has_default?
  false
end
presigned_upload(options) click to toggle source
# File lib/stratosphere/attachment.rb, line 53
def presigned_upload(options)
  options.merge!(key: "#{base_path}/#{options[:file_name]}")
  options.delete :file_name
  file_store.presigned_upload options
end
set_base_path() click to toggle source
# File lib/stratosphere/attachment.rb, line 29
def set_base_path
  plural_attr  = name.to_s.pluralize
  plural_model = owner.class.to_s.downcase.pluralize

  if config.dir_prefix
    prefix     = config.dir_prefix[-1, 1] == '/' ? config.dir_prefix.slice(0, -1) : config.dir_prefix
    @base_path = "#{prefix}/#{plural_model}/#{plural_attr}/#{owner.id}"
  else
    @base_path = "#{plural_model}/#{plural_attr}/#{owner.id}"
  end
end
url() click to toggle source
# File lib/stratosphere/attachment.rb, line 49
def url
  "#{config.domain}/#{base_path}/#{file_name}" if file_name
end