Class: Paperclip::Migrator::AttachmentInstance

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of AttachmentInstance.



4
5
6
7
8
9
10
11
# File 'lib/paperclip-migrator/attachment_instance.rb', line 4

def initialize(instance, klass, attachment, old_layout, dry_run = true)
  @dry_run = dry_run
  @instance = instance
  @klass = klass
  @attachment = attachment
  @old_layout = old_layout
  @old_path = @instance.send(@attachment).send(:interpolate, @old_layout)
end

Instance Method Details

#migrate!Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/paperclip-migrator/attachment_instance.rb', line 13

def migrate!
  if File.exists?(@old_path)
    if @dry_run
      puts "Would be loading it from #{@old_path}".green
    else
      begin
        @instance.send(:"#{@attachment}=", File.open(@old_path))
        @instance.save!
      rescue
        raise "#{@klass.name} (##{@instance.id}) got an exception when saving #{@attachment} using #{@old_path}: #{$!}"
      end
    end
  else
    raise "#{@klass.name} (##{@instance.id}) can't find picture for #{@attachment} using #{@old_path}"
  end
end