Module: HasImages::ClassMethods

Defined in:
lib/has_images.rb

Instance Method Summary collapse

Instance Method Details

#has_images(options = {}) ⇒ Object

adds has_images to model



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/has_images.rb', line 10

def has_images(options={})
  counter_cache = options.delete(:counter_cache) || false
  # eval is not always evil ;)
  # we generate a Digineo::Model::Image clase to store the given paperclip configuration in it  
  eval <<-EOF
    module Digineo::#{self.name} 
      class Digineo::#{self.name}::Image < Digineo::Image
         has_attached_file :file, #{options.inspect}
         belongs_to :parentmodel, :polymorphic => true, :counter_cache => #{counter_cache.inspect}
      end
    end
  EOF
  
  has_many :images, :as => :parentmodel, :dependent => :destroy, :order => 'id ASC', :class_name => "Digineo::#{self.name}::Image"
  has_one  :avatar, :as => :parentmodel, :conditions => 'avatar=1', :class_name => "Digineo::#{self.name}::Image"      
  has_many :galleries, :as => :parentmodel, :dependent => :destroy, :class_name => 'Digineo::ImageGallery'            
  
  named_scope :with_avatar, :include => :avatar
  
  send :include, InstanceMethods 
end