Module: SimplyCouch::HasAttachment

Defined in:
lib/simply_couch/has_attachment.rb

Overview

Drop-in replacement for Paperclip in SimplyCouch/CouchDB models.

Usage (replaces ‘include Paperclip::Glue` + `has_attached_file`):

class Image
  include SimplyCouch::Model
  include SimplyCouch::HasAttachment

  has_attachment :file, styles: {
    medium: "354x1000>",
    thumb:  "160x1250>"
  }

  # Validations (standard Rails, replaces validates_attachment)
  validate :file_content_type_must_be_image

  private

  def file_content_type_must_be_image
    return if file.blank?
    unless %w[image/jpeg image/gif image/png].include?(file_content_type)
      errors.add(:file, :invalid_content_type)
    end
  end
end

The module auto-declares CouchDB properties for:

<name>_file_name, <name>_content_type, <name>_file_size, <name>_updated_at

Files are stored on disk at:

public/system/<attachment>/<id>/original.<ext>
public/system/<attachment>/<id>/<style>.<ext>

Uses MiniMagick (ImageMagick) for thumbnail generation. Same geometry syntax as Paperclip: “300x300>”, “160x1250>”, etc.

Defined Under Namespace

Modules: ClassMethods Classes: Proxy

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



40
41
42
# File 'lib/simply_couch/has_attachment.rb', line 40

def self.included(base)
  base.extend(ClassMethods)
end