Sha256: 0c12f8169fc5a0de97339d7cb0cf07af80292c5bc58f254581564ab854027fe5

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 KB

Contents

require 'active_support/concern'

module Mongoid::Paperclip::ImageDimension
  extend ActiveSupport::Concern

  included do
    class_attribute :perperclip_image_dimension_fields
    self.perperclip_image_dimension_fields = []
  end

  module ClassMethods
    def save_image_dimensions_on(*args)
      args.flatten.each do |attachment_field|
        attachment_field = attachment_field.to_sym
        dimension_field_name = "#{attachment_field}_dimensions".to_sym
        perperclip_image_dimension_fields << attachment_field
        field dimension_field_name, :type => Hash

        class_eval <<-END
          def #{attachment_field}_dimension(style=:original)
            #{attachment_field}_dimensions[style.to_s].map(&:to_i)
          end

          def #{attachment_field}_dimension_str(style=:original)
            #{attachment_field}_dimension(style).join('x')
          end

          after_#{attachment_field}_post_process do
            save_dimensions_for(:"#{attachment_field}")
          end
        END

      end
    end
  end

  def save_dimensions_for(attachment_field)
    styles = self.class.attachment_definitions[attachment_field][:styles].keys + [:original]
    dimension_hash = {}
    styles.each do |style|
      attachment = self.send attachment_field
      geo = Paperclip::Geometry.from_file(attachment.queued_for_write[style])
      dimension_hash[style] = [ geo.width.to_i, geo.height.to_i ]
    end
    self.send "#{attachment_field}_dimensions=", dimension_hash
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongoid_paperclip_image_dimension-0.1.2 lib/mongoid_paperclip_image_dimension.rb
mongoid_paperclip_image_dimension-0.1.1 lib/mongoid_paperclip_image_dimension.rb