paperclip-migrator 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in paperclip-migrator.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Andrew Eberbach
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Paperclip::Migrator
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'paperclip-migrator'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install paperclip-migrator
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require 'paperclip-migrator'
3
+ Paperclip::Migrator::Command.run(ARGV)
@@ -0,0 +1,16 @@
1
+ require "clamp"
2
+ require "progressbar"
3
+ require "highline"
4
+ require "thor"
5
+ require "paperclip"
6
+ require "colored"
7
+ require 'active_record'
8
+
9
+ module Paperclip
10
+ module Migrator
11
+ autoload :AttachmentInstance, 'paperclip-migrator/attachment_instance'
12
+ autoload :Command, 'paperclip-migrator/command'
13
+ autoload :PaperclipMover, 'paperclip-migrator/paperclip_mover'
14
+ autoload :VERSION, 'paperclip-migrator/version'
15
+ end
16
+ end
@@ -0,0 +1,31 @@
1
+ module Paperclip
2
+ module Migrator
3
+ class AttachmentInstance
4
+ def initialize(instance, klass, attachment, old_layout, dry_run = true)
5
+ @dry_run = dry_run
6
+ @instance = instance
7
+ @klass = klass
8
+ @attachment = attachment
9
+ @old_layout = old_layout
10
+ @old_path = @instance.send(@attachment).send(:interpolate, @old_layout)
11
+ end
12
+
13
+ def migrate!
14
+ if File.exists?(@old_path)
15
+ if @dry_run
16
+ puts "Would be loading it from #{@old_path}".green
17
+ else
18
+ begin
19
+ @instance.send(:"#{@attachment}=", File.open(@old_path))
20
+ @instance.save!
21
+ rescue
22
+ raise "#{@klass.name} (##{@instance.id}) got an exception when saving #{@attachment} using #{@old_path}: #{$!}"
23
+ end
24
+ end
25
+ else
26
+ raise "#{@klass.name} (##{@instance.id}) can't find picture for #{@attachment} using #{@old_path}"
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,120 @@
1
+ module Paperclip
2
+ module Migrator
3
+ class Command < Clamp::Command
4
+ option ["-q", "--quiet"], :flag, "suppress output", :default => false
5
+ option ["-p", "--progress-bar"], :flag, "show a progress bar", :default => false
6
+ option ["-d", "--dry-run"], :flag, "dry run, don't actually do antying", :default => true
7
+
8
+ def execute
9
+ warn "This is a dry run, no changes will be made to the actual files." if dry_run?
10
+ begin
11
+ request_class.tap do |klass|
12
+ request_attachment(klass).tap do |attachment|
13
+ request_old_layout(klass, attachment).tap do |old_layout|
14
+ with_progress_bar(klass, attachment) do |progress_bar|
15
+ PaperclipMover.new(klass, attachment, old_layout, dry_run?).each_attachment do |attachment_instance|
16
+ begin
17
+ attachment_instance.migrate!
18
+ rescue
19
+ complain($!)
20
+ end
21
+ progress_bar.try(:inc)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ rescue Interrupt
28
+ error "Control-C, Quitting"
29
+ rescue
30
+ error $!
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def error(message)
37
+ puts message.to_s.red unless quiet?
38
+ exit 1
39
+ end
40
+
41
+ def complain(message)
42
+ puts message.to_s.red unless quiet?
43
+ end
44
+
45
+ def warn(message)
46
+ puts message.to_s.yellow unless quiet?
47
+ end
48
+
49
+ def say(message)
50
+ puts message.green unless quiet?
51
+ end
52
+
53
+ def request_class
54
+ PaperclipMover.candidate_classes.tap do |classes|
55
+ raise "No classes have paperclips" if classes.empty?
56
+ HighLine.new.choose do |menu|
57
+ menu.prompt = "Please choose your class: "
58
+ classes.each do |klass|
59
+ menu.choice(klass.name) { return klass }
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ def request_attachment(klass)
66
+ attachments = PaperclipMover.attachment_keys_for(klass)
67
+
68
+ if attachments.size == 1
69
+ say "Using #{attachments.first} (it's the only one)"
70
+ return attachments.first
71
+ else
72
+ HighLine.new.choose do |menu|
73
+ menu.prompt = "Please choose your attachment: "
74
+ attachments.each do |attachment|
75
+ menu.choice(attachment.to_s) { return attachment.to_sym }
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ def request_old_layout(klass, attachment)
82
+ auto_detected_layouts = PaperclipMover.auto_detected_layouts_for(klass, attachment)
83
+
84
+ HighLine.new.choose do |menu|
85
+ if auto_detected_layouts.empty?
86
+ menu.prompt = "Could not autodetect layout, please select: "
87
+ else
88
+ menu.prompt = "Please select layout: "
89
+ end
90
+
91
+ PaperclipMover::DEFAULT_LAYOUTS.each do |layout|
92
+ label = layout
93
+ if auto_detected_layouts.include?(layout)
94
+ label += " (autodetected)"
95
+ label = label.green
96
+ end
97
+ menu.choice(label) { layout }
98
+ end
99
+ menu.choice("Other") do
100
+ HighLine.new.ask("Enter the layout: ") do |q|
101
+ q.validate = PaperclipMover::SANTITY_CHECK
102
+ q.responses[:not_valid] = "That doesn't look like a Paperclip interpolated path."
103
+ end
104
+ end
105
+ end
106
+ end
107
+
108
+ def with_progress_bar(klass, attachment)
109
+ if quiet? || !progress_bar?
110
+ yield nil
111
+ else
112
+ ProgressBar.new("#{klass}##{attachment}", klass.count).tap do |pbar|
113
+ yield pbar
114
+ pbar.finish
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,78 @@
1
+ module Paperclip
2
+ module Migrator
3
+ class PaperclipMover
4
+ DEFAULT_LAYOUTS = [
5
+ ":rails_root/public/system/:attachment/:id/:style/:filename",
6
+ ":rails_root/public/system/:attachment/:id/:style/:basename.:extension",
7
+ ":rails_root/public/system/:class/:attachment/:id_partition/:style/:basename.:extension",
8
+ ":rails_root/public/system/:class/:attachment/:id_partition/:style/:filename",
9
+ Paperclip::Attachment.default_options[:path]
10
+ ].uniq
11
+
12
+ SANTITY_CHECK = /(:filename|:basename|:id)/
13
+
14
+ class << self
15
+ def auto_detected_layouts_for(klass, attachment)
16
+ [].tap do |auto_detected_layouts|
17
+ possible_attachments_for(klass, attachment).tap do |instances|
18
+ raise "no instances found" if instances.empty?
19
+ instances.each do |inst|
20
+ if inst.send(:"#{attachment}?")
21
+ DEFAULT_LAYOUTS.detect do |layout|
22
+ old_file_name = inst.send(attachment).send(:interpolate, layout)
23
+ if File.exists?(old_file_name)
24
+ auto_detected_layouts << layout unless auto_detected_layouts.include?(layout)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ def root_dir
34
+ if defined?(Rails)
35
+ Rails.root
36
+ else
37
+ Dir.pwd
38
+ end
39
+ end
40
+
41
+ def possible_attachments_for(klass, attachment)
42
+ klass.find(:all, :order => "rand()", :limit => 100).select { |inst| inst.send(:"#{attachment}?") }
43
+ end
44
+
45
+ def attachment_keys_for(klass)
46
+ klass.attachment_definitions.keys.map(&:to_sym)
47
+ end
48
+
49
+ def candidate_classes
50
+ ActiveSupport::Autoload.eager_autoload! if defined?(ActiveSupport::Autoload)
51
+ if ActiveRecord::Base.respond_to?(:descendants)
52
+ ActiveRecord::Base.descendants
53
+ else
54
+ Object.subclasses_of(ActiveRecord::Base)
55
+ end.reject { |c| c.attachment_definitions.nil? }.sort { |a, b| a.name <=> b.name }
56
+ end
57
+ end
58
+
59
+ def initialize(klass, attachment, old_layout, dry_run = true)
60
+ if old_layout == Paperclip::Attachment.default_options[:path]
61
+ raise "we are already on this layout"
62
+ end
63
+
64
+ @klass = klass
65
+ @attachment = attachment
66
+ @old_layout = old_layout
67
+ @dry_run = dry_run
68
+ end
69
+
70
+ def each_attachment
71
+ @klass.all.tap { |collection| raise "No instances found" if collection.empty? }.each do |inst|
72
+ next unless inst.send(:"#{@attachment}?")
73
+ yield AttachmentInstance.new(inst, @klass, @attachment, @old_layout, @dry_run)
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,5 @@
1
+ module Paperclip
2
+ module Migrator
3
+ VERSION = "0.9.1"
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/paperclip-migrator/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Andrew Eberbach"]
6
+ gem.email = ["andrew@ebertech.ca"]
7
+ gem.description = %q{A gem that helps migrate paperclips to a different layout}
8
+ gem.summary = %q{A gem that helps migrate paperclips to a different layout}
9
+ gem.homepage = "https://github.com/ebertech/paperclip-migrator"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "paperclip-migrator"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Paperclip::Migrator::VERSION
17
+
18
+ gem.add_dependency 'activesupport'
19
+ gem.add_dependency "clamp"
20
+ gem.add_dependency "progressbar"
21
+ gem.add_dependency "highline"
22
+ gem.add_dependency "thor"
23
+ gem.add_dependency "paperclip"
24
+ gem.add_dependency "colored"
25
+
26
+ gem.add_development_dependency "rspec", ">= 2.0"
27
+ gem.add_development_dependency 'rails', "~> 2.3.14"
28
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe Paperclip::Migrator::PaperclipMover do
4
+ describe "class methods" do
5
+ describe ".candidate_classes" do
6
+ subject { Paperclip::Migrator::PaperclipMover.candidate_classes }
7
+ it "be empty if there are no classes that have paperclips" do
8
+ should be_empty
9
+ end
10
+ end
11
+
12
+ describe ".root_dir" do
13
+ context "with Rails defined" do
14
+ before(:each) do
15
+ Rails = double("rails").tap do |double|
16
+ double.stub(:root) { "/some/path" }
17
+ end
18
+ end
19
+
20
+ it "should return the current Rails.root" do
21
+ Paperclip::Migrator::PaperclipMover.root_dir.should == Rails.root
22
+ end
23
+
24
+ after(:each) do
25
+ Object.instance_eval{remove_const(:Rails)}
26
+ end
27
+ end
28
+
29
+ context "without Rails defined" do
30
+ it "should return the current Rails.root" do
31
+ Paperclip::Migrator::PaperclipMover.root_dir.should == Dir.pwd
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ it "should raise an exception if we are trying to migrate to the current layout" do
38
+ expect do
39
+ Paperclip::Migrator::PaperclipMover.new(nil, nil, Paperclip::Attachment.default_options[:path])
40
+ end.to raise_exception RuntimeError, "we are already on this layout"
41
+ end
42
+ end
@@ -0,0 +1,20 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper.rb"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ ENV["RAILS_ENV"] = "test"
8
+
9
+ require 'active_support/all'
10
+ require File.expand_path("../../lib/paperclip-migrator", __FILE__)
11
+ ActiveSupport::Deprecation.silenced = true
12
+
13
+ # Load support files
14
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
15
+
16
+ RSpec.configure do |config|
17
+ config.treat_symbols_as_metadata_keys_with_true_values = true
18
+ config.run_all_when_everything_filtered = true
19
+ config.filter_run :focus
20
+ end
metadata ADDED
@@ -0,0 +1,210 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paperclip-migrator
3
+ version: !ruby/object:Gem::Version
4
+ hash: 57
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 9
9
+ - 1
10
+ version: 0.9.1
11
+ platform: ruby
12
+ authors:
13
+ - Andrew Eberbach
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-06-13 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activesupport
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: clamp
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: progressbar
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ type: :runtime
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: highline
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ type: :runtime
75
+ version_requirements: *id004
76
+ - !ruby/object:Gem::Dependency
77
+ name: thor
78
+ prerelease: false
79
+ requirement: &id005 !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ type: :runtime
89
+ version_requirements: *id005
90
+ - !ruby/object:Gem::Dependency
91
+ name: paperclip
92
+ prerelease: false
93
+ requirement: &id006 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ type: :runtime
103
+ version_requirements: *id006
104
+ - !ruby/object:Gem::Dependency
105
+ name: colored
106
+ prerelease: false
107
+ requirement: &id007 !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ hash: 3
113
+ segments:
114
+ - 0
115
+ version: "0"
116
+ type: :runtime
117
+ version_requirements: *id007
118
+ - !ruby/object:Gem::Dependency
119
+ name: rspec
120
+ prerelease: false
121
+ requirement: &id008 !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ hash: 3
127
+ segments:
128
+ - 2
129
+ - 0
130
+ version: "2.0"
131
+ type: :development
132
+ version_requirements: *id008
133
+ - !ruby/object:Gem::Dependency
134
+ name: rails
135
+ prerelease: false
136
+ requirement: &id009 !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ hash: 31
142
+ segments:
143
+ - 2
144
+ - 3
145
+ - 14
146
+ version: 2.3.14
147
+ type: :development
148
+ version_requirements: *id009
149
+ description: A gem that helps migrate paperclips to a different layout
150
+ email:
151
+ - andrew@ebertech.ca
152
+ executables:
153
+ - paperclip_migrate
154
+ extensions: []
155
+
156
+ extra_rdoc_files: []
157
+
158
+ files:
159
+ - .gitignore
160
+ - .rspec
161
+ - Gemfile
162
+ - LICENSE
163
+ - README.md
164
+ - Rakefile
165
+ - bin/paperclip_migrate
166
+ - lib/paperclip-migrator.rb
167
+ - lib/paperclip-migrator/attachment_instance.rb
168
+ - lib/paperclip-migrator/command.rb
169
+ - lib/paperclip-migrator/paperclip_mover.rb
170
+ - lib/paperclip-migrator/version.rb
171
+ - paperclip-migrator.gemspec
172
+ - spec/models/paperclip_mover_spec.rb
173
+ - spec/spec_helper.rb
174
+ homepage: https://github.com/ebertech/paperclip-migrator
175
+ licenses: []
176
+
177
+ post_install_message:
178
+ rdoc_options: []
179
+
180
+ require_paths:
181
+ - lib
182
+ required_ruby_version: !ruby/object:Gem::Requirement
183
+ none: false
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ hash: 3
188
+ segments:
189
+ - 0
190
+ version: "0"
191
+ required_rubygems_version: !ruby/object:Gem::Requirement
192
+ none: false
193
+ requirements:
194
+ - - ">="
195
+ - !ruby/object:Gem::Version
196
+ hash: 3
197
+ segments:
198
+ - 0
199
+ version: "0"
200
+ requirements: []
201
+
202
+ rubyforge_project:
203
+ rubygems_version: 1.8.21
204
+ signing_key:
205
+ specification_version: 3
206
+ summary: A gem that helps migrate paperclips to a different layout
207
+ test_files:
208
+ - spec/models/paperclip_mover_spec.rb
209
+ - spec/spec_helper.rb
210
+ has_rdoc: