Commit 24dc7b25 by Mike Burns and George Brocklehurst Committed by Mike Burns

Remove classes_with_attachments

parent d8a721f6
......@@ -177,7 +177,6 @@ module Paperclip
HasAttachedFile.define_on(self, name, options)
options = Paperclip::AttachmentOptions.new(options)
Paperclip.classes_with_attachments << self.name
Paperclip.check_for_path_clash(name, options[:path], self.name)
Paperclip::Tasks::Attachments.add(self, name, options)
......
......@@ -3,15 +3,12 @@ require 'set'
module Paperclip
class << self
attr_accessor :classes_with_attachments
attr_writer :registered_attachments_styles_path
def registered_attachments_styles_path
@registered_attachments_styles_path ||= Rails.root.join('public/system/paperclip_attachments.yml').to_s
end
end
self.classes_with_attachments = Set.new
# Get list of styles saved on previous deploy (running rake paperclip:refresh:missing_styles)
def self.get_registered_attachments_styles
YAML.load_file(Paperclip.registered_attachments_styles_path)
......@@ -37,18 +34,15 @@ module Paperclip
# }
def self.current_attachments_styles
Hash.new.tap do |current_styles|
Paperclip.classes_with_attachments.each do |klass_name|
klass = Paperclip.class_for(klass_name)
Paperclip::Tasks::Attachments.definitions_for(klass).each do |attachment_name, attachment_attributes|
# TODO: is it even possible to take into account Procs?
next if attachment_attributes[:styles].kind_of?(Proc)
attachment_attributes[:styles].try(:keys).try(:each) do |style_name|
klass_sym = klass.to_s.to_sym
current_styles[klass_sym] ||= Hash.new
current_styles[klass_sym][attachment_name.to_sym] ||= Array.new
current_styles[klass_sym][attachment_name.to_sym] << style_name.to_sym
current_styles[klass_sym][attachment_name.to_sym].map!(&:to_s).sort!.map!(&:to_sym).uniq!
end
Paperclip::Tasks::Attachments.each_definition do |klass, attachment_name, attachment_attributes|
# TODO: is it even possible to take into account Procs?
next if attachment_attributes[:styles].kind_of?(Proc)
attachment_attributes[:styles].try(:keys).try(:each) do |style_name|
klass_sym = klass.to_s.to_sym
current_styles[klass_sym] ||= Hash.new
current_styles[klass_sym][attachment_name.to_sym] ||= Array.new
current_styles[klass_sym][attachment_name.to_sym] << style_name.to_sym
current_styles[klass_sym][attachment_name.to_sym].map!(&:to_s).sort!.map!(&:to_sym).uniq!
end
end
end
......
......@@ -9,12 +9,20 @@ module Paperclip
instance.add(klass, attachment_name, attachment_options)
end
def self.clear
instance.clear
end
def self.names_for(klass)
instance.names_for(klass)
end
def self.definitions_for(klass)
instance.definitions_for(klass)
def self.each_definition(&block)
instance.each_definition(&block)
end
def initialize
clear
end
def add(klass, attachment_name, attachment_options)
......@@ -23,12 +31,20 @@ module Paperclip
@attachments[klass][attachment_name] = attachment_options
end
def clear
@attachments = Hash.new { |h,k| h[k] = {} }
end
def names_for(klass)
@attachments[klass].keys
end
def definitions_for(klass)
@attachments[klass]
def each_definition
@attachments.each do |klass, attachments|
attachments.each do |name, options|
yield klass, name, options
end
end
end
end
end
......
......@@ -207,70 +207,6 @@ class IntegrationTest < Test::Unit::TestCase
end
end
context "A model with no convert_options setting" do
setup do
rebuild_model :styles => { :large => "300x300>",
:medium => "100x100",
:thumb => ["32x32#", :gif] },
:default_style => :medium,
:url => "/:attachment/:class/:style/:id/:basename.:extension",
:path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
@dummy = Dummy.new
end
should "have its definition return nil when asked about convert_options" do
assert ! Dummy.attachment_definitions[:avatar][:convert_options]
end
context "redefined to have convert_options setting" do
setup do
rebuild_model :styles => { :large => "300x300>",
:medium => "100x100",
:thumb => ["32x32#", :gif] },
:convert_options => "-strip -depth 8",
:default_style => :medium,
:url => "/:attachment/:class/:style/:id/:basename.:extension",
:path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
end
should "have its definition return convert_options value when asked about convert_options" do
assert_equal "-strip -depth 8", Dummy.attachment_definitions[:avatar][:convert_options]
end
end
end
context "A model with no source_file_options setting" do
setup do
rebuild_model :styles => { :large => "300x300>",
:medium => "100x100",
:thumb => ["32x32#", :gif] },
:default_style => :medium,
:url => "/:attachment/:class/:style/:id/:basename.:extension",
:path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
@dummy = Dummy.new
end
should "have its definition return nil when asked about source_file_options" do
assert ! Dummy.attachment_definitions[:avatar][:source_file_options]
end
context "redefined to have source_file_options setting" do
setup do
rebuild_model :styles => { :large => "300x300>",
:medium => "100x100",
:thumb => ["32x32#", :gif] },
:source_file_options => "-density 400",
:default_style => :medium,
:url => "/:attachment/:class/:style/:id/:basename.:extension",
:path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
end
should "have its definition return source_file_options value when asked about source_file_options" do
assert_equal "-density 400", Dummy.attachment_definitions[:avatar][:source_file_options]
end
end
end
[000,002,022].each do |umask|
context "when the umask is #{umask}" do
setup do
......
......@@ -3,21 +3,10 @@ require './test/helper'
class PaperclipMissingAttachmentStylesTest < Test::Unit::TestCase
context "Paperclip" do
setup do
Paperclip.classes_with_attachments = Set.new
end
teardown do
File.unlink(Paperclip.registered_attachments_styles_path) rescue nil
end
should "be able to keep list of models using it" do
assert_kind_of Set, Paperclip.classes_with_attachments
assert Paperclip.classes_with_attachments.empty?, 'list should be empty'
rebuild_model
assert_equal ['Dummy'].to_set, Paperclip.classes_with_attachments
end
should "enable to get and set path to registered styles file" do
assert_equal ROOT.join('tmp/public/system/paperclip_attachments.yml').to_s, Paperclip.registered_attachments_styles_path
Paperclip.registered_attachments_styles_path = '/tmp/config/paperclip_attachments.yml'
......
......@@ -2,6 +2,10 @@ require './test/helper'
require 'paperclip/tasks/attachments'
class AttachmentsTest < Test::Unit::TestCase
def setup
Paperclip::Tasks::Attachments.clear
end
context '.names_for' do
should 'include attachment names for the given class' do
foo = Class.new
......@@ -18,21 +22,40 @@ class AttachmentsTest < Test::Unit::TestCase
assert_equal [:lover], Paperclip::Tasks::Attachments.names_for(bar)
end
should 'produce the empty array for a missing key' do
assert_empty Paperclip::Tasks::Attachments.names_for(Class.new)
end
end
context '.each_definition' do
should 'call the block with the class, attachment name, and options' do
foo = Class.new
expected_accumulations = [
[foo,:avatar, { yo: 'greeting' }],
[foo, :greeter, { ciao: 'greeting' }]
]
expected_accumulations.each do |args|
Paperclip::Tasks::Attachments.add(*args)
end
accumulations = []
Paperclip::Tasks::Attachments.each_definition do |*args|
accumulations << args
end
assert_equal expected_accumulations, accumulations
end
end
context '.definitions_for' do
should 'produce the attachment name and options' do
expected_definitions = {
avatar: { yo: 'greeting' },
greeter: { ciao: 'greeting' }
}
context '.clear' do
should 'remove all of the existing attachment definitions' do
foo = Class.new
Paperclip::Tasks::Attachments.add(foo, :avatar, { yo: 'greeting' })
Paperclip::Tasks::Attachments.add(foo, :greeter, { ciao: 'greeting' })
definitions = Paperclip::Tasks::Attachments.definitions_for(foo)
Paperclip::Tasks::Attachments.clear
assert_equal expected_definitions, definitions
assert_empty Paperclip::Tasks::Attachments.names_for(foo)
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment