forked from ubicloud/ubicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.rb
134 lines (111 loc) · 3.31 KB
/
model.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# frozen_string_literal: true
require_relative "db"
require "sequel/model"
if ENV["RACK_ENV"] == "development"
Sequel::Model.cache_associations = false
end
Sequel::Model.plugin :auto_validations
Sequel::Model.plugin :require_valid_schema
Sequel::Model.plugin :singular_table_names
Sequel::Model.plugin :subclasses unless ENV["RACK_ENV"] == "development"
Sequel::Model.plugin :column_encryption do |enc|
enc.key 0, Config.clover_column_encryption_key
end
module SemaphoreMethods
def self.included(base)
base.class_eval do
one_to_many :semaphores, key: :strand_id
end
base.extend(ClassMethods)
end
module ClassMethods
def semaphore(*names)
names.map!(&:intern)
names.each do |name|
define_method "incr_#{name}" do
Semaphore.incr(id, name)
end
define_method "#{name}_set?" do
semaphores.any? { |s| s.name == name.to_s }
end
end
end
end
end
module ResourceMethods
def self.included(base)
base.extend(ClassMethods)
end
def ubid
@ubid ||= UBID.from_uuidish(id).to_s.downcase
end
NON_ARCHIVED_MODELS = ["DeletedRecord", "Semaphore"]
def before_destroy
model_name = self.class.name
unless NON_ARCHIVED_MODELS.include?(model_name)
model_values = values.merge(model_name: model_name)
encryption_metadata = self.class.instance_variable_get(:@column_encryption_metadata)
unless encryption_metadata.empty?
encryption_metadata.keys.each do |key|
model_values.delete(key)
end
end
DeletedRecord.create(deleted_at: Time.now, model_name: model_name, model_values: model_values)
end
super
end
module ClassMethods
# Adapted from sequel/model/inflections.rb's underscore, to convert
# class names into symbols
def self.uppercase_underscore(s)
s.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z\d])([A-Z])/, '\1_\2').tr("-", "_").upcase
end
def from_ubid(ubid)
self[id: UBID.parse(ubid).to_uuid]
rescue UBIDParseError
nil
end
def ubid_type
Object.const_get("UBID::TYPE_#{ClassMethods.uppercase_underscore(name)}")
end
def generate_ubid
UBID.generate(ubid_type)
end
def generate_uuid
generate_ubid.to_uuid
end
def create_with_id(*args, **kwargs)
create(*args, **kwargs) { _1.id = generate_uuid }
end
end
end
if ENV["RACK_ENV"] == "development" || ENV["RACK_ENV"] == "test"
require "logger"
LOGGER = Logger.new($stdout)
LOGGER.level = Logger::FATAL if ENV["RACK_ENV"] == "test"
DB.loggers << LOGGER
end
module SequelExtensions
def delete(force: false, &block)
rodaauth_in_callstack = !caller.grep(/rodauth/).empty?
destroy_in_callstack = !caller.grep(/sequel\/model\/base.*_destroy_delete/).empty?
unless rodaauth_in_callstack || destroy_in_callstack || force
raise "Calling delete is discouraged as it skips hooks such as before_destroy, which " \
"we use to archive records. Use destroy instead. If you know what you are doing " \
"and still want to use delete, you can pass force: true to trigger delete."
end
if is_a?(Sequel::Dataset)
super(&block)
else
super()
end
end
end
module Sequel
class Dataset
prepend SequelExtensions
end
class Model
prepend SequelExtensions
end
end