Class: Bulldog::Attachment::Base

Inherits:
Maybe
  • Object
show all
Defined in:
lib/bulldog/attachment/base.rb

Direct Known Subclasses

Image, Pdf, Unknown, Video

Instance Attribute Summary

Attributes inherited from Maybe

#name, #record, #saved, #stream, #value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Maybe

#==, #copy_for, handle, #interpolate_path, #interpolate_url, #load, #read_storable_attributes, #reflection, #reload, #saved?, #type

Constructor Details

#initialize(record, name, stream) ⇒ Base

Returns a new instance of Base.



13
14
15
16
# File 'lib/bulldog/attachment/base.rb', line 13

def initialize(record, name, stream)
  super
  @examination_result = nil
end

Class Method Details

.try(record, name, stream) ⇒ Object

Return an instance of the record if the stream is valid, nil otherwise.



8
9
10
11
# File 'lib/bulldog/attachment/base.rb', line 8

def self.try(record, name, stream)
  attachment = new(record, name, stream)
  attachment.send(:examine) ? attachment : nil
end

Instance Method Details

#content_typeObject

Return the content type of the data.



80
81
82
# File 'lib/bulldog/attachment/base.rb', line 80

def content_type
  @content_type ||= stream.content_type
end

#destroyObject

Called when the attachment is destroyed, or just before another attachment is saved in its place.



98
99
100
# File 'lib/bulldog/attachment/base.rb', line 98

def destroy
  delete_files_and_empty_parent_directories
end

#examineObject

Ensure the file examination has been run, and return the result.



113
114
115
116
117
118
119
# File 'lib/bulldog/attachment/base.rb', line 113

def examine
  if @examination_result.nil?
    @examination_result = run_examination
  else
    @examination_result
  end
end

#file_nameObject

Return the original file name of the attached file.



73
74
75
# File 'lib/bulldog/attachment/base.rb', line 73

def file_name
  @file_name ||= stream.file_name
end

#file_sizeObject

Return the size of the attached file.



66
67
68
# File 'lib/bulldog/attachment/base.rb', line 66

def file_size
  stream.size
end

#path(style_name = reflection.default_style) ⇒ Object

Return the path on the file system where the given style’s image is to be stored.



52
53
54
# File 'lib/bulldog/attachment/base.rb', line 52

def path(style_name = reflection.default_style)
  interpolate_path(style_name)
end

#process(event_name, options = {}) ⇒ Object

Run the processors for the named event.

Return true if no errors were encountered, false otherwise.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bulldog/attachment/base.rb', line 23

def process(event_name, options={})
  reflection.events[event_name].each do |event|
    if (types = event.attachment_types)
      next unless types.include?(type)
    end
    processor_type = event.processor_type || default_processor_type
    processor_class = Processor.const_get(processor_type.to_s.camelize)
    processor = processor_class.new(self, options[:input] || stream.path)
    styles = reflection.styles
    names = options[:styles] || event.styles and
      styles = reflection.styles.slice(*names)
    processor.process(styles, options, &event.callback)
  end
  record.errors.empty?
end

#process!(event_name, options = {}, &block) ⇒ Object

Like #process, but raise ActiveRecord::RecordInvalid if there are any errors.



43
44
45
46
# File 'lib/bulldog/attachment/base.rb', line 43

def process!(event_name, options={}, &block)
  process(event_name, options, &block) or
    raise ActiveRecord::RecordInvalid, record
end

#saveObject

Called when the attachment is saved.



87
88
89
90
91
92
# File 'lib/bulldog/attachment/base.rb', line 87

def save
  return if saved?
  self.saved = true
  original_path = interpolate_path(:original)
  stream.write_to(original_path)
end

#unloadObject



121
122
123
124
# File 'lib/bulldog/attachment/base.rb', line 121

def unload
  super
  @examination_result = nil
end

#url(style_name = reflection.default_style) ⇒ Object

Return the URL where the given style’s image is to be found.



59
60
61
# File 'lib/bulldog/attachment/base.rb', line 59

def url(style_name = reflection.default_style)
  interpolate_url(style_name)
end