Module: AttachmerbFu::ActMethods

Defined in:
lib/attachmerb_fu.rb

Instance Method Summary collapse

Instance Method Details

#has_attachment(options = {}) ⇒ Object

Options:

  • :content_type - Allowed content types. Allows all by default. Use :image to allow all standard image types.

  • :min_size - Minimum size allowed. 1 byte is the default.

  • :max_size - Maximum size allowed. 1.megabyte is the default.

  • :size - Range of sizes allowed. (1..1.megabyte) is the default. This overrides the :min_size and :max_size options.

  • :resize_to - Used by RMagick to resize images. Pass either an array of width/height, or a geometry string.

  • :thumbnails - Specifies a set of thumbnails to generate. This accepts a hash of filename suffixes and RMagick resizing options.

  • :thumbnail_class - Set what class to use for thumbnails. This attachment class is used by default.

  • :path_prefix - path to store the uploaded files. Uses public/#table_name by default for the filesystem, and just #table_name

    for the S3 backend.  Setting this sets the :storage to :file_system.
    
  • :storage - Use :file_system to specify the attachment data is stored with the file system. Defaults to :db_system.

Examples:

has_attachment :max_size => 1.kilobyte
has_attachment :size => 1.megabyte..2.megabytes
has_attachment :content_type => 'application/pdf'
has_attachment :content_type => ['application/pdf', 'application/msword', 'text/plain']
has_attachment :content_type => :image, :resize_to => [50,50]
has_attachment :content_type => ['application/pdf', :image], :resize_to => 'x50'
has_attachment :thumbnails => { :thumb => [50, 50], :geometry => 'x50' }
has_attachment :storage => :file_system, :path_prefix => 'public/files'
has_attachment :storage => :file_system, :path_prefix => 'public/files', 
  :content_type => :image, :resize_to => [50,50]
has_attachment :storage => :file_system, :path_prefix => 'public/files',
  :thumbnails => { :thumb => [50, 50], :geometry => 'x50' }
has_attachment :storage => :s3


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/attachmerb_fu.rb', line 45

def has_attachment(options = {})
  # this allows you to redefine the acts' options for each subclass, however
  options[:min_size]         ||= 1
  options[:max_size]         ||= 1024*1024*1024
  options[:size]             ||= (options[:min_size]..options[:max_size])
  options[:thumbnails]       ||= {}
  options[:thumbnail_class]  ||= self
  options[:s3_access]        ||= :public_read
  options[:content_type] = [options[:content_type]].flatten.collect! { |t| t == :image ? AttachmerbFu.content_types : t }.flatten unless options[:content_type].nil?
  
  unless options[:thumbnails].is_a?(Hash)
    raise ArgumentError, ":thumbnails option should be a hash: e.g. :thumbnails => { :foo => '50x50' }"
  end
  
  # doing these shenanigans so that #attachment_options is available to processors and backends
  class_inheritable_accessor :attachment_options
  self.attachment_options = options

  # only need to define these once on a class
  unless included_modules.include?(InstanceMethods)
    attr_accessor :thumbnail_resize_options

    attachment_options[:storage]     ||= :file_system
    attachment_options[:path_prefix] ||= attachment_options[:file_system_path]
    if attachment_options[:path_prefix].nil?
      attachment_options[:path_prefix] = File.join("public", table.name)
    end
    attachment_options[:path_prefix]   = attachment_options[:path_prefix][1..-1] if options[:path_prefix][0] == '/'

    has_many   :thumbnails, :class_name => attachment_options[:thumbnail_class].to_s, :foreign_key => 'parent_id'
    belongs_to :parent, :class_name => table.klass.to_s, :foreign_key => 'parent_id'

    before_update :rename_file
    before_destroy :destroy_thumbnails

    before_validation :set_size_from_temp_path
    after_save :after_process_attachment
    after_destroy :destroy_file
    extend  ClassMethods
    include InstanceMethods
    
    backend = "#{options[:storage].to_s.classify}Backend"
    require "attachmerb_fu/backends/#{backend.snake_case}"
    include AttachmerbFu::Backends.const_get(backend)

    case attachment_options[:processor]
      when :none
      when nil
        processors = AttachmerbFu.default_processors.dup
        begin
          if processors.any?
            attachment_options[:processor] = "#{processors.first}Processor"
            require "attachmerb_fu/processors/#{attachment_options[:processor].snake_case}"
            
            include AttachmerbFu::Processors.const_get(attachment_options[:processor])
          end
        rescue LoadError
          processors.shift
          retry
        end
      else
        begin
          processor = "#{options[:processor].to_s.classify}Processor"
          require "attachmerb_fu/processors/#{processor.snake_case}"
          include AttachmerbFu::Processors.const_get(processor)
        rescue LoadError
          puts "Problems loading #{processor}: #{$!}"
        end
    end
    
    after_validation :process_attachment
  end
end