Sha256: 33b74cad50acf144e26e12e7ac28b2f3ceb06ca8c8d5dae9daec52792352091e

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 KB

Contents

module Joint
  module InstanceMethods
    def grid
      @grid ||= Mongo::Grid.new(database)
    end

    private
      def assigned_attachments
        @assigned_attachments ||= {}
      end

      def nil_attachments
        @nil_attachments ||= {}
      end

      # IO must respond to read and rewind
      def save_attachments
        assigned_attachments.each_pair do |name, io|
          next unless io.respond_to?(:read)
          io.rewind if io.respond_to?(:rewind)
          grid.delete(send(name).id)
          grid.put(io, {
            :_id          => send(name).id,
            :filename     => send(name).name,
            :content_type => send(name).type,
          })
        end
        assigned_attachments.clear
      end
      
      def nullify_nil_attachments_attributes
        nil_attachments.each_key do |name|
          send(:"#{name}_id=", nil)
          send(:"#{name}_size=", nil)
          send(:"#{name}_type=", nil)
          send(:"#{name}_name=", nil)
        end
      end

      def destroy_nil_attachments
        nil_attachments.each_value do |id|
          grid.delete(id)
        end

        nil_attachments.clear
      end

      def destroy_all_attachments
        self.class.attachment_names.map { |name| grid.delete(send(name).id) }
      end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
jamieorc-joint-0.6.2 lib/joint/instance_methods.rb
joint-0.6.1 lib/joint/instance_methods.rb
jamieorc-joint-0.6.1 lib/joint/instance_methods.rb