Sha256: ba0c04392512140519352b1c984c47e76361f985ecefa3ff138c304f65d091e4

Contents?: true

Size: 1.69 KB

Versions: 5

Compression:

Stored size: 1.69 KB

Contents

class AttachmentImage
  include Mongoid::Document

  include SimpleFormAttachments::Attachment

  extend Dragonfly::Model
  extend Dragonfly::Model::Validations

  # ---------------------------------------------------------------------

  field :file_ext, type: String
  field :file_format, type: Symbol
  field :file_mime_type, type: String
  field :file_name, type: String
  field :file_size, type: Integer
  field :file_uid, type: String
  field :file_aspect_ratio, type: Float
  field :file_height, type: Integer
  field :file_width, type: Integer

  field :caption

  # ---------------------------------------------------------------------

  dragonfly_accessor :file

  # ---------------------------------------------------------------------

  # validates :file, presence: true

  validates_size_of :file, maximum: 500.kilobytes, message: 'is too big, should be maximum 500kb'

  # ---------------------------------------------------------------------

  # validates_property :ext, of: :file, in: ['jpg']
  # validates_property :format, of: :file, in: ['jpeg', 'gif']
  # validates_property :mime_type, of: :file, as: 'image/jpeg'

  # =====================================================================

  def thumb_url dimensions="100x100#", jpg_quality=75
    return unless file
    return file.url if (file.ext.present? && file.ext.to_sym == :svg)

    file.thumb(dimensions).url
  end

  # ---------------------------------------------------------------------

  # Append thumbnail
  #
  # @return [Hash]
  def as_json options={}
    super(options).merge(
      {
        thumb_url: thumb_url,
        caption: caption
      }
    )
  end

  # ---------------------------------------------------------------------

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
simple_form_attachments-1.2.0 test/dummy/app/models/attachment_image.rb
simple_form_attachments-1.1.1 test/dummy/app/models/attachment_image.rb
simple_form_attachments-1.1.0 test/dummy/app/models/attachment_image.rb
simple_form_attachments-1.0.1 test/dummy/app/models/attachment_image.rb
simple_form_attachments-1.0.0 test/dummy/app/models/attachment_image.rb