Skip to content

Commit

Permalink
Move validates_each into HasAttachedFile class
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-burns committed Jan 25, 2013
1 parent bd8c08c commit 40337d1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
5 changes: 0 additions & 5 deletions lib/paperclip.rb
Expand Up @@ -186,11 +186,6 @@ def has_attached_file(name, options = {})
after_destroy { send(name).send(:flush_deletes) }

define_paperclip_callbacks :post_process, :"#{name}_post_process"

validates_each(name) do |record, attr, value|
attachment = record.send(name)
attachment.send(:flush_errors)
end
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions lib/paperclip/has_attached_file.rb
Expand Up @@ -11,13 +11,21 @@ def initialize(klass, name, options)
end

def define
define_flush_errors
define_getter
define_setter
define_query
end

private

def define_flush_errors
@klass.send(:validates_each, @name) do |record, attr, value|
attachment = record.send(@name)
attachment.send(:flush_errors)
end
end

def define_getter
name = @name
options = @options
Expand Down
22 changes: 21 additions & 1 deletion test/has_attached_file_test.rb
Expand Up @@ -14,6 +14,10 @@ class HasAttachedFileTest < Test::Unit::TestCase
should 'define a query on the class object' do
assert_adding_attachment('avatar').defines_method('avatar?')
end

should 'flush errors as part of validations' do
assert_adding_attachment('avatar').defines_validation
end
end

private
Expand All @@ -31,13 +35,29 @@ def initialize(attachment_name)
end

def defines_method(method_name)
a_class = stub('class', define_method: nil)
a_class = stub_class

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

assert_received(a_class, :define_method) do |expect|
expect.with(method_name)
end
end

def defines_validation
a_class = stub_class

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

assert_received(a_class, :validates_each) do |expect|
expect.with(@attachment_name)
end
end

private

def stub_class
stub('class', validates_each: nil, define_method: nil)
end
end
end

0 comments on commit 40337d1

Please sign in to comment.