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 (c) 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.

Methods
Classes and Modules
Module Lipsiadmin::Attachment::IOStream
Module Lipsiadmin::Attachment::Storage
Module Lipsiadmin::Attachment::Upfile
Class Lipsiadmin::Attachment::Attach
Class Lipsiadmin::Attachment::Geometry
Class Lipsiadmin::Attachment::Processor
Class Lipsiadmin::Attachment::Tempfile
Class Lipsiadmin::Attachment::Thumbnail
Public Class methods
options()

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.
    # File lib/data_base/attachment/attach.rb, line 43
43:       def options
44:         @options ||= {
45:           :whiny_thumbnails  => false,
46:           :command_path      => nil,
47:           :log               => true,
48:           :swallow_stderr    => true
49:         }
50:       end
run(cmd, params = "", expected_outcodes = 0)

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.

    # File lib/data_base/attachment/attach.rb, line 74
74:       def run(cmd, params = "", expected_outcodes = 0)
75:         command = %Q<#{%Q[#{path_for_command(cmd)} #{params}].gsub(/\s+/, " ")}>
76:         command = "#{command} 2>#{bit_bucket}" if Attachment.options[:swallow_stderr]
77:         output = `#{command}`
78:         unless [expected_outcodes].flatten.include?($?.exitstatus)
79:           raise AttachmentCommandLineError, "Error while running #{cmd}"
80:         end
81:         output
82:       end