# == Paperclip without ActiveRecord | |
# | |
# Simple and lightweight object that can use Paperclip | |
# | |
# | |
# Customized part can be extracted in another class which | |
# would inherit from SimplePaperclip. | |
# | |
# class MyClass < SimplePaperclip | |
# attr_accessor :image_file_name # :<atached_file_name>_file_name | |
# has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" } | |
# end | |
# | |
# author : Bastien Gysler <basgys@gmail.com> | |
class SimplePaperclip | |
extend ActiveModel::Naming | |
extend ActiveModel::Callbacks | |
include ActiveModel::Validations | |
include Paperclip::Glue | |
# Paperclip required callbacks | |
define_model_callbacks :save, only: [:after] | |
define_model_callbacks :destroy, only: [:before, :after] | |
# Paperclip attached file example | |
# -- Customize here -- | |
attr_accessor :image_file_name # :<atached_file_name>_file_name | |
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" } | |
# -- /Customize here -- | |
# ActiveModel requirements | |
def to_model | |
self | |
end | |
def valid?() true end | |
def new_record?() true end | |
def destroyed?() true end | |
def errors | |
obj = Object.new | |
def obj.[](key) [] end | |
def obj.full_messages() [] end | |
obj | |
end | |
end |
This comment has been minimized.
This comment has been minimized.
Made some slight updates to get things to jive with Rails 4. Note with the example above, the 'save' method wasn't defined, nor were the callbacks being invoked. class PublishableItemImage
extend ActiveModel::Callbacks
include ActiveModel::Model
include Paperclip::Glue
# Paperclip required callbacks
define_model_callbacks :save, only: [:after]
define_model_callbacks :destroy, only: [:before, :after]
attr_accessor :photo_file_name, :photo_content_type, :photo_file_size, :photo_updated_at, :id
has_attached_file :photo, :styles => { :thumb => ["100x100#", :png], :small => ["200x200#", :png], :cover => ["1300x528#", :png] }, :processors => [:auto_orient, :thumbnail]
def save
run_callbacks :save do
self.id = 1000 + Random.rand(9000)
end
return true
end
def destroy
run_callbacks :destroy
end
def updated_at_short
return Time.now.to_s(:autosave_time)
end
def errors
obj = Object.new
def obj.[](key) [] end
def obj.full_messages() [] end
def obj.any?() false end
obj
end
end |
This comment has been minimized.
This comment has been minimized.
Had to make a slight update since upgrading to Paperclip 4.1.1. Note the inclusion of "do_not_validate_attachment_file_type": # This class behaves like an ActiveRecord object so we can take full
# advantage of paperclip. But it ain't a real AR object! Nice piece
# of trickery here.
class PublishableItemImage
extend ActiveModel::Callbacks
include ActiveModel::Model
include Paperclip::Glue
# Paperclip required callbacks
define_model_callbacks :save, only: [:after]
define_model_callbacks :commit, only: [:after]
define_model_callbacks :destroy, only: [:before, :after]
attr_accessor :photo_file_name, :photo_content_type, :photo_file_size, :photo_updated_at, :id
has_attached_file :photo, :styles => { :thumb => ["100x100#", :png], :small => ["200x200>", :png], :cover => ["1300x528>", :png] }, :processors => [:auto_orient, :thumbnail]
do_not_validate_attachment_file_type :photo
def save
run_callbacks :save do
self.id = 1000 + Random.rand(9000)
end
return true
end
def destroy
run_callbacks :destroy
end
def updated_at_short
return Time.now.to_s(:autosave_time)
end
def errors
obj = Object.new
def obj.[](key) [] end
def obj.full_messages() [] end
def obj.any?() false end
obj
end
end |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
Hey @jgrahamthomas, what's the deal with the inclusion of |
This comment has been minimized.
This comment has been minimized.
Why self.id = Random? Wouldn't be a timestamp + random a better solution? |
This comment has been minimized.
Awesome work, one update: paperclip 3.5.1 requires
errors.any?