GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: DataMapper port of the Paperclip plugin by Thoughtbot
Homepage: http://invalidlogic.com/dm-paperclip/
Clone URL: git://github.com/krobertson/dm-paperclip.git
dm-paperclip / tasks / paperclip_tasks.rake
100644 39 lines (35 sloc) 1.107 kb
1
2
3
4
5
6
7
8
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
def obtain_class
  class_name = ENV['CLASS'] || ENV['class']
  raise "Must specify CLASS" unless class_name
  @klass = Object.const_get(class_name)
end
 
def obtain_attachments
  name = ENV['ATTACHMENT'] || ENV['attachment']
  raise "Class #{@klass.name} has no attachments specified" unless @klass.respond_to?(:attachment_definitions)
  if !name.blank? && @klass.attachment_definitions.keys.include?(name)
    [ name ]
  else
    @klass.attachment_definitions.keys
  end
end
 
namespace :paperclip do
  desc "Regenerates thumbnails for a given CLASS (and optional ATTACHMENT)"
  task :refresh => :environment do
    klass = obtain_class
    names = obtain_attachments
    instances = klass.all
 
    puts "Regenerating thumbnails for #{instances.length} instances of #{klass.name}:"
    instances.each do |instance|
      names.each do |name|
        result = if instance.send("#{ name }?")
          instance.send(name).reprocess!
          instance.send(name).save
        else
          true
        end
        print result ? "." : "x"; $stdout.flush
      end
    end
    puts " Done."
  end
end