Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use Paperclip without ActiveRecord
# == 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
@chip-miller

This comment has been minimized.

chip-miller commented Nov 26, 2013

Awesome work, one update: paperclip 3.5.1 requires errors.any?

  def errors
    obj = Object.new
    def obj.[](key)         [] end
    def obj.full_messages() [] end
    def obj.any?()       false end
    obj
  end 
@jgrahamthomas

This comment has been minimized.

jgrahamthomas commented Feb 19, 2014

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
@jgrahamthomas

This comment has been minimized.

jgrahamthomas commented Aug 2, 2014

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
@thedanielhanke

This comment has been minimized.

thedanielhanke commented Jul 6, 2016

👍 thanks for sharing!

@em77

This comment has been minimized.

em77 commented Jul 11, 2016

Hey @jgrahamthomas, what's the deal with the inclusion of do_not_validate_attachment_file_type? I'm using this code now and I'm noticing that if instead I try using a normal validation method I'd usually use with paperclip on an activerecord object like validates_attachment_content_typeit's giving me a NoMethodError of undefined method 'add' for #<Object:0x007f7fa5e88d80>. If I use your way above, it works, but is there any way to do validations with this? I'd like to have some degree of validation of the files being uploaded. Thanks for your contributions on this so far!

@posixpascal

This comment has been minimized.

posixpascal commented Dec 6, 2016

Why self.id = Random? Wouldn't be a timestamp + random a better solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment