Module: ModelAttachment::ClassMethods

Defined in:
lib/model_attachment.rb

Instance Method Summary collapse

Instance Method Details

#attachment_optionsObject

Returns attachment options defined by each call to acts_as_attachment.



114
115
116
117
# File 'lib/model_attachment.rb', line 114

def attachment_options
#      read_inheritable_attribute(:attachment_options)
  self.attachment_options
end

#has_attachment(options = {}) ⇒ Object

has_attachment adds the ability to upload files and make thumbnails of images

  • path: the path format for saving the documents

  • types: a hash of thumbnails to create for images

    :types => {
         :small => { :command => 'convert -geometry 100x100' },
         :large => { :command => 'convert -geometry 500x500' }
    }
    
  • aws: the path to the aws config file or :default for rails config/amazon.yml

    access_key_id:
    secret_access_key:
    
  • logging: set to true to see logging



39
40
41
42
43
44
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
# File 'lib/model_attachment.rb', line 39

def has_attachment(options = {})
  include InstanceMethods
  
  if options[:aws]
    begin
      require 'aws/s3'
    rescue LoadError => e
      e.messages << "You man need to install the aws-s3 gem"
      raise e
    end
  end

  if options[:aws] == :default
    config_file = File.join(Rails.root, "config", "amazon.yml")
    if File.exist?(config_file)
      options[:aws] = config_file
      include AmazonInstanceMethods
    else
      raise("You must provide a config/amazon.yml setup file")
    end
  elsif !options[:aws].nil? && File.exist?(options[:aws])
    include AmazonInstanceMethods
  end

  class_attribute :attachment_options
  self.attachment_options = options
#      write_inheritable_attribute(:attachment_options, options)
    
  # must be before the save to save the attributes
  before_save :save_attributes
  
  # must be after save to get the id for the path
  after_save :save_attached_files
  before_destroy :destroy_attached_files
  
end

#validates_attachment_presence(name, options = {}) ⇒ Object

Places ActiveRecord-style validations on the presence of a file. Options:

  • if: A lambda or name of a method on the instance. Validation will only be run is this lambda or method returns true.

  • unless: Same as if but validates if lambda or method returns false.



105
106
107
108
109
110
111
# File 'lib/model_attachment.rb', line 105

def validates_attachment_presence name, options = {}
  message = options[:message] || "must be set."
  validates_presence_of name, 
                        :message => message,
                        :if      => options[:if],
                        :unless  => options[:unless]
end

#validates_attachment_size(name, options = {}) ⇒ Object

Places ActiveRecord-style validations on the size of the file assigned. The possible options are:

  • in: a Range of bytes (i.e. 1..1.megabyte),

  • less_than: equivalent to :in => 0..options

  • greater_than: equivalent to :in => options..Infinity

  • message: error message to display, use :min and :max as replacements

  • if: A lambda or name of a method on the instance. Validation will only be run is this lambda or method returns true.

  • unless: Same as if but validates if lambda or method returns false.



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/model_attachment.rb', line 86

def validates_attachment_size name, options = {}
  min     = options[:greater_than] || (options[:in] && options[:in].first) || 0
  max     = options[:less_than]    || (options[:in] && options[:in].last)  || (1.0/0)
  range   = (min..max)
  message = options[:message] || "file size must be between :min and :max bytes."
  message = message.gsub(/:min/, min.to_s).gsub(/:max/, max.to_s)

  validates_inclusion_of name,
                         :in      => range,
                         :message => message,
                         :if      => options[:if],
                         :unless  => options[:unless]
end