Module: Lipsiadmin::Attachment

Defined in:
lib/data_base/attachment/attach.rb,
lib/data_base/attachment/upfile.rb,
lib/data_base/attachment/storage.rb,
lib/data_base/attachment/geometry.rb,
lib/data_base/attachment/iostream.rb,
lib/data_base/attachment/processor.rb,
lib/data_base/attachment/thumbnail.rb

Overview

Attachment allows file attachments that are stored in the filesystem. All graphical transformations are done using the Graphics/ImageMagick command line utilities and are stored in Tempfiles until the record is saved. Attachment does not require a separate model for storing the attachment’s information, instead adding a few simple columns to your table.

Author

Jon Yurek

Copyright

Copyright © 2008 thoughtbot, inc.

License

MIT License (www.opensource.org/licenses/mit-license.php)

Attachment defines an attachment as any file, though it makes special considerations for image files. You can declare that a model has an attached file with the has_one_attachment method:

From your console:

script/generate attachment

Then in any model you can do:

class User < ActiveRecord::Base
  has_many_attachments                  :attachments, :dependent => :destroy
  has_one_attachment                    :image
  attachment_styles_for                 :attachments, :normal, "128x128!"
  validates_attachment_presence_for     :attachments
  validates_attachment_size_for         :attachments, :greater_than => 10.megabytes
  validates_attachment_content_type_for :attachments, "image/png"
end

See the Lipsiadmin::DataBase::Attachment::ClassMethods documentation for more details.

Defined Under Namespace

Modules: IOStream, Storage, Upfile Classes: Attach, AttachmentCommandLineError, AttachmentError, Geometry, NotIdentifiedByImageMagickError, Processor, Tempfile, Thumbnail

Class Method Summary collapse

Class Method Details

.bit_bucketObject

:nodoc:



89
90
91
# File 'lib/data_base/attachment/attach.rb', line 89

def bit_bucket #:nodoc:
  File.exists?("/dev/null") ? "/dev/null" : "NUL"
end

.interpolates(key, &block) ⇒ Object

:nodoc:



61
62
63
# File 'lib/data_base/attachment/attach.rb', line 61

def interpolates(key, &block) #:nodoc:
  Lipsiadmin::Attachment.interpolations[key] = block
end

.optionsObject

Provides configurability to Attachment. There are a number of options available, such as:

  • whiny_thumbnails: Will raise an error if Attachment cannot process thumbnails of an uploaded image. Defaults to false.

  • log: Logs progress to the Rails log. Uses ActiveRecord’s logger, so honors log levels, etc. Defaults to true.

  • command_path: Defines the path at which to find the command line programs if they are not visible to Rails the system’s search path. Defaults to nil, which uses the first executable found in the user’s search path.

  • image_magick_path: Deprecated alias of command_path.



43
44
45
46
47
48
49
50
# File 'lib/data_base/attachment/attach.rb', line 43

def options
  @options ||= {
    :whiny_thumbnails  => false,
    :command_path      => nil,
    :log               => true,
    :swallow_stderr    => true
  }
end

.path_for_command(command) ⇒ Object

:nodoc:



84
85
86
87
# File 'lib/data_base/attachment/attach.rb', line 84

def path_for_command(command)#:nodoc:
  path = [options[:command_path] || options[:image_magick_path], command].compact
  File.join(*path)
end

.processor(name) ⇒ Object

:nodoc:



52
53
54
55
56
57
58
59
# File 'lib/data_base/attachment/attach.rb', line 52

def processor(name) #:nodoc:
  name = name.to_s.camelize
  processor = Lipsiadmin::Attachment.const_get(name)
  unless processor.ancestors.include?(Lipsiadmin::Attachment::Processor)
    raise AttachmentError.new("Processor #{name} was not found")
  end
  processor
end

.run(cmd, params = "", expected_outcodes = 0) ⇒ Object

The run method takes a command to execute and a string of parameters that get passed to it. The command is prefixed with the :command_path option from Attachment.options. If you have many commands to run and they are in different paths, the suggested course of action is to symlink them so they are all in the same directory.

If the command returns with a result code that is not one of the expected_outcodes, a AttachmentCommandLineError will be raised. Generally a code of 0 is expected, but a list of codes may be passed if necessary.



74
75
76
77
78
79
80
81
82
# File 'lib/data_base/attachment/attach.rb', line 74

def run(cmd, params = "", expected_outcodes = 0)
  command = %Q<#{%Q[#{path_for_command(cmd)} #{params}].gsub(/\s+/, " ")}>
  command = "#{command} 2>#{bit_bucket}" if Attachment.options[:swallow_stderr]
  output = `#{command}`
  unless [expected_outcodes].flatten.include?($?.exitstatus)
    raise AttachmentCommandLineError, "Error while running #{cmd}"
  end
  output
end