initial code with basic option/validation tests

git-svn-id: http://svn.techno-weenie.net/projects/plugins/attachment_fu@2551 567b1171-46fb-0310-a4c9-b4bef9110e78
This commit is contained in:
technoweenie 2006-12-14 02:41:24 +00:00
Родитель 7a14bcb9cd
Коммит 54b14490e0
2 изменённых файлов: 62 добавлений и 44 удалений

Просмотреть файл

@ -76,6 +76,11 @@ module Technoweenie # :nodoc:
validate :attachment_attributes_valid?
end
# Returns true or false if the given content type is recognized as an image.
def image?(content_type)
content_types.include?(content_type)
end
# Callback after an attachment has been saved either to the file system or the DB.
# Only called if the file has been changed, not necessarily if the record is updated.
#
@ -91,6 +96,19 @@ module Technoweenie # :nodoc:
end
module InstanceMethods
# Checks whether the attachment's content type is an image content type
def image?
self.class.image?(content_type)
end
def thumbnailable?
image? && respond_to?(:parent_id)
end
def thumbnail_class
self.class.thumbnail_class
end
# Gets the thumbnail name for a filename. 'foo.jpg' becomes 'foo_thumbnail.jpg'
def thumbnail_name_for(thumbnail = nil)
return filename if thumbnail.blank?

88
test/fixtures/attachment.rb поставляемый
Просмотреть файл

@ -32,53 +32,53 @@ class ImageOrPdfAttachment < Attachment
has_attachment :content_type => ['pdf', :image], :resize_to => 'x50'
end
#class ImageWithThumbsAttachment < Attachment
# has_attachment :thumbnails => { :thumb => [50, 50], :geometry => 'x50' }, :resize_to => [55,55]
# after_resize do |record, img|
# record.aspect_ratio = img.columns.to_f / img.rows.to_f
# end
#end
class ImageWithThumbsAttachment < Attachment
has_attachment :thumbnails => { :thumb => [50, 50], :geometry => 'x50' }, :resize_to => [55,55]
#after_resize do |record, img|
# record.aspect_ratio = img.columns.to_f / img.rows.to_f
#end
end
class FileAttachment < ActiveRecord::Base
has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files'
validates_as_attachment
end
#class ImageFileAttachment < FileAttachment
# has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files',
# :content_type => :image, :resize_to => [50,50]
#end
#
#class ImageWithThumbsFileAttachment < FileAttachment
# has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files',
# :thumbnails => { :thumb => [50, 50], :geometry => 'x50' }, :resize_to => [55,55]
# after_resize do |record, img|
# record.aspect_ratio = img.columns.to_f / img.rows.to_f
# end
#end
#
#class ImageWithThumbsClassFileAttachment < FileAttachment
# has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files',
# :thumbnails => { :thumb => [50, 50] }, :resize_to => [55,55],
# :thumbnail_class => 'ImageThumbnail'
#end
#
#class ImageThumbnail < FileAttachment
# has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files/thumbnails'
#end
#
## no parent
#class OrphanAttachment < ActiveRecord::Base
# has_attachment
# validates_as_attachment
#end
#
## no filename, no size, no content_type
#class MinimalAttachment < ActiveRecord::Base
# has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files'
# validates_as_attachment
#
# def filename
# "#{id}.file"
# end
#end
class ImageFileAttachment < FileAttachment
has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files',
:content_type => :image, :resize_to => [50,50]
end
class ImageWithThumbsFileAttachment < FileAttachment
has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files',
:thumbnails => { :thumb => [50, 50], :geometry => 'x50' }, :resize_to => [55,55]
#after_resize do |record, img|
# record.aspect_ratio = img.columns.to_f / img.rows.to_f
#end
end
class ImageWithThumbsClassFileAttachment < FileAttachment
has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files',
:thumbnails => { :thumb => [50, 50] }, :resize_to => [55,55],
:thumbnail_class => 'ImageThumbnail'
end
class ImageThumbnail < FileAttachment
has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files/thumbnails'
end
# no parent
class OrphanAttachment < ActiveRecord::Base
has_attachment
validates_as_attachment
end
# no filename, no size, no content_type
class MinimalAttachment < ActiveRecord::Base
has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files'
validates_as_attachment
def filename
"#{id}.file"
end
end