better_record 0.7.2 → 0.7.3
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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6fc69e149e9934f116d1b8a1b6acdc2d7a3feca91946efad68c8d79e9aac6db2
|
4
|
+
data.tar.gz: b09dc11d66083687ba54e1aa9da41381d798b621b695bf015e46567cd51152f3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5c0e1b68ca9451c72d876f6f01e231532781af82bf44c0b58c93b31e58fd5c960be3996ec81cdc5074174b05e1693e21ee11d5bc00b8b799ef90055ce5a02016
|
7
|
+
data.tar.gz: ff13808fb4c58fd68694afe9d147d295bcf59ef5583105e9c66983d194ee4671eda9073670c9810fa9262497bf661af5b67d696042bbf6809f3fcc9494426374
|
@@ -1,5 +1,19 @@
|
|
1
1
|
module BetterRecord
|
2
2
|
class Current < ActiveSupport::CurrentAttributes
|
3
3
|
attribute :user, :ip_address
|
4
|
+
|
5
|
+
def self.user_type
|
6
|
+
BetterRecord::PolymorphicOverride.polymorphic_value(self.user.class) if self.user
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.set(user, ip)
|
10
|
+
self.user = user.presence || nil
|
11
|
+
self.ip_address = ip.presence || nil
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.drop_values
|
15
|
+
self.user = nil
|
16
|
+
self.ip_address = nil
|
17
|
+
end
|
4
18
|
end
|
5
19
|
end
|
@@ -10,26 +10,6 @@ module BetterRecord
|
|
10
10
|
end
|
11
11
|
|
12
12
|
class_methods do
|
13
|
-
def table_name_defined?
|
14
|
-
@table_name_defined ||= method_defined?(:table_name) || !!table_name.present?
|
15
|
-
end
|
16
|
-
|
17
|
-
def table_name_has_schema?
|
18
|
-
@table_name_has_schema ||= (table_name =~ /\w+\.\w+/)
|
19
|
-
end
|
20
|
-
|
21
|
-
def table_name_without_schema
|
22
|
-
@table_name_without_schema ||= (table_name =~ /\w+\.\w+/) ? table_name.split('.').last : table_name
|
23
|
-
end
|
24
|
-
|
25
|
-
def table_name_with_schema
|
26
|
-
@table_name_without_schema ||= "#{table_schema}.#{table_name_without_schema}"
|
27
|
-
end
|
28
|
-
|
29
|
-
def table_schema
|
30
|
-
@table_schema ||= table_name_has_schema? ? table_name.split('.').first : 'public'
|
31
|
-
end
|
32
|
-
|
33
13
|
def audit_relation_name
|
34
14
|
@@audit_relation_name ||= (BetterRecord.audit_relation_name.presence || :audits).to_sym
|
35
15
|
end
|
@@ -54,6 +34,10 @@ module BetterRecord
|
|
54
34
|
str
|
55
35
|
end
|
56
36
|
|
37
|
+
def current_user_type
|
38
|
+
BetterRecord::Current.user_type
|
39
|
+
end
|
40
|
+
|
57
41
|
def default_print
|
58
42
|
column_names
|
59
43
|
end
|
@@ -76,14 +60,34 @@ module BetterRecord
|
|
76
60
|
@@queue_adapter == :inline
|
77
61
|
end
|
78
62
|
|
63
|
+
def table_name_defined?
|
64
|
+
@table_name_defined ||= method_defined?(:table_name) || !!table_name.present?
|
65
|
+
end
|
66
|
+
|
67
|
+
def table_name_has_schema?
|
68
|
+
@table_name_has_schema ||= (table_name =~ /\w+\.\w+/)
|
69
|
+
end
|
70
|
+
|
71
|
+
def table_name_without_schema
|
72
|
+
@table_name_without_schema ||= (table_name =~ /\w+\.\w+/) ? table_name.split('.').last : table_name
|
73
|
+
end
|
74
|
+
|
75
|
+
def table_name_with_schema
|
76
|
+
@table_name_without_schema ||= "#{table_schema}.#{table_name_without_schema}"
|
77
|
+
end
|
78
|
+
|
79
|
+
def table_schema
|
80
|
+
@table_schema ||= table_name_has_schema? ? table_name.split('.').first : 'public'
|
81
|
+
end
|
82
|
+
|
79
83
|
def table_size
|
80
84
|
BetterRecord::TableSize.unscoped.find_by(name: table_name_without_schema, schema: table_schema)
|
81
85
|
end
|
82
86
|
|
83
87
|
def transaction(*args)
|
84
88
|
super(*args) do
|
85
|
-
if Current.user
|
86
|
-
ip = Current.ip_address ? "'#{Current.ip_address}'" : 'NULL'
|
89
|
+
if BetterRecord::Current.user
|
90
|
+
ip = BetterRecord::Current.ip_address ? "'#{BetterRecord::Current.ip_address}'" : 'NULL'
|
87
91
|
|
88
92
|
ActiveRecord::Base.connection.execute <<-SQL
|
89
93
|
CREATE TEMP TABLE IF NOT EXISTS
|
@@ -92,15 +96,15 @@ module BetterRecord
|
|
92
96
|
UPDATE
|
93
97
|
_app_user
|
94
98
|
SET
|
95
|
-
user_id=#{Current.user.id},
|
96
|
-
user_type='#{
|
99
|
+
user_id=#{BetterRecord::Current.user.id},
|
100
|
+
user_type='#{current_user_type}',
|
97
101
|
ip_address=#{ip};
|
98
102
|
|
99
103
|
INSERT INTO
|
100
104
|
_app_user (user_id, user_type, ip_address)
|
101
105
|
SELECT
|
102
|
-
#{Current.user.id},
|
103
|
-
'#{
|
106
|
+
#{BetterRecord::Current.user.id},
|
107
|
+
'#{current_user_type}',
|
104
108
|
#{ip}
|
105
109
|
WHERE NOT EXISTS (SELECT * FROM _app_user);
|
106
110
|
SQL
|
@@ -14,24 +14,35 @@ module Current
|
|
14
14
|
# == Callbacks ============================================================
|
15
15
|
|
16
16
|
# == Class Methods ========================================================
|
17
|
+
|
18
|
+
def self.user
|
19
|
+
BetterRecord::Current.user
|
20
|
+
end
|
21
|
+
|
17
22
|
def self.user=(user)
|
18
23
|
set_user(user)
|
19
24
|
end
|
20
25
|
|
26
|
+
def self.ip_address
|
27
|
+
BetterRecord::Current.user
|
28
|
+
end
|
29
|
+
|
21
30
|
def self.ip_address=(user)
|
22
31
|
set_user(user)
|
23
32
|
end
|
24
33
|
|
34
|
+
def self.user_type
|
35
|
+
BetterRecord::Current.user_type
|
36
|
+
end
|
37
|
+
|
25
38
|
def self.set(user, ip)
|
26
|
-
BetterRecord::Current.user
|
27
|
-
BetterRecord::Current.ip_address = ip.presence || nil
|
39
|
+
BetterRecord::Current.set(user, ip)
|
28
40
|
end
|
29
41
|
|
30
42
|
def self.drop_values
|
31
|
-
BetterRecord::Current.
|
32
|
-
BetterRecord::Current.ip_address = nil
|
43
|
+
BetterRecord::Current.drop_values
|
33
44
|
end
|
34
|
-
|
45
|
+
|
35
46
|
# == Instance Methods =====================================================
|
36
47
|
|
37
48
|
end
|