Sha256: c6fd21a5fe6b8cd61ef6fd88500c94b0b763e944eba95287d994d449ca6d48a2

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

module CarrierWave
  module Meta
    extend ActiveSupport::Concern

    included do
      include CarrierWave::ModelDelegateAttribute
      include CarrierWave::MimeTypes

      set_content_type(true)
      
      after :retrieve_from_cache, :set_content_type
      after :retrieve_from_cache, :call_store_meta
      after :retrieve_from_store, :set_content_type
      after :retrieve_from_store, :call_store_meta

      model_delegate_attribute :content_type, ''
      model_delegate_attribute :file_size, 0
      model_delegate_attribute :image_size, []
      model_delegate_attribute :width, 0
      model_delegate_attribute :height, 0
    end

    module InstanceMethods
      def store_meta
        if self.file.present?
          dimensions = get_dimensions
          width, height = dimensions
          self.content_type = self.file.content_type
          self.file_size = self.file.size
          self.image_size = dimensions
          self.width = width
          self.height = height
        end
      end
      
      def set_content_type(file = nil)
        set_content_type(true)
      end
      
      private
      def call_store_meta(file = nil)
        # Re-retrieve metadata for a file only if model is not present.
        store_meta if self.model.nil?
      end
      
      def get_dimensions
        [].tap do |size|
          if self.file.content_type =~ /image/
            manipulate! do |img|
              if img.is_a?(::Magick::Image)
                size << img.columns
                size << img.rows
              else
                raise "Only RMagick is supported yet. Fork and update it."
              end        
              img
            end
          end
        end
      end        
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
carrierwave-meta-0.0.1 lib/carrierwave-meta/meta.rb