sequel-soft-destroy 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 307895206c73cd43079f99c83aa643c391e22f4a
4
+ data.tar.gz: f43efe87305c17b7ace9e8723ff0dd8858d17cde
5
+ SHA512:
6
+ metadata.gz: cb2b4aa7829f6a8bd4393c789b5da914821e13a09f75b00c707cbdbce414383651a515d9226132a79f26fea0858731f62a3c4c3adbb5090fd10c8f223e40a73b
7
+ data.tar.gz: 54785dfdf8ea36aba9de166a8a6185842f0077cc24f6089b7f6fbfcb6153ffee91a52f4934f50c6021b320408d70f81a1c85d359a57b0972b6e37ca2ffef3f9e
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ [![Build Status](https://travis-ci.org/fsaravia/sequel-soft-destroy.svg?branch=master)](https://travis-ci.org/fsaravia/sequel-soft-destroy)
2
+
3
+ # Sequel SoftDestroy plugin
4
+
5
+ `Sequel::Plugins::SoftDestroy` is a simple and opinionated Sequel plugin that provides soft delete capabilities.
6
+
7
+ This gem avoids dealing with Sequel internals and relies on models having a `deleted_at` column. When a model is soft deleted, a timestamp will be added to the `deleted_at` field, which marks it as deleted and also stores the UTC time in which the model was deleted. This is useful if any unique constraints have to be observed in your application.
8
+
9
+ ## Usage
10
+
11
+ ```ruby
12
+ class Foo < Sequel::Model
13
+ plugin :soft_destroy
14
+ end
15
+ ```
16
+
17
+ When you want to soft delete a model, just call `soft_destroy` on it:
18
+
19
+ ```ruby
20
+ foo = Foo.create(name: "foo")
21
+
22
+ foo.deleted? #=> false
23
+
24
+ foo.soft_destroy
25
+
26
+ foo.deleted? #=> true
27
+ ```
28
+
29
+ This library also provides convenience methods to filter out deleted items:
30
+
31
+ ```ruby
32
+ foo_1 = Foo.create(name: "foo 2")
33
+ foo_2 = Foo.create(name: "foo 1")
34
+
35
+ foo_1.soft_destroy
36
+
37
+ Foo[foo_1.id] #=> nil
38
+
39
+ Foo.filter_deleted.all #=> [foo_2]
40
+ ```
41
+
42
+ If you ever need to recover a deleted mode, just call `recover`:
43
+
44
+ ```ruby
45
+ foo = Foo.create(name: "foo")
46
+
47
+ foo.deleted? #=> false
48
+
49
+ foo.soft_destroy
50
+
51
+ foo.deleted? #=> true
52
+
53
+ foo.recover
54
+
55
+ foo.deleted? #=> false
56
+ ```
@@ -0,0 +1,41 @@
1
+ require "sequel"
2
+
3
+ module Sequel
4
+ module Plugins
5
+ module SoftDestroy
6
+ DELETED_AT_COLUMN = :deleted_at
7
+
8
+ module ClassMethods
9
+ Sequel::Plugins.def_dataset_methods(self, :filter_deleted)
10
+
11
+ def [](*args)
12
+ model = super
13
+
14
+ return if model.nil? || model.deleted?
15
+
16
+ model
17
+ end
18
+ end
19
+
20
+ module DatasetMethods
21
+ def filter_deleted
22
+ where(DELETED_AT_COLUMN => nil)
23
+ end
24
+ end
25
+
26
+ module InstanceMethods
27
+ def soft_destroy
28
+ update(DELETED_AT_COLUMN => Time.now.utc)
29
+ end
30
+
31
+ def recover
32
+ update(DELETED_AT_COLUMN => nil)
33
+ end
34
+
35
+ def deleted?
36
+ !values[DELETED_AT_COLUMN].nil?
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,15 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "sequel-soft-destroy"
3
+ s.author = "Federico Saravia"
4
+ s.email = "fedesaravia@gmail.com"
5
+ s.version = "0.0.1"
6
+ s.summary = "A simple Sequel plugin that provides soft delete capabilities."
7
+ s.homepage = "https://github.com/fsaravia/sequel-soft-destroy"
8
+ s.license = "MIT"
9
+ s.files = Dir[
10
+ "README.md",
11
+ "lib/**/*.rb",
12
+ "*.gemspec",
13
+ "test/*.*"
14
+ ]
15
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,19 @@
1
+ require "sequel"
2
+ require "minitest/autorun"
3
+
4
+ DB = Sequel.sqlite
5
+
6
+ DB.create_table(:foos) do
7
+ primary_key :id
8
+
9
+ column :name, :text
10
+ column :deleted_at, :integer
11
+ end
12
+
13
+ class SoftDestroyTest < Minitest::Test
14
+ def run
15
+ Sequel::Model.db.transaction(rollback: :always, auto_savepoint: true) do
16
+ super
17
+ end
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sequel-soft-destroy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Federico Saravia
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: fedesaravia@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - README.md
20
+ - lib/sequel/plugins/soft_destroy.rb
21
+ - sequel-soft-destroy.gemspec
22
+ - test/helper.rb
23
+ homepage: https://github.com/fsaravia/sequel-soft-destroy
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.6.11
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: A simple Sequel plugin that provides soft delete capabilities.
47
+ test_files: []