stipa 0.2.4 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +10 -4
- data/lib/stipa/generators/api.rb +1 -0
- data/lib/stipa/generators/base.rb +33 -7
- data/lib/stipa/generators/vue.rb +1 -0
- data/lib/stipa/model/soft_delete.rb +38 -4
- data/lib/stipa/model/uuid.rb +5 -2
- data/lib/stipa/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 007f691ff0ffe9eda4e751fe7d5839c71eaf1de64fbad49544c0e5e8cfc754df
|
|
4
|
+
data.tar.gz: e5f289efe22da0ec25c7e43c7a622d538de4a87f226b3e282d23b7f68c7bde06
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d750ef8b7affa8b8446a6af38b5be1b11fec7526a7132c78f6857c8da99005c30d2811b378e77fe40b5168108b48e1bbe988362abc186f7eabc762b1887e0af5
|
|
7
|
+
data.tar.gz: 2f5994e2de25d2d2c0f5cd7c1f2561e3f0e1b597891585afb97d5f955edf5002f2eead41020564d0c8f6039a76e066ba60a513d0769db4c9ae2ae10b7d98a6b9
|
data/README.md
CHANGED
|
@@ -430,13 +430,19 @@ result[:records] # => [#<User>, ...]
|
|
|
430
430
|
result[:total] # => 150
|
|
431
431
|
result[:total_pages] # => 15
|
|
432
432
|
|
|
433
|
-
# Soft delete
|
|
433
|
+
# Soft delete (requires a `deleted_at` column)
|
|
434
434
|
user.soft_delete
|
|
435
435
|
user.deleted? # => true
|
|
436
|
-
User.all # => excludes soft-deleted
|
|
437
|
-
User.with_deleted # => includes all
|
|
438
|
-
User.only_deleted # => soft-deleted only
|
|
439
436
|
user.restore
|
|
437
|
+
|
|
438
|
+
# Scopes
|
|
439
|
+
User.dataset.deleted # => WHERE (deleted_at IS NOT NULL)
|
|
440
|
+
User.dataset.not_deleted # => WHERE (deleted_at IS NULL)
|
|
441
|
+
|
|
442
|
+
# To exclude soft-deleted records by default, override dataset:
|
|
443
|
+
# def self.dataset
|
|
444
|
+
# super.not_deleted
|
|
445
|
+
# end
|
|
440
446
|
```
|
|
441
447
|
|
|
442
448
|
---
|
data/lib/stipa/generators/api.rb
CHANGED
|
@@ -35,6 +35,7 @@ module Stipa
|
|
|
35
35
|
'controllers/application_controller.rb' => t_application_controller,
|
|
36
36
|
'controllers/health_controller.rb' => t_health_controller,
|
|
37
37
|
'models/application_model.rb' => t_application_model,
|
|
38
|
+
'models/post.rb' => t_sample_model,
|
|
38
39
|
'db/migrate/001_create_posts.rb' => t_migration_create_posts,
|
|
39
40
|
}
|
|
40
41
|
end
|
|
@@ -188,19 +188,45 @@ module Stipa
|
|
|
188
188
|
<<~RUBY
|
|
189
189
|
# frozen_string_literal: true
|
|
190
190
|
|
|
191
|
+
Sequel::Model.require_valid_table = false
|
|
192
|
+
|
|
191
193
|
require 'stipa/model'
|
|
192
194
|
|
|
193
|
-
#
|
|
194
|
-
#
|
|
195
|
+
# Base model class providing timestamps, UUID, soft delete,
|
|
196
|
+
# serialization, dirty tracking, and validation helpers.
|
|
195
197
|
#
|
|
196
198
|
# Usage:
|
|
197
|
-
# class
|
|
198
|
-
# include ApplicationModel
|
|
199
|
+
# class Car < ApplicationModel
|
|
199
200
|
# end
|
|
200
201
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
202
|
+
class ApplicationModel < Sequel::Model
|
|
203
|
+
include Stipa::Model
|
|
204
|
+
|
|
205
|
+
# Ensure every subclass maps to its own table (e.g. Car → cars),
|
|
206
|
+
# not to the parent's abstract +application_models+ table.
|
|
207
|
+
def self.inherited(subclass)
|
|
208
|
+
super
|
|
209
|
+
subclass.set_dataset(subclass.implicit_table_name) unless subclass.name.to_s.empty?
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
Sequel::Model.require_valid_table = true
|
|
214
|
+
RUBY
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def t_sample_model
|
|
218
|
+
<<~RUBY
|
|
219
|
+
# frozen_string_literal: true
|
|
220
|
+
|
|
221
|
+
class Post < ApplicationModel
|
|
222
|
+
def validate
|
|
223
|
+
super
|
|
224
|
+
errors.add(:title, 'is required') if title.to_s.strip.empty?
|
|
225
|
+
errors.add(:slug, 'is required') if slug.to_s.strip.empty?
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
def to_param
|
|
229
|
+
slug
|
|
204
230
|
end
|
|
205
231
|
end
|
|
206
232
|
RUBY
|
data/lib/stipa/generators/vue.rb
CHANGED
|
@@ -71,6 +71,7 @@ module Stipa
|
|
|
71
71
|
'app/controllers/home_controller.rb' => t_home_controller,
|
|
72
72
|
'app/controllers/health_controller.rb' => t_health_controller,
|
|
73
73
|
'app/models/application_model.rb' => t_application_model,
|
|
74
|
+
'app/models/post.rb' => t_sample_model,
|
|
74
75
|
'db/migrate/001_create_posts.rb' => t_migration_create_posts,
|
|
75
76
|
'app/views/layouts/application.html.erb' => t_layout,
|
|
76
77
|
'app/views/home/index.html.erb' => t_home_index,
|
|
@@ -1,5 +1,35 @@
|
|
|
1
1
|
module Stipa
|
|
2
2
|
module Model
|
|
3
|
+
# Soft delete support for Sequel models.
|
|
4
|
+
#
|
|
5
|
+
# Provides `soft_delete`, `restore`, and `deleted?` instance methods,
|
|
6
|
+
# plus `deleted` and `not_deleted` dataset scopes.
|
|
7
|
+
#
|
|
8
|
+
# Your table must have a nullable `deleted_at` timestamp column.
|
|
9
|
+
#
|
|
10
|
+
# # db/migrate/001_create_posts.rb
|
|
11
|
+
# create_table :posts do
|
|
12
|
+
# primary_key :id
|
|
13
|
+
# DateTime :deleted_at
|
|
14
|
+
# # ...
|
|
15
|
+
# end
|
|
16
|
+
#
|
|
17
|
+
# ==== Opt into the default scope
|
|
18
|
+
#
|
|
19
|
+
# By default, soft-deleted rows are NOT filtered from queries.
|
|
20
|
+
# To exclude them automatically, override `dataset` in your model:
|
|
21
|
+
#
|
|
22
|
+
# class Post < ApplicationModel
|
|
23
|
+
# def self.dataset
|
|
24
|
+
# super.not_deleted
|
|
25
|
+
# end
|
|
26
|
+
# end
|
|
27
|
+
#
|
|
28
|
+
# Post.where(title: "foo") # => WHERE (deleted_at IS NULL) AND (title = 'foo')
|
|
29
|
+
#
|
|
30
|
+
# Without the override, include records as usual:
|
|
31
|
+
#
|
|
32
|
+
# Post.where(title: "foo") # => WHERE (title = 'foo')
|
|
3
33
|
module SoftDelete
|
|
4
34
|
def self.included(base)
|
|
5
35
|
base.instance_eval do
|
|
@@ -12,21 +42,25 @@ module Stipa
|
|
|
12
42
|
where(deleted_at: nil)
|
|
13
43
|
end
|
|
14
44
|
end
|
|
15
|
-
|
|
16
|
-
def self.dataset
|
|
17
|
-
super.not_deleted
|
|
18
|
-
end
|
|
19
45
|
end
|
|
20
46
|
end
|
|
21
47
|
|
|
48
|
+
# Mark the record as soft-deleted.
|
|
49
|
+
#
|
|
50
|
+
# post.soft_delete # sets deleted_at = now
|
|
51
|
+
# post.soft_delete # no-op (already deleted)
|
|
22
52
|
def soft_delete
|
|
23
53
|
update(deleted_at: Time.now.utc)
|
|
24
54
|
end
|
|
25
55
|
|
|
56
|
+
# Restore a soft-deleted record.
|
|
57
|
+
#
|
|
58
|
+
# post.restore # sets deleted_at = nil
|
|
26
59
|
def restore
|
|
27
60
|
update(deleted_at: nil)
|
|
28
61
|
end
|
|
29
62
|
|
|
63
|
+
# Returns +true+ if the record has been soft-deleted.
|
|
30
64
|
def deleted?
|
|
31
65
|
!deleted_at.nil?
|
|
32
66
|
end
|
data/lib/stipa/model/uuid.rb
CHANGED
data/lib/stipa/version.rb
CHANGED