This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf61ebd
commit 9b7dd41
Showing
7 changed files
with
99 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,40 @@ | ||
require 'active_support/inflector' | ||
|
||
class StyleAdder | ||
def self.run(table_name, attachment_name, styles) | ||
model = table_name.to_s.??? | ||
new(model, attachment_name, styles).run | ||
new(model_class(table_name), attachment_name, styles).run | ||
end | ||
|
||
def initialize(model_class, attachment_name, styles) | ||
@model_class = model_class | ||
@attachment_name = attachment_name | ||
@styles = styles | ||
end | ||
|
||
def run | ||
p @model_class | ||
p @model_class.all | ||
@model_class.find_each do |model| | ||
attachment = model.send(@attachment_name) | ||
|
||
file = Paperclip.io_adapters.for(attachment) | ||
attachment.instance_variable_set('@queued_for_write', {:original => file}) | ||
|
||
attachment.send(:post_process, *@styles.keys) | ||
|
||
model.save | ||
end | ||
end | ||
|
||
private | ||
|
||
def self.model_class(table_name) | ||
model_class_name = table_name.to_s.singularize.camelize | ||
begin | ||
model_class_name.constantize | ||
rescue NameError | ||
p "here" | ||
raise ArgumentError, "found no model named #{model_class_name}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require './test/helper' | ||
require 'paperclip/style_adder' | ||
|
||
class StyleAdderTest < Test::Unit::TestCase | ||
should 'process the specific style' do | ||
Dummy = rebuild_model styles: { thumbnail: '24x24' }, processors: [:recording] | ||
dummy = Dummy.new | ||
dummy.avatar = File.new(fixture_file("50x50.png"), 'rb') | ||
dummy.save | ||
|
||
Dummy.class_eval do | ||
has_attached_file :avatar, styles: { thumbnail: '24x24', large: '124x124' }, processors: [:recording] | ||
Paperclip.reset_duplicate_clash_check! | ||
end | ||
|
||
RecordingProcessor.clear | ||
|
||
StyleAdder.run :dummies, :avatar, large: '124x124' | ||
|
||
assert RecordingProcessor.has_processed?(large: '124x124') | ||
assert !RecordingProcessor.has_processed?(thumbnail: '24x24') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class RecordingProcessor | ||
def self.make(file, options, attachment) | ||
@style_hashes ||= [] | ||
@style_hashes << options | ||
File.new('/etc/passwd') | ||
end | ||
|
||
def self.clear | ||
@style_hashes = [] | ||
end | ||
|
||
def self.has_processed?(expected_style_hash) | ||
expected_geometries = expected_style_hash.values | ||
@style_hashes && @style_hashes.any? do |style_hash| | ||
expected_geometries.include?(style_hash[:geometry]) | ||
end | ||
end | ||
end | ||
|
||
Paperclip.configure do |c| | ||
c.register_processor :recording, RecordingProcessor | ||
end |