Class: Paperclip::Migrator::PaperclipMover
- Inherits:
-
Object
- Object
- Paperclip::Migrator::PaperclipMover
- 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.[:path] ].uniq
- SANTITY_CHECK =
/(:filename|:basename|:id)/
Class Method Summary collapse
- .attachment_keys_for(klass) ⇒ Object
- .auto_detected_layouts_for(klass, attachment) ⇒ Object
- .candidate_classes ⇒ Object
- .possible_attachments_for(klass, attachment) ⇒ Object
- .root_dir ⇒ Object
Instance Method Summary collapse
- #each_attachment ⇒ Object
-
#initialize(klass, attachment, old_layout, dry_run = true) ⇒ PaperclipMover
constructor
A new instance of PaperclipMover.
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, , old_layout, dry_run = true) if old_layout == Paperclip::Attachment.[:path] raise "we are already on this layout" end @klass = klass @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 (klass) klass..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, ) [].tap do |auto_detected_layouts| (klass, ).tap do |instances| raise "no instances found" if instances.empty? instances.each do |inst| if inst.send(:"#{}?") DEFAULT_LAYOUTS.detect do |layout| old_file_name = inst.send().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_classes ⇒ Object
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..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 (klass, ) klass.find(:all, :order => "rand()", :limit => 100).select { |inst| inst.send(:"#{}?") } end |
.root_dir ⇒ Object
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_attachment ⇒ Object
70 71 72 73 74 75 |
# File 'lib/paperclip-migrator/paperclip_mover.rb', line 70 def @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 |