Sha256: 042e22323c3f858c6aa7a7c485f1a1456ded1fe20fd1da03ab8cff754acb5626

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

require 'paperclip'

module Paperclip
  module Dimension
    def self.included(klass)
      klass.extend Paperclip::Dimension::ClassMethods
    end

    module ClassMethods
      # override has_attached_file to:
      # 1). save dimensions on post process
      # 2). create dimension accessors
      def has_attached_file name, options={}
        super

        class_eval <<-END
          # for ActiveRecord
          serialize :#{name}_dimensions, Hash if respond_to?(:serialize)

          def #{name}_dimension(style=:original)
            self.#{name}_dimensions[style]
          end

          def #{name}_dimension_str(style=:original)
             dim = #{name}_dimension(style)
             dim ? dim.join('x') : nil
          end
        END

        send "after_#{name}_post_process", lambda { save_dimensions_for(name) }
      end
    end

    def save_dimensions_for(name)
      opts = self.class.attachment_definitions[name]

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

  module Glue
    class << self
      def included_with_dimension(klass)
        included_without_dimension(klass)
        klass.send :include, Paperclip::Dimension
      end

      alias :included_without_dimension :included
      alias :included :included_with_dimension
    end
  end

  module Schema
    @@columns[:dimensions] = :string
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
paperclip-dimension-0.1.0 lib/paperclip-dimension.rb