Commit f7a5b91b by Prem Sichanugrist

Put the helper methods into Paperclip::Task module

Closes #434
parent ff3929ee
def obtain_class
class_name = ENV['CLASS'] || ENV['class']
raise "Must specify CLASS" unless class_name
class_name
end
module Paperclip
module Task
def self.obtain_class
class_name = ENV['CLASS'] || ENV['class']
raise "Must specify CLASS" unless class_name
class_name
end
def obtain_attachments(klass)
klass = Paperclip.class_for(klass.to_s)
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
def self.obtain_attachments(klass)
klass = Paperclip.class_for(klass.to_s)
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
end
end
......@@ -23,8 +27,8 @@ namespace :paperclip do
desc "Regenerates thumbnails for a given CLASS (and optional ATTACHMENT and STYLES splitted by comma)."
task :thumbnails => :environment do
errors = []
klass = obtain_class
names = obtain_attachments(klass)
klass = Paperclip::Task.obtain_class
names = Paperclip::Task.obtain_attachments(klass)
styles = (ENV['STYLES'] || ENV['styles'] || '').split(',').map(&:to_sym)
names.each do |name|
Paperclip.each_instance_with_attachment(klass, name) do |instance|
......@@ -37,8 +41,8 @@ namespace :paperclip do
desc "Regenerates content_type/size metadata for a given CLASS (and optional ATTACHMENT)."
task :metadata => :environment do
klass = obtain_class
names = obtain_attachments(klass)
klass = Paperclip::Task.obtain_class
names = Paperclip::Task.obtain_attachments(klass)
names.each do |name|
Paperclip.each_instance_with_attachment(klass, name) do |instance|
if file = instance.send(name).to_file(:original)
......@@ -60,8 +64,8 @@ namespace :paperclip do
desc "Cleans out invalid attachments. Useful after you've added new validations."
task :clean => :environment do
klass = obtain_class
names = obtain_attachments(klass)
klass = Paperclip::Task.obtain_class
names = Paperclip::Task.obtain_attachments(klass)
names.each do |name|
Paperclip.each_instance_with_attachment(klass, name) do |instance|
instance.send(name).send(:validate)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment