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

Remove classes_with_attachments

parent d8a721f6
...@@ -177,7 +177,6 @@ module Paperclip ...@@ -177,7 +177,6 @@ module Paperclip
HasAttachedFile.define_on(self, name, options) HasAttachedFile.define_on(self, name, options)
options = Paperclip::AttachmentOptions.new(options) options = Paperclip::AttachmentOptions.new(options)
Paperclip.classes_with_attachments << self.name
Paperclip.check_for_path_clash(name, options[:path], self.name) Paperclip.check_for_path_clash(name, options[:path], self.name)
Paperclip::Tasks::Attachments.add(self, name, options) Paperclip::Tasks::Attachments.add(self, name, options)
......
...@@ -3,15 +3,12 @@ require 'set' ...@@ -3,15 +3,12 @@ require 'set'
module Paperclip module Paperclip
class << self class << self
attr_accessor :classes_with_attachments
attr_writer :registered_attachments_styles_path attr_writer :registered_attachments_styles_path
def registered_attachments_styles_path def registered_attachments_styles_path
@registered_attachments_styles_path ||= Rails.root.join('public/system/paperclip_attachments.yml').to_s @registered_attachments_styles_path ||= Rails.root.join('public/system/paperclip_attachments.yml').to_s
end end
end end
self.classes_with_attachments = Set.new
# Get list of styles saved on previous deploy (running rake paperclip:refresh:missing_styles) # Get list of styles saved on previous deploy (running rake paperclip:refresh:missing_styles)
def self.get_registered_attachments_styles def self.get_registered_attachments_styles
YAML.load_file(Paperclip.registered_attachments_styles_path) YAML.load_file(Paperclip.registered_attachments_styles_path)
...@@ -37,18 +34,15 @@ module Paperclip ...@@ -37,18 +34,15 @@ module Paperclip
# } # }
def self.current_attachments_styles def self.current_attachments_styles
Hash.new.tap do |current_styles| Hash.new.tap do |current_styles|
Paperclip.classes_with_attachments.each do |klass_name| Paperclip::Tasks::Attachments.each_definition do |klass, attachment_name, attachment_attributes|
klass = Paperclip.class_for(klass_name) # TODO: is it even possible to take into account Procs?
Paperclip::Tasks::Attachments.definitions_for(klass).each do |attachment_name, attachment_attributes| next if attachment_attributes[:styles].kind_of?(Proc)
# TODO: is it even possible to take into account Procs? attachment_attributes[:styles].try(:keys).try(:each) do |style_name|
next if attachment_attributes[:styles].kind_of?(Proc) klass_sym = klass.to_s.to_sym
attachment_attributes[:styles].try(:keys).try(:each) do |style_name| current_styles[klass_sym] ||= Hash.new
klass_sym = klass.to_s.to_sym current_styles[klass_sym][attachment_name.to_sym] ||= Array.new
current_styles[klass_sym] ||= Hash.new current_styles[klass_sym][attachment_name.to_sym] << style_name.to_sym
current_styles[klass_sym][attachment_name.to_sym] ||= Array.new current_styles[klass_sym][attachment_name.to_sym].map!(&:to_s).sort!.map!(&:to_sym).uniq!
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
end end
end end
......
...@@ -9,12 +9,20 @@ module Paperclip ...@@ -9,12 +9,20 @@ module Paperclip
instance.add(klass, attachment_name, attachment_options) instance.add(klass, attachment_name, attachment_options)
end end
def self.clear
instance.clear
end
def self.names_for(klass) def self.names_for(klass)
instance.names_for(klass) instance.names_for(klass)
end end
def self.definitions_for(klass) def self.each_definition(&block)
instance.definitions_for(klass) instance.each_definition(&block)
end
def initialize
clear
end end
def add(klass, attachment_name, attachment_options) def add(klass, attachment_name, attachment_options)
...@@ -23,12 +31,20 @@ module Paperclip ...@@ -23,12 +31,20 @@ module Paperclip
@attachments[klass][attachment_name] = attachment_options @attachments[klass][attachment_name] = attachment_options
end end
def clear
@attachments = Hash.new { |h,k| h[k] = {} }
end
def names_for(klass) def names_for(klass)
@attachments[klass].keys @attachments[klass].keys
end end
def definitions_for(klass) def each_definition
@attachments[klass] @attachments.each do |klass, attachments|
attachments.each do |name, options|
yield klass, name, options
end
end
end end
end end
end end
......
...@@ -207,70 +207,6 @@ class IntegrationTest < Test::Unit::TestCase ...@@ -207,70 +207,6 @@ class IntegrationTest < Test::Unit::TestCase
end end
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| [000,002,022].each do |umask|
context "when the umask is #{umask}" do context "when the umask is #{umask}" do
setup do setup do
......
...@@ -3,21 +3,10 @@ require './test/helper' ...@@ -3,21 +3,10 @@ require './test/helper'
class PaperclipMissingAttachmentStylesTest < Test::Unit::TestCase class PaperclipMissingAttachmentStylesTest < Test::Unit::TestCase
context "Paperclip" do context "Paperclip" do
setup do
Paperclip.classes_with_attachments = Set.new
end
teardown do teardown do
File.unlink(Paperclip.registered_attachments_styles_path) rescue nil File.unlink(Paperclip.registered_attachments_styles_path) rescue nil
end 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 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 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' Paperclip.registered_attachments_styles_path = '/tmp/config/paperclip_attachments.yml'
......
...@@ -2,6 +2,10 @@ require './test/helper' ...@@ -2,6 +2,10 @@ require './test/helper'
require 'paperclip/tasks/attachments' require 'paperclip/tasks/attachments'
class AttachmentsTest < Test::Unit::TestCase class AttachmentsTest < Test::Unit::TestCase
def setup
Paperclip::Tasks::Attachments.clear
end
context '.names_for' do context '.names_for' do
should 'include attachment names for the given class' do should 'include attachment names for the given class' do
foo = Class.new foo = Class.new
...@@ -18,21 +22,40 @@ class AttachmentsTest < Test::Unit::TestCase ...@@ -18,21 +22,40 @@ class AttachmentsTest < Test::Unit::TestCase
assert_equal [:lover], Paperclip::Tasks::Attachments.names_for(bar) assert_equal [:lover], Paperclip::Tasks::Attachments.names_for(bar)
end 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 end
context '.definitions_for' do context '.clear' do
should 'produce the attachment name and options' do should 'remove all of the existing attachment definitions' do
expected_definitions = {
avatar: { yo: 'greeting' },
greeter: { ciao: 'greeting' }
}
foo = Class.new foo = Class.new
Paperclip::Tasks::Attachments.add(foo, :avatar, { yo: 'greeting' })
Paperclip::Tasks::Attachments.add(foo, :greeter, { ciao: '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 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