Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Reintroduce attachment_definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Yurek committed Jul 25, 2013
1 parent f8cf74b commit c3fd4d6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
23 changes: 18 additions & 5 deletions lib/paperclip/has_attached_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize(klass, name, options)

def define
define_flush_errors
define_getter
define_getters
define_setter
define_query
register_new_attachment
Expand All @@ -29,7 +29,12 @@ def define_flush_errors
end
end

def define_getter
def define_getters
define_instance_getter
define_class_getter
end

def define_instance_getter
name = @name
options = @options

Expand All @@ -50,24 +55,26 @@ def define_getter
end
end

def define_class_getter
@klass.extend(ClassMethods)
end

def define_setter
name = @name

@klass.send :define_method, "#{@name}=" do |file|
send(name).assign(file)
end
end

def define_query
name = @name

@klass.send :define_method, "#{@name}?" do
send(name).file?
end
end

def register_new_attachment
Paperclip::AttachmentRegistry.register(@klass, @name, @options)
Paperclip::AttachmentRegistry.register(@klass.name, @name, @options)
end

def add_active_record_callbacks
Expand All @@ -82,5 +89,11 @@ def add_paperclip_callbacks
:define_paperclip_callbacks,
:post_process, :"#{@name}_post_process")
end

module ClassMethods
def attachment_definitions
Paperclip::AttachmentRegistry.definitions_for(self.class.name)
end
end
end
end
18 changes: 17 additions & 1 deletion test/has_attached_file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class HasAttachedFileTest < Test::Unit::TestCase
assert_adding_attachment('avatar').defines_method('avatar?')
end

should 'define a method on the class to get all of its attachments' do
assert_adding_attachment('avatar').defines_class_method('attachment_definitions')
end

should 'flush errors as part of validations' do
assert_adding_attachment('avatar').defines_validation
end
Expand Down Expand Up @@ -64,6 +68,17 @@ def defines_method(method_name)
end
end

def defines_class_method(method_name)
a_class = stub_class
a_class.class.stubs(:define_method)

Paperclip::HasAttachedFile.define_on(a_class, @attachment_name, {})

assert_received(a_class, :extend) do |expect|
expect.with(Paperclip::HasAttachedFile::ClassMethods)
end
end

def defines_validation
a_class = stub_class

Expand All @@ -81,7 +96,7 @@ def registers_attachment
Paperclip::HasAttachedFile.define_on(a_class, @attachment_name, {size: 1})

assert_received(Paperclip::AttachmentRegistry, :register) do |expect|
expect.with(a_class, @attachment_name, {size: 1})
expect.with(a_class.name, @attachment_name, {size: 1})
end
end

Expand All @@ -103,6 +118,7 @@ def stub_class
before_destroy: nil,
after_destroy: nil,
define_paperclip_callbacks: nil,
extend: nil,
name: 'Billy')
end
end
Expand Down

1 comment on commit c3fd4d6

@mshibuya
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for this, since many gems(including our RailsAdmin) are relying on this feature.

Please sign in to comment.