Commit b62cc2d2 by Jon Yurek

Change Paperclip::Tasks::Attachment to Paperclip::AttachmentRegistry

parent 65cc5c7d
When /^I attach the file "([^"]*)" to "([^"]*)" on S3$/ do |file_path, field| When /^I attach the file "([^"]*)" to "([^"]*)" on S3$/ do |file_path, field|
definition = Paperclip::Tasks::Attachments.definitions_for(User)[field.downcase.to_sym] definition = Paperclip::AttachmentRegistry.definitions_for(User)[field.downcase.to_sym]
path = "https://paperclip.s3.amazonaws.com#{definition[:path]}" path = "https://paperclip.s3.amazonaws.com#{definition[:path]}"
path.gsub!(':filename', File.basename(file_path)) path.gsub!(':filename', File.basename(file_path))
path.gsub!(/:([^\/\.]+)/) do |match| path.gsub!(/:([^\/\.]+)/) do |match|
......
...@@ -51,7 +51,7 @@ require 'paperclip/validators' ...@@ -51,7 +51,7 @@ require 'paperclip/validators'
require 'paperclip/logger' require 'paperclip/logger'
require 'paperclip/helpers' require 'paperclip/helpers'
require 'paperclip/has_attached_file' require 'paperclip/has_attached_file'
require 'paperclip/tasks/attachments' require 'paperclip/attachment_registry'
require 'paperclip/filename_cleaner' require 'paperclip/filename_cleaner'
require 'mime/types' require 'mime/types'
require 'logger' require 'logger'
......
require 'singleton' require 'singleton'
module Paperclip module Paperclip
module Tasks class AttachmentRegistry
class Attachments
include Singleton include Singleton
def self.add(klass, attachment_name, attachment_options) def self.register(klass, attachment_name, attachment_options)
instance.add(klass, attachment_name, attachment_options) instance.register(klass, attachment_name, attachment_options)
end end
def self.clear def self.clear
...@@ -29,7 +28,7 @@ module Paperclip ...@@ -29,7 +28,7 @@ module Paperclip
clear clear
end end
def add(klass, attachment_name, attachment_options) def register(klass, attachment_name, attachment_options)
@attachments ||= {} @attachments ||= {}
@attachments[klass] ||= {} @attachments[klass] ||= {}
@attachments[klass][attachment_name] = attachment_options @attachments[klass][attachment_name] = attachment_options
...@@ -55,5 +54,4 @@ module Paperclip ...@@ -55,5 +54,4 @@ module Paperclip
@attachments[klass] @attachments[klass]
end end
end end
end
end end
...@@ -15,7 +15,7 @@ module Paperclip ...@@ -15,7 +15,7 @@ module Paperclip
define_getter define_getter
define_setter define_setter
define_query define_query
register_with_rake_tasks register_new_attachment
add_active_record_callbacks add_active_record_callbacks
add_paperclip_callbacks add_paperclip_callbacks
end end
...@@ -66,8 +66,8 @@ module Paperclip ...@@ -66,8 +66,8 @@ module Paperclip
end end
end end
def register_with_rake_tasks def register_new_attachment
Paperclip::Tasks::Attachments.add(@klass, @name, @options) Paperclip::AttachmentRegistry.register(@klass, @name, @options)
end end
def add_active_record_callbacks def add_active_record_callbacks
......
require 'paperclip/tasks/attachments' require 'paperclip/attachment_registry'
require 'set' require 'set'
module Paperclip module Paperclip
...@@ -34,7 +34,7 @@ module Paperclip ...@@ -34,7 +34,7 @@ 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::Tasks::Attachments.each_definition do |klass, attachment_name, attachment_attributes| Paperclip::AttachmentRegistry.each_definition do |klass, attachment_name, attachment_attributes|
# TODO: is it even possible to take into account Procs? # TODO: is it even possible to take into account Procs?
next if attachment_attributes[:styles].kind_of?(Proc) next if attachment_attributes[:styles].kind_of?(Proc)
attachment_attributes[:styles].try(:keys).try(:each) do |style_name| attachment_attributes[:styles].try(:keys).try(:each) do |style_name|
......
require 'paperclip/tasks/attachments' require 'paperclip/attachment_registry'
module Paperclip module Paperclip
module Task module Task
...@@ -12,7 +12,7 @@ module Paperclip ...@@ -12,7 +12,7 @@ module Paperclip
klass = Paperclip.class_for(klass.to_s) klass = Paperclip.class_for(klass.to_s)
name = ENV['ATTACHMENT'] || ENV['attachment'] name = ENV['ATTACHMENT'] || ENV['attachment']
attachment_names = Paperclip::Tasks::Attachments.names_for(klass) attachment_names = Paperclip::AttachmentRegistry.names_for(klass)
if attachment_names.empty? if attachment_names.empty?
raise "Class #{klass.name} has no attachments specified" raise "Class #{klass.name} has no attachments specified"
......
...@@ -52,4 +52,5 @@ Gem::Specification.new do |s| ...@@ -52,4 +52,5 @@ Gem::Specification.new do |s|
s.add_development_dependency('fakeweb') s.add_development_dependency('fakeweb')
s.add_development_dependency('railties') s.add_development_dependency('railties')
s.add_development_dependency('actionmailer', '>= 3.0.0') s.add_development_dependency('actionmailer', '>= 3.0.0')
s.add_development_dependency('protected_attributes')
end end
require './test/helper' require './test/helper'
require 'paperclip/tasks/attachments' require 'paperclip/attachment_registry'
class AttachmentsTest < Test::Unit::TestCase class AttachmentRegistryTest < Test::Unit::TestCase
def setup def setup
Paperclip::Tasks::Attachments.clear Paperclip::AttachmentRegistry.clear
end 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
Paperclip::Tasks::Attachments.add(foo, :avatar, {}) Paperclip::AttachmentRegistry.register(foo, :avatar, {})
assert_equal [:avatar], Paperclip::Tasks::Attachments.names_for(foo) assert_equal [:avatar], Paperclip::AttachmentRegistry.names_for(foo)
end end
should 'not include attachment names for other classes' do should 'not include attachment names for other classes' do
foo = Class.new foo = Class.new
bar = Class.new bar = Class.new
Paperclip::Tasks::Attachments.add(foo, :avatar, {}) Paperclip::AttachmentRegistry.register(foo, :avatar, {})
Paperclip::Tasks::Attachments.add(bar, :lover, {}) Paperclip::AttachmentRegistry.register(bar, :lover, {})
assert_equal [:lover], Paperclip::Tasks::Attachments.names_for(bar) assert_equal [:lover], Paperclip::AttachmentRegistry.names_for(bar)
end end
should 'produce the empty array for a missing key' do should 'produce the empty array for a missing key' do
assert_empty Paperclip::Tasks::Attachments.names_for(Class.new) assert_empty Paperclip::AttachmentRegistry.names_for(Class.new)
end end
end end
...@@ -36,11 +36,11 @@ class AttachmentsTest < Test::Unit::TestCase ...@@ -36,11 +36,11 @@ class AttachmentsTest < Test::Unit::TestCase
[foo, :greeter, { ciao: 'greeting' }] [foo, :greeter, { ciao: 'greeting' }]
] ]
expected_accumulations.each do |args| expected_accumulations.each do |args|
Paperclip::Tasks::Attachments.add(*args) Paperclip::AttachmentRegistry.register(*args)
end end
accumulations = [] accumulations = []
Paperclip::Tasks::Attachments.each_definition do |*args| Paperclip::AttachmentRegistry.each_definition do |*args|
accumulations << args accumulations << args
end end
...@@ -55,10 +55,10 @@ class AttachmentsTest < Test::Unit::TestCase ...@@ -55,10 +55,10 @@ class AttachmentsTest < Test::Unit::TestCase
greeter: { ciao: 'greeting' } greeter: { ciao: 'greeting' }
} }
foo = Class.new foo = Class.new
Paperclip::Tasks::Attachments.add(foo, :avatar, { yo: 'greeting' }) Paperclip::AttachmentRegistry.register(foo, :avatar, { yo: 'greeting' })
Paperclip::Tasks::Attachments.add(foo, :greeter, { ciao: 'greeting' }) Paperclip::AttachmentRegistry.register(foo, :greeter, { ciao: 'greeting' })
definitions = Paperclip::Tasks::Attachments.definitions_for(foo) definitions = Paperclip::AttachmentRegistry.definitions_for(foo)
assert_equal expected_definitions, definitions assert_equal expected_definitions, definitions
end end
...@@ -67,11 +67,11 @@ class AttachmentsTest < Test::Unit::TestCase ...@@ -67,11 +67,11 @@ class AttachmentsTest < Test::Unit::TestCase
context '.clear' do context '.clear' do
should 'remove all of the existing attachment definitions' do should 'remove all of the existing attachment definitions' do
foo = Class.new foo = Class.new
Paperclip::Tasks::Attachments.add(foo, :greeter, { ciao: 'greeting' }) Paperclip::AttachmentRegistry.register(foo, :greeter, { ciao: 'greeting' })
Paperclip::Tasks::Attachments.clear Paperclip::AttachmentRegistry.clear
assert_empty Paperclip::Tasks::Attachments.names_for(foo) assert_empty Paperclip::AttachmentRegistry.names_for(foo)
end end
end end
end end
...@@ -201,7 +201,10 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -201,7 +201,10 @@ class AttachmentTest < Test::Unit::TestCase
dummy = Dummy.new dummy = Dummy.new
dummy.id = 1234 dummy.id = 1234
dummy.avatar_file_name = "fake.jpg" dummy.avatar_file_name = "fake.jpg"
expected_string = '{"dummy":{"avatar":"/system/dummies/avatars/000/001/234/original/fake.jpg"}}' expected_string = '{"avatar":"/system/dummies/avatars/000/001/234/original/fake.jpg"}'
if ActiveRecord::Base.include_root_in_json # This is true by default in Rails 3, and false in 4
expected_string = %({"dummy":#{expected_string}})
end
# active_model pre-3.2 checks only by calling any? on it, thus it doesn't work if it is empty # active_model pre-3.2 checks only by calling any? on it, thus it doesn't work if it is empty
assert_equal expected_string, dummy.to_json(:only => [:dummy_key_for_old_active_model], :methods => [:avatar]) assert_equal expected_string, dummy.to_json(:only => [:dummy_key_for_old_active_model], :methods => [:avatar])
end end
......
...@@ -19,8 +19,8 @@ class HasAttachedFileTest < Test::Unit::TestCase ...@@ -19,8 +19,8 @@ class HasAttachedFileTest < Test::Unit::TestCase
assert_adding_attachment('avatar').defines_validation assert_adding_attachment('avatar').defines_validation
end end
should 'register the attachment with Paperclip::Tasks' do should 'register the attachment with Paperclip::AttachmentRegistry' do
assert_adding_attachment('avatar').registers_with_tasks assert_adding_attachment('avatar').registers_attachment
end end
should 'define an after_save callback' do should 'define an after_save callback' do
...@@ -74,13 +74,13 @@ class HasAttachedFileTest < Test::Unit::TestCase ...@@ -74,13 +74,13 @@ class HasAttachedFileTest < Test::Unit::TestCase
end end
end end
def registers_with_tasks def registers_attachment
a_class = stub_class a_class = stub_class
Paperclip::Tasks::Attachments.stubs(:add) Paperclip::AttachmentRegistry.stubs(:register)
Paperclip::HasAttachedFile.define_on(a_class, @attachment_name, {size: 1}) Paperclip::HasAttachedFile.define_on(a_class, @attachment_name, {size: 1})
assert_received(Paperclip::Tasks::Attachments, :add) do |expect| assert_received(Paperclip::AttachmentRegistry, :register) do |expect|
expect.with(a_class, @attachment_name, {size: 1}) expect.with(a_class, @attachment_name, {size: 1})
end end
end end
......
...@@ -2,19 +2,19 @@ require 'rubygems' ...@@ -2,19 +2,19 @@ require 'rubygems'
require 'tempfile' require 'tempfile'
require 'pathname' require 'pathname'
require 'test/unit' require 'test/unit'
require 'shoulda'
require 'mocha/setup'
require 'bourne'
require 'active_record' require 'active_record'
require 'active_record/version' require 'active_record/version'
require 'active_support' require 'active_support'
require 'active_support/core_ext' require 'active_support/core_ext'
require 'shoulda'
require 'mocha/setup'
require 'bourne'
require 'shoulda/context'
require 'mime/types' require 'mime/types'
require 'pathname' require 'pathname'
require 'ostruct' require 'ostruct'
require 'pry' require 'pry'
require 'protected_attributes'
puts "Testing against version #{ActiveRecord::VERSION::STRING}" puts "Testing against version #{ActiveRecord::VERSION::STRING}"
...@@ -48,7 +48,7 @@ require './shoulda_macros/paperclip' ...@@ -48,7 +48,7 @@ require './shoulda_macros/paperclip'
FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures") FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures")
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml')) config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new(File.dirname(__FILE__) + "/debug.log") ActiveRecord::Base.logger = ActiveSupport::Logger.new(File.dirname(__FILE__) + "/debug.log")
ActiveRecord::Base.establish_connection(config['test']) ActiveRecord::Base.establish_connection(config['test'])
Paperclip.options[:logger] = ActiveRecord::Base.logger Paperclip.options[:logger] = ActiveRecord::Base.logger
......
...@@ -2,12 +2,12 @@ require './test/helper' ...@@ -2,12 +2,12 @@ require './test/helper'
class PaperclipMissingAttachmentStylesTest < Test::Unit::TestCase class PaperclipMissingAttachmentStylesTest < Test::Unit::TestCase
def setup def setup
Paperclip::Tasks::Attachments.clear Paperclip::AttachmentRegistry.clear
end end
context "Paperclip" do context "Paperclip" do
setup do setup do
Paperclip::Tasks::Attachments.clear Paperclip::AttachmentRegistry.clear
end end
teardown do teardown do
......
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