Sha256: 9c971d4feeefa800feda5e4a6fb51f3d278251b5a997704d3eb431c4062286d5

Contents?: true

Size: 1.12 KB

Versions: 7

Compression:

Stored size: 1.12 KB

Contents

module Ohm
  # Provides support for soft deletion
  #
  # @example
  #
  #   class Post < Ohm::Model
  #     include Ohm::SoftDelete
  #
  #     attribute :title
  #     index :title
  #   end
  #
  #   post = Post.create(:title => 'Title')
  #
  #   post.deleted?
  #   # => false
  #
  #   post.delete
  #
  #   post.deleted?
  #   # => true
  #
  #   Post.all.empty?
  #   # => true
  #
  #   Post.find(:title => 'Title').empty?
  #   # => true
  #
  #   Post.exists?(post.id)
  #   # => true
  #
  #   post = Post[post.id]
  #
  #   post.deleted?
  #   # => true
  module SoftDelete
    IS_DELETED = "1"

    def self.included(base)
      base.attribute :deleted
      base.index :deleted
      base.extend ClassMethods
    end

    def delete
      update(:deleted => IS_DELETED)
    end

    def deleted?
      deleted == IS_DELETED
    end

  private
    def create_model_membership
      self.class.key[:all].sadd(self.id)
    end

    def delete_model_membership
      key.del
      self.class.key[:all].srem(self.id)
    end

    module ClassMethods
      def all
        super.except(:deleted => IS_DELETED)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ohm-contrib-0.1.2 lib/ohm/contrib/soft_delete.rb
ohm-contrib-0.1.1 lib/ohm/contrib/soft_delete.rb
ohm-contrib-0.1.0 lib/ohm/contrib/soft_delete.rb
ohm-contrib-0.0.42 lib/ohm/contrib/soft_delete.rb
ohm-contrib-0.0.41 lib/ohm/contrib/soft_delete.rb
ohm-contrib-0.0.40 lib/ohm/contrib/soft_delete.rb
ohm-contrib-0.0.39 lib/ohm/contrib/soft_delete.rb