Class: AttachmentOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/attach_it/attachment_options.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model = nil, name = nil, options = {}) ⇒ AttachmentOptions

Returns a new instance of AttachmentOptions.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/attach_it/attachment_options.rb', line 7

def initialize(model = nil, name = nil, options = {})
  @model = model
  @class_name = model.class.name.downcase
  @object_id = model.id.to_s

  @name = name
  @attachment = name.to_s.downcase.pluralize

  @url = set_url(options[:url], options[:default_url])
  @path = set_path(options[:path])
  @styles = set_styles(options[:styles])
  @storage = set_storage(options[:storage])

  if !@model.send("#{@name.to_s}_file_name").nil?
    @filename = @model.send("#{@name.to_s}_file_name")
    @extension = set_extension(@model.send("#{@name.to_s}_file_name"))
  end

  @queued_for_delete = set_queued_for_delete

  @errors = Hash.new
end

Instance Attribute Details

#assigned_fileObject

Returns the value of attribute assigned_file.



5
6
7
# File 'lib/attach_it/attachment_options.rb', line 5

def assigned_file
  @assigned_file
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/attach_it/attachment_options.rb', line 5

def name
  @name
end

#object_idObject

Returns the value of attribute object_id.



5
6
7
# File 'lib/attach_it/attachment_options.rb', line 5

def object_id
  @object_id
end

#stylesObject

Returns the value of attribute styles.



5
6
7
# File 'lib/attach_it/attachment_options.rb', line 5

def styles
  @styles
end

Instance Method Details

#add_error(description = nil) ⇒ Object



67
68
69
# File 'lib/attach_it/attachment_options.rb', line 67

def add_error(description = nil)
  (@errors[:processing] ||= []) << description
end

#assign(file = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/attach_it/attachment_options.rb', line 30

def assign(file = nil)
  @errors = {}

  @filename = file_name(file)
  @extension = set_extension(@filename)
  file = file.tempfile if file.respond_to?(:tempfile)

  @model.send("#{@name.to_s}_file_name=", @filename)
  @model.send("#{@name.to_s}_file_size=", File.size(file))
  @model.send("#{@name.to_s}_content_type=", Wand.wave(file.path))
  @model.send("#{@name.to_s}_updated_at=", Time.now)

  add_error('Could not resize file') unless file_is_resizale?
end

#base64(style = 'original') ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/attach_it/attachment_options.rb', line 90

def base64(style = 'original')
  begin
    bytes = nil

    if @storage.is_a?(Gridfs)
      bytes = get_from_gridfs(style).read
    elsif @storage.is_a?(Filesystem)
      bytes = File.open(path(style), 'rb').read
    end

    'data:' + @model.send("#{@name.to_s}_content_type") + ';base64,' + Base64.encode64(bytes)
  rescue Exception => exception
    nil
  end
end

#deleteObject



63
64
65
# File 'lib/attach_it/attachment_options.rb', line 63

def delete
  @storage.flush_delete(@queued_for_delete)
end

#file_name(file = nil) ⇒ Object



77
78
79
80
# File 'lib/attach_it/attachment_options.rb', line 77

def file_name(file = nil)
  @assigned_file ||= file
  @assigned_file.respond_to?(:original_filename) ? @assigned_file.original_filename : File.basename(@assigned_file.path)
end

#flush_errorsObject



71
72
73
74
75
# File 'lib/attach_it/attachment_options.rb', line 71

def flush_errors
  @errors.each do |error, message|
    [message].flatten.each { |m| @model.errors.add(@name, m) }
  end
end

#get_from_gridfs(style = 'original') ⇒ Object



82
83
84
85
86
87
88
# File 'lib/attach_it/attachment_options.rb', line 82

def get_from_gridfs(style = 'original')
  if @storage.is_a?(Gridfs)
    @storage.read("#{@object_id}_#{@name}_#{style}")
  else
    nil
  end
end

#path(style_name = 'original') ⇒ Object



50
51
52
53
# File 'lib/attach_it/attachment_options.rb', line 50

def path(style_name = 'original')
  return nil unless @storage.is_a?(Filesystem)
  interpolate(@path, style_name)
end

#saveObject



55
56
57
58
59
60
61
# File 'lib/attach_it/attachment_options.rb', line 55

def save
  unless @assigned_file.nil?
    @storage.flush_delete(@queued_for_delete)
    @storage.flush_write(self)
    @queued_for_delete = set_queued_for_delete
  end
end

#url(style_name = 'original') ⇒ Object



45
46
47
48
# File 'lib/attach_it/attachment_options.rb', line 45

def url(style_name = 'original')
  return nil unless @storage.is_a?(Filesystem)
  interpolate(@url, style_name)
end