Module: DeleteSoftly::ARExtender
- Defined in:
- lib/delete_softly.rb
Instance Method Summary collapse
-
#active ⇒ Object
Always have Model.active available.
-
#delete_softly(options = {:enforce => :active}) ⇒ Object
Make the model delete softly.
Instance Method Details
#active ⇒ Object
Always have Model.active available. It is a good practice to use it when you want the active records. Even use it when no logic is in place yet. Now it is an alias for scoped, but can be overwritten for custom behaviour, for example:
class Post < ActiveRecord::Base
delete_softly
has_many :comments
def self.active
super.where(:disabled.ne => true)
end
end
class Comment < ActiveRecord::Base
belongs_to :post
end
will result in:
Post.all #=> SELECT * FROM posts WHERE deleted_at IS NULL;
Post.active #=> SELECT * FROM posts WHERE deleted_at IS NULL AND disabled != 't';
Comment.all #=> SELECT * FROM comments;
Comment.active #=> SELECT * FROM comments;
35 36 37 |
# File 'lib/delete_softly.rb', line 35 def active scoped end |
#delete_softly(options = {:enforce => :active}) ⇒ Object
Make the model delete softly. A deleted_at:datetime column is required for this to work. The two most important differences are that it can be enforce on a model or be more free.
class Post < ActiveRecord::Base
delete_softly
end
will enforce soft delete on the post model. A deleted post will never appear, unless the explicit with_deleted is called. When the model is:
class Post < ActiveRecord::Base
delete_softly :enforce => false
end
An object will still be available after destroy is called,
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/delete_softly.rb', line 52 def delete_softly( = {:enforce => :active}) # Make destroy! the old destroy alias_method :destroy!, :destroy include DeleteSoftly::InstanceMethods extend DeleteSoftly::ClassMethods # Support single argument # delete_softly :active # Same as :enforce => :active, default behaviour # delete_softly :enforce=> :with_deleted # Same as without argument = {:enforce=> } unless .is_a?(Hash) if [:enforce] if [:enforce].is_a?(Symbol) && respond_to?([:enforce]) default_scope send([:enforce]) else default_scope active end end end |