Sha256: f730b5c5707d134d1f54b438bcf2305408526c45648e8f2b662c33510d28f42e

Contents?: true

Size: 1.07 KB

Versions: 8

Compression:

Stored size: 1.07 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  module State #:nodoc:
    extend ActiveSupport::Concern
    included do
      attr_reader :new_record
    end

    module InstanceMethods
      # Returns true if the +Document+ has not been persisted to the database,
      # false if it has. This is determined by the variable @new_record
      # and NOT if the object has an id.
      def new_record?
        !!@new_record
      end

      # Sets the new_record boolean - used after document is saved.
      def new_record=(saved)
        @new_record = saved
      end

      # Checks if the document has been saved to the database.
      def persisted?
        !new_record?
      end

      # Returns true if the +Document+ has been succesfully destroyed, and false if it hasn't.
      # This is determined by the variable @destroyed and NOT by checking the database.
      def destroyed?
        @destroyed == true
      end

      # Sets the destroyed boolean - used after document is destroyed.
      def destroyed=(destroyed)
        @destroyed = destroyed && true
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 4 rubygems

Version Path
mongoid-1.9.5 lib/mongoid/state.rb
mongoid-with-auth-1.9.4 lib/mongoid/state.rb
mongoid-rails2-1.9.4 lib/mongoid/state.rb
mongoid-rails2-1.9.3 lib/mongoid/state.rb
mongoid-1.9.2 lib/mongoid/state.rb
sskirby-mongoid-1.9.1 lib/mongoid/state.rb
mongoid-1.9.1 lib/mongoid/state.rb
mongoid-1.9.0 lib/mongoid/state.rb