Sha256: a81bbf64c1006653867085433306e43bcf8c0dcb77da5ebd532eeb918e899f14

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

# encoding: utf-8

require "carrierwave/orm/activerecord"

module CarrierWave
  module ActiveRecord
    module Serializable
      def serialized_uploaders
        @serialized_uploaders ||= {}
      end
    
      def serialized_uploader?(column)
        serialized_uploaders.key?(column) && serialized_attributes.key?(serialized_uploaders[column].to_s)
      end
      
      ##
      # See +CarrierWave::Mount#mount_uploader+ for documentation
      #
      def mount_uploader(column, uploader=nil, options={}, &block)
        super
        
        serialize_to = options.delete :serialize_to
        if serialize_to
          serialized_uploaders[column] = serialize_to 
          class_eval <<-RUBY, __FILE__, __LINE__+1
            def #{column}_will_change!
              #{serialize_to}_will_change!
              @#{column}_changed = true
            end

            def #{column}_changed?
              @#{column}_changed
            end
          RUBY
        end
        
        class_eval <<-RUBY, __FILE__, __LINE__+1
          def write_uploader(column, identifier)
            if self.class.serialized_uploader?(column)
              serialized_field = self.send self.class.serialized_uploaders[column]
              serialized_field[column.to_s] = identifier
            else
              super
            end
          end

          def read_uploader(column)
            if self.class.serialized_uploader?(column)
              serialized_field = self.send self.class.serialized_uploaders[column]
              serialized_field[column.to_s]
            else
              super
            end
          end
        RUBY
      end
    end # Serializable
  end # ActiveRecord
end # CarrierWave

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
carrierwave-serializable-0.0.2 lib/carrierwave-serializable/orm/activerecord.rb
carrierwave-serializable-0.0.1 lib/carrierwave-serializable/orm/activerecord.rb