Sha256: 76be8430647675c68756d4ee468c07a28c6e2ffc10dfc4d985309b955d7ef614

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

require 'active_support/concern'

module Mongoid::Paperclip::ImageDimension
  extend ActiveSupport::Concern
  
  included do
    class_inheritable_reader :perperclip_image_dimension_fields
    write_inheritable_attribute(: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

1 entries across 1 versions & 1 rubygems

Version Path
mongoid_paperclip_image_dimension-0.1.0 lib/mongoid_paperclip_image_dimension.rb