Class: Paperclip::Migrator::PaperclipMover

Inherits:
Object
  • Object
show all
Defined in:
lib/paperclip-migrator/paperclip_mover.rb

Constant Summary collapse

DEFAULT_LAYOUTS =
[
  ":rails_root/public/system/:attachment/:id/:style/:filename",
  ":rails_root/public/system/:attachment/:id/:style/:basename.:extension",
  ":rails_root/public/system/:class/:attachment/:id_partition/:style/:basename.:extension",
  ":rails_root/public/system/:class/:attachment/:id_partition/:style/:filename",
  Paperclip::Attachment.default_options[:path]
].uniq
SANTITY_CHECK =
/(:filename|:basename|:id)/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, attachment, old_layout, dry_run = true) ⇒ PaperclipMover

Returns a new instance of PaperclipMover.



59
60
61
62
63
64
65
66
67
68
# File 'lib/paperclip-migrator/paperclip_mover.rb', line 59

def initialize(klass, attachment, old_layout, dry_run = true)
  if old_layout == Paperclip::Attachment.default_options[:path]
    raise "we are already on this layout"
  end

  @klass = klass
  @attachment = attachment
  @old_layout = old_layout
  @dry_run = dry_run
end

Class Method Details

.attachment_keys_for(klass) ⇒ Object



45
46
47
# File 'lib/paperclip-migrator/paperclip_mover.rb', line 45

def attachment_keys_for(klass)
  klass.attachment_definitions.keys.map(&:to_sym)
end

.auto_detected_layouts_for(klass, attachment) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/paperclip-migrator/paperclip_mover.rb', line 15

def auto_detected_layouts_for(klass, attachment)
  [].tap do |auto_detected_layouts|
    possible_attachments_for(klass, attachment).tap do |instances|
      raise "no instances found" if instances.empty?
      instances.each do |inst|
        if inst.send(:"#{attachment}?")
          DEFAULT_LAYOUTS.detect do |layout|
            old_file_name = inst.send(attachment).send(:interpolate, layout)
            if File.exists?(old_file_name)
              auto_detected_layouts << layout unless auto_detected_layouts.include?(layout)
            end
          end
        end
      end
    end
  end
end

.candidate_classesObject



49
50
51
52
53
54
55
56
# File 'lib/paperclip-migrator/paperclip_mover.rb', line 49

def candidate_classes
  ActiveSupport::Autoload.eager_autoload! if defined?(ActiveSupport::Autoload)
  if ActiveRecord::Base.respond_to?(:descendants)
    ActiveRecord::Base.descendants
  else
    Object.subclasses_of(ActiveRecord::Base)
  end.reject { |c| c.attachment_definitions.nil? }.sort { |a, b| a.name <=> b.name }
end

.possible_attachments_for(klass, attachment) ⇒ Object



41
42
43
# File 'lib/paperclip-migrator/paperclip_mover.rb', line 41

def possible_attachments_for(klass, attachment)
  klass.find(:all, :order => "rand()", :limit => 100).select { |inst| inst.send(:"#{attachment}?") }
end

.root_dirObject



33
34
35
36
37
38
39
# File 'lib/paperclip-migrator/paperclip_mover.rb', line 33

def root_dir
  if defined?(Rails)
    Rails.root
  else
    Dir.pwd
  end
end

Instance Method Details

#each_attachmentObject



70
71
72
73
74
75
# File 'lib/paperclip-migrator/paperclip_mover.rb', line 70

def each_attachment
  @klass.all.tap { |collection| raise "No instances found" if collection.empty? }.each do |inst|
    next unless inst.send(:"#{@attachment}?")
    yield AttachmentInstance.new(inst, @klass, @attachment, @old_layout, @dry_run)
  end
end