Commit 5fbb8ac8 by jyurek

Raises if there are any problems with required columns.

git-svn-id: https://svn.thoughtbot.com/plugins/paperclip/trunk@206 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
parent 62a6682a
......@@ -162,6 +162,7 @@ module Thoughtbot #:nodoc:
attachment_names.each do |attr|
attachments[attr] = (attachments[attr] || {:name => attr}).merge(options)
whine_about_columns_for attachments[attr]
define_method "#{attr}=" do |uploaded_file|
uploaded_file = fetch_uri(uploaded_file) if uploaded_file.is_a? URI
......@@ -271,6 +272,15 @@ module Thoughtbot #:nodoc:
r.errors.add(a, "requires a valid attachment.") unless r.send("#{a}_valid?")
end
end
def whine_about_columns_for attachment #:nodoc:
name = attachment[:name]
unless column_names.include?("#{name}_file_name") && column_names.include?("#{name}_content_type")
error = "Class #{self.name} does not have the necessary columns to have an attachment named #{name}. " +
"(#{name}_file_name and #{name}_content_type)"
raise PaperclipError.new(attachment), error
end
end
end
module InstanceMethods #:nodoc:
......@@ -336,7 +346,7 @@ module Thoughtbot #:nodoc:
begin
FileUtils.rm file_path if file_path
rescue SystemCallError => e
raise PaperclipError(attachment[:name], e.message, e) if ::Thoughtbot::Paperclip.options[:whiny_deletes] || complain
raise PaperclipError.new(attachment), "Could not delete thumbnail." if ::Thoughtbot::Paperclip.options[:whiny_deletes] || complain
end
end
end
......
namespace :paperclip do
# desc "Regenerates thumbnails for a given CLASS (and optional ATTACHMENT)"
# task :refresh do
# end
#
# desc "Cleans out unused attachments for the given CLASS (and optional ATTACHMENT)"
# task :clean do
# end
end
\ No newline at end of file
begin
ActiveRecord::Base.connection.create_table :foos do |table|
ActiveRecord::Base.connection.create_table :foos, :force => true do |table|
table.column :image_file_name, :string
table.column :image_content_type, :string
end
ActiveRecord::Base.connection.create_table :bars do |table|
ActiveRecord::Base.connection.create_table :bars, :force => true do |table|
table.column :document_file_name, :string
table.column :document_content_type, :string
end
ActiveRecord::Base.connection.create_table :non_standards do |table|
ActiveRecord::Base.connection.create_table :non_standards, :force => true do |table|
table.column :resume_file_name, :string
table.column :resume_content_type, :string
table.column :avatar_file_name, :string
table.column :avatar_content_type, :string
end
ActiveRecord::Base.connection.create_table :negatives, :force => true do |table|
table.column :this_is_the_wrong_name_file_name, :string
end
rescue Exception
end
......@@ -43,3 +46,6 @@ class NonStandard < ActiveRecord::Base
:default_style => :square,
:missing_url => "/:class/:style/:attachment/404.png"
end
class Negative < ActiveRecord::Base
end
\ No newline at end of file
......@@ -50,4 +50,10 @@ class PaperclipTest < Test::Unit::TestCase
assert_match /requires a valid/, @bar.errors.on(:document), @bar.errors.on(:document)
end
def test_should_raise_if_table_missing_columns
assert_raises Thoughtbot::Paperclip::PaperclipError do
Negative.send(:has_attached_file, :missing)
end
end
end
\ No newline at end of file
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