Module: ActiveRecord::Acts::UploaderUpload::ClassMethods

Defined in:
lib/active_record/acts/uploader_upload.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_uploader(options = {}) ⇒ Object

options:

S3_no_wait - Immediately send files to S3 instead of waiting for an aysnc process to send them over.
keep_local_file - If using S3 determines whether or not to keep files local as well as sending to S3.

acts_as_uploader requires an option for :has_attached_file. These values will be passed to paperclip. i.e. acts_as_uploader :has_attached_file => { :url => “/uploads/:class/:id/:style_:basename.:extension”, :path => “:rails_root/public/uploads/:class/:id/:style_:basename.:extension”, :styles => { :icon => “30x30!”, :thumb => “100>”, :small => “150>”, :medium => “300>”, :large => “660>”}, :default_url => “/images/profile_default.jpg” }, :disable_halt_nonimage_processing => false

disable_halt_nonimage_processing - By default all post processing is turned off for non image files. This is useful if you want to setup styles to generate thumbnails for

images but don't want the default Geometry processor to die on non-image files.


26
27
28
29
30
31
32
33
34
35
36
37
38
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
# File 'lib/active_record/acts/uploader_upload.rb', line 26

def acts_as_uploader(options = {})

  default_options = {
    :S3_no_wait => false,
    :keep_local_file => true
  }
  
  options = default_options.merge(options)
  
  #Named scopes
  named_scope :newest, :order => "created_at DESC"
  named_scope :by_filename, :order => "local_file_name DESC"
  named_scope :newest, :order => "created_at DESC"
  named_scope :public, :conditions => 'is_public = true'
  named_scope :images, :conditions => "local_content_type IN (#{Uploader::MimeTypeGroups::IMAGE_TYPES.collect{|type| "'#{type}'"}.join(',')})"
  named_scope :documents, :conditions => "local_content_type IN (#{(Uploader::MimeTypeGroups::WORD_TYPES + Uploader::MimeTypeGroups::EXCEL_TYPES + Uploader::MimeTypeGroups::PDF_TYPES).collect{|type| "'#{type}'"}.join(',')})" 
  named_scope :files, :conditions => "local_content_type NOT IN (#{Uploader::MimeTypeGroups::IMAGE_TYPES.collect{|type| "'#{type}'"}.join(',')})"
  named_scope :recent, lambda { |*args| { :conditions => ["created_at > ?", (args.first || 7.days.ago.to_s(:db)) ]} }
  named_scope :created_by, lambda { |*args| { :conditions => ["creator_id = ?", (args.first) ]} }
  named_scope :pending_s3_migrations, lambda { { :conditions =>  ["remote_file_name IS NULL"], :order => 'created_at DESC' } }

  @uploader_s3_no_wait = options.delete(:S3_no_wait)
  @uploader_keep_file_local = options.delete(:keep_local_file)
  
  # Paperclip
  has_attached_file :local, options[:has_attached_file].merge(:storage => :filesystem) # Override any storage settings.  This one has to be local.
  has_attached_file :remote, options[:has_attached_file].merge(:url => ':s3_alias_url',
                                                               :path => options[:s3_path],
                                                               :storage => :s3)

  belongs_to :uploadable, :polymorphic => true
  belongs_to :creator, :class_name => 'User', :foreign_key => 'creator_id'
  
  before_save :determine_immediate_send_to_remote
                                
  class_eval <<-EOV
    
    before_post_process :transliterate_file_name
    before_post_process :halt_nonimage_processing unless options[:disable_halt_nonimage_processing]
    before_create :add_width_and_height
    
    # prevents a user from submitting a crafted form that bypasses activation
    attr_protected :creator_id, :uploadable_id, :uploadable_type
  EOV

  include ActiveRecord::Acts::UploaderUpload::InstanceMethods
  extend ActiveRecord::Acts::UploaderUpload::SingletonMethods
end