Module: Forge::CanUseAsset::ClassMethods

Defined in:
lib/forge/lib/forge/can_use_asset.rb

Instance Method Summary collapse

Instance Method Details

#can_use_asset_for(*args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/forge/lib/forge/can_use_asset.rb', line 9

def can_use_asset_for(*args)
  args.each do |field|
    self.send("before_#{field}_post_process".to_sym, "prevent_#{field}_pdf_thumbnail".to_sym)

    self.send(:before_validation, "check_for_#{field}_asset".to_sym)
    self.send(:attr_accessor, "#{field}_asset_id".to_sym, "#{field}_asset_url".to_sym)
    
    define_method("check_for_#{field}_asset".to_sym) do
      raise "Your attachment must have a :thumbnail style (ideally '120x108#') to be compatible with can_use_asset" unless self.send(field.to_sym).styles.has_key?(:thumbnail)
      asset = Asset.find_by_id(self.send("#{field}_asset_id".to_sym))
      self.send("#{field}=".to_sym, File.new(asset.attachment.path)) unless asset.blank?
    end
    
    define_method("#{field}_icon_path".to_sym) do
      icon_path = "/images/forge/asset-icons"
      case self.send("#{field}_content_type")
      when /image/
        self.send(field.to_sym).url(:thumbnail)
      when /audio/
        ActionController::Base.helpers.asset_path "forge/asset-icons/audio.jpg"
      when /excel/
        ActionController::Base.helpers.asset_path "forge/asset-icons/spreadsheet.jpg"
      when /pdf/
        ActionController::Base.helpers.asset_path "forge/asset-icons/pdf.jpg"
      else
        ActionController::Base.helpers.asset_path "forge/asset-icons/misc.jpg"
      end
    end
    
    define_method("prevent_#{field}_pdf_thumbnail".to_sym) do
      return false unless self.send("#{field}_content_type".to_sym).index("image") 
    end

  end
end