better_record 0.8.2 → 0.8.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 +4 -4
- data/app/models/better_record/base.rb +7 -0
- data/app/models/better_record/current.rb +27 -6
- data/app/models/better_record/logged_action.rb +6 -0
- data/app/models/better_record/model_concerns/has_protected_password.rb +76 -0
- data/app/models/better_record/model_concerns/has_validated_avatar.rb +111 -0
- data/app/models/better_record/table_size.rb +4 -0
- data/lib/better_record/concerns/active_record_extensions/base_extensions.rb +0 -2
- data/lib/better_record/version.rb +1 -1
- data/lib/generators/better_record/setup/templates/current.rb +4 -0
- data/lib/templates/active_record/model/model.rb +4 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a112c2583da52c9e168052c349be6a9ad0abc16c95f74be580fdcc57ddddf33a
|
4
|
+
data.tar.gz: f7b4575c6d436d2a1f2e69d5f24ed356027424cc2523c6871b1710bb362cdc18
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4703f8f2b2881d9092f199af42454b88d83f001c354b6cf751fa81d6b59d08df02d22a73c17d733e7ad30b5e6bd67554ae65ed86a433fcdc57e37aea6971c705
|
7
|
+
data.tar.gz: e05e27899557daba3b3fdf9ee3016d51e3f15747bc2b4691881328fbb9d40273f50e045a17a5db2965764636782a8baa9af4e5545317c6dec973c3e079f67247
|
@@ -1,6 +1,8 @@
|
|
1
1
|
module BetterRecord
|
2
2
|
class Base < ActiveRecord::Base
|
3
3
|
self.abstract_class = true
|
4
|
+
include ModelConcerns::HasValidatedAvatar
|
5
|
+
include ModelConcerns::HasProtectedPassword
|
4
6
|
|
5
7
|
# == Constants ============================================================
|
6
8
|
|
@@ -23,11 +25,16 @@ module BetterRecord
|
|
23
25
|
|
24
26
|
# == Callbacks ============================================================
|
25
27
|
|
28
|
+
# == Boolean Class Methods ================================================
|
29
|
+
|
26
30
|
# == Class Methods ========================================================
|
27
31
|
|
32
|
+
# == Boolean Methods ======================================================
|
33
|
+
|
28
34
|
# == Instance Methods =====================================================
|
29
35
|
def indifferent_attributes
|
30
36
|
attributes.with_indifferent_access
|
31
37
|
end
|
38
|
+
|
32
39
|
end
|
33
40
|
end
|
@@ -2,10 +2,28 @@
|
|
2
2
|
|
3
3
|
module BetterRecord
|
4
4
|
class Current < ActiveSupport::CurrentAttributes
|
5
|
+
# == Constants ============================================================
|
6
|
+
|
7
|
+
# == Attributes ===========================================================
|
5
8
|
attribute :user, :ip_address
|
6
9
|
|
7
|
-
|
8
|
-
|
10
|
+
# == Extensions ===========================================================
|
11
|
+
|
12
|
+
# == Relationships ========================================================
|
13
|
+
|
14
|
+
# == Validations ==========================================================
|
15
|
+
|
16
|
+
# == Scopes ===============================================================
|
17
|
+
|
18
|
+
# == Callbacks ============================================================
|
19
|
+
|
20
|
+
# == Boolean Class Methods ================================================
|
21
|
+
|
22
|
+
# == Class Methods ========================================================
|
23
|
+
def self.drop_values
|
24
|
+
self.user = nil
|
25
|
+
self.ip_address = nil
|
26
|
+
self
|
9
27
|
end
|
10
28
|
|
11
29
|
def self.set(user, ip)
|
@@ -14,10 +32,13 @@ module BetterRecord
|
|
14
32
|
self
|
15
33
|
end
|
16
34
|
|
17
|
-
def self.
|
18
|
-
self.user
|
19
|
-
self.ip_address = nil
|
20
|
-
self
|
35
|
+
def self.user_type
|
36
|
+
BetterRecord::PolymorphicOverride.polymorphic_value(self.user.class) if self.user
|
21
37
|
end
|
38
|
+
|
39
|
+
# == Boolean Methods ======================================================
|
40
|
+
|
41
|
+
# == Instance Methods =====================================================
|
42
|
+
|
22
43
|
end
|
23
44
|
end
|
@@ -21,12 +21,15 @@ module BetterRecord
|
|
21
21
|
primary_type: :table_name,
|
22
22
|
foreign_key: :row_id,
|
23
23
|
foreign_type: :table_name
|
24
|
+
|
24
25
|
# == Validations ==========================================================
|
25
26
|
|
26
27
|
# == Scopes ===============================================================
|
27
28
|
|
28
29
|
# == Callbacks ============================================================
|
29
30
|
|
31
|
+
# == Boolean Class Methods ================================================
|
32
|
+
|
30
33
|
# == Class Methods ========================================================
|
31
34
|
def self.default_print
|
32
35
|
[
|
@@ -40,6 +43,8 @@ module BetterRecord
|
|
40
43
|
]
|
41
44
|
end
|
42
45
|
|
46
|
+
# == Boolean Methods ======================================================
|
47
|
+
|
43
48
|
# == Instance Methods =====================================================
|
44
49
|
def changed_columns
|
45
50
|
(self.changed_fields || {}).keys.join(', ').presence || 'N/A'
|
@@ -48,5 +53,6 @@ module BetterRecord
|
|
48
53
|
def action_type
|
49
54
|
ACTIONS[action] || 'UNKNOWN'
|
50
55
|
end
|
56
|
+
|
51
57
|
end
|
52
58
|
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/concern'
|
4
|
+
require 'active_support/number_helper'
|
5
|
+
|
6
|
+
module BetterRecord
|
7
|
+
module ModelConcerns
|
8
|
+
module HasProtectedPassword
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def has_protected_password(
|
13
|
+
password_field: :password,
|
14
|
+
password_validator: nil,
|
15
|
+
min_image_size: nil,
|
16
|
+
max_image_size: 500.kilobytes,
|
17
|
+
**opts
|
18
|
+
)
|
19
|
+
# == Constants ============================================================
|
20
|
+
|
21
|
+
# == Attributes ===========================================================
|
22
|
+
attribute :"new_#{password_field}", :text
|
23
|
+
attribute :"new_#{password_field}_confirmation", :text
|
24
|
+
|
25
|
+
# == Extensions ===========================================================
|
26
|
+
|
27
|
+
# == Relationships ========================================================
|
28
|
+
|
29
|
+
# == Validations ==========================================================
|
30
|
+
validate :"new_#{password_field}", :"require_#{password_field}_confirmation", if: :"new_#{password_field}?"
|
31
|
+
|
32
|
+
if password_validator
|
33
|
+
validate password_validator if :"new_#{password_field}?"
|
34
|
+
end
|
35
|
+
|
36
|
+
# == Scopes ===============================================================
|
37
|
+
|
38
|
+
# == Callbacks ============================================================
|
39
|
+
|
40
|
+
# == Boolean Class Methods ================================================
|
41
|
+
|
42
|
+
# == Class Methods ========================================================
|
43
|
+
|
44
|
+
# == Boolean Methods ======================================================
|
45
|
+
|
46
|
+
# == Instance Methods =====================================================
|
47
|
+
define_method password_field do
|
48
|
+
self[password_field]
|
49
|
+
end
|
50
|
+
private password_field
|
51
|
+
|
52
|
+
define_method :"#{password_field}=" do |value|
|
53
|
+
write_attribute password_field, value
|
54
|
+
end
|
55
|
+
private :"#{password_field}="
|
56
|
+
|
57
|
+
define_method :"require_#{password_field}_confirmation" do
|
58
|
+
tmp_new_pwd = __send__ :"new_#{password_field}"
|
59
|
+
tmp_new_confirmation = __send__ :"new_#{password_field}_confirmation"
|
60
|
+
|
61
|
+
if tmp_new_pwd.present?
|
62
|
+
if tmp_new_pwd != tmp_new_confirmation
|
63
|
+
errors.add(:"new_#{password_field}", 'Password does not match confirmation')
|
64
|
+
else
|
65
|
+
self.password = new_password
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
private :"require_#{password_field}_confirmation"
|
70
|
+
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/concern'
|
4
|
+
require 'active_support/number_helper'
|
5
|
+
|
6
|
+
module BetterRecord
|
7
|
+
module ModelConcerns
|
8
|
+
module HasValidatedAvatar
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def has_validated_avatar(
|
13
|
+
avatar_name: :avatar,
|
14
|
+
image_validator: :valid_image,
|
15
|
+
min_image_size: nil,
|
16
|
+
max_image_size: 500.kilobytes,
|
17
|
+
**opts
|
18
|
+
)
|
19
|
+
# == Constants ============================================================
|
20
|
+
|
21
|
+
# == Attributes ===========================================================
|
22
|
+
attribute :"new_#{avatar_name}?", :boolean
|
23
|
+
|
24
|
+
# == Extensions ===========================================================
|
25
|
+
|
26
|
+
# == Relationships ========================================================
|
27
|
+
has_one_attached :"last_#{avatar_name}"
|
28
|
+
has_one_attached avatar_name
|
29
|
+
|
30
|
+
# == Validations ==========================================================
|
31
|
+
validate avatar_name, image_validator
|
32
|
+
|
33
|
+
# == Scopes ===============================================================
|
34
|
+
|
35
|
+
# == Callbacks ============================================================
|
36
|
+
after_save :"cache_current_#{avatar_name}", if: :"new_#{avatar_name}?"
|
37
|
+
|
38
|
+
# == Boolean Class Methods ================================================
|
39
|
+
|
40
|
+
# == Class Methods ========================================================
|
41
|
+
|
42
|
+
# == Boolean Methods ======================================================
|
43
|
+
|
44
|
+
# == Instance Methods =====================================================
|
45
|
+
define_method :"attach_#{avatar_name}" do |file, **options|
|
46
|
+
__send__(avatar_name).attach(file, **options)
|
47
|
+
__send__ image_validator
|
48
|
+
end
|
49
|
+
|
50
|
+
define_method :valid_image_format do
|
51
|
+
unless __send__(avatar_name).blob.content_type.start_with? 'image/'
|
52
|
+
errors.add(avatar_name, 'is not an image file')
|
53
|
+
return false
|
54
|
+
end
|
55
|
+
true
|
56
|
+
end
|
57
|
+
|
58
|
+
define_method :valid_image_size do
|
59
|
+
if max_image_size && __send__(avatar_name).blob.byte_size > max_image_size
|
60
|
+
errors.add(avatar_name, "is too large, maximum #{ActiveSupport::NumberHelper.number_to_human_size(max_image_size)}")
|
61
|
+
return false
|
62
|
+
elsif min_image_size && __send__(avatar_name).blob.byte_size < min_image_size
|
63
|
+
errors.add(avatar_name, "is too small, minimum #{ActiveSupport::NumberHelper.number_to_human_size(min_image_size)}")
|
64
|
+
return false
|
65
|
+
end
|
66
|
+
true
|
67
|
+
end
|
68
|
+
|
69
|
+
define_method :valid_image do
|
70
|
+
return unless __send__(avatar_name).attached?
|
71
|
+
|
72
|
+
if valid_image_format && valid_image_size
|
73
|
+
__send__(:"cache_current_#{avatar_name}")
|
74
|
+
else
|
75
|
+
purge(__send__(avatar_name))
|
76
|
+
__send__(:"load_last_#{avatar_name}") if __send__(:"last_#{avatar_name}").attached?
|
77
|
+
false
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
define_method :"cache_current_#{avatar_name}" do
|
82
|
+
__send__ :"copy_#{avatar_name}"
|
83
|
+
end
|
84
|
+
|
85
|
+
define_method :"load_last_#{avatar_name}" do
|
86
|
+
__send__ :"copy_#{avatar_name}", :"last_#{avatar_name}", avatar_name
|
87
|
+
end
|
88
|
+
|
89
|
+
define_method :"copy_#{avatar_name}" do |from = avatar_name, to = :"last_#{avatar_name}"|
|
90
|
+
from = __send__ from
|
91
|
+
to = __send__ to
|
92
|
+
|
93
|
+
purge(to) if to.attached?
|
94
|
+
|
95
|
+
tmp = Tempfile.new
|
96
|
+
tmp.binmode
|
97
|
+
tmp.write(from.download)
|
98
|
+
tmp.flush
|
99
|
+
tmp.rewind
|
100
|
+
|
101
|
+
to.attach(io: tmp, filename: from.filename, content_type: from.content_type)
|
102
|
+
tmp.close
|
103
|
+
true
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -48,6 +48,8 @@ module BetterRecord
|
|
48
48
|
default_scope { where(schema: [ :public ]) }
|
49
49
|
# == Callbacks ============================================================
|
50
50
|
|
51
|
+
# == Boolean Class Methods ================================================
|
52
|
+
|
51
53
|
# == Class Methods ========================================================
|
52
54
|
def self.find_by(*args)
|
53
55
|
reload_data
|
@@ -72,6 +74,8 @@ module BetterRecord
|
|
72
74
|
# ]
|
73
75
|
# end
|
74
76
|
|
77
|
+
# == Boolean Methods ======================================================
|
78
|
+
|
75
79
|
# == Instance Methods =====================================================
|
76
80
|
def changed_columns
|
77
81
|
(self.changed_fields || {}).keys.join(', ').presence || 'N/A'
|
@@ -15,6 +15,8 @@ module Current
|
|
15
15
|
|
16
16
|
# == Callbacks ============================================================
|
17
17
|
|
18
|
+
# == Boolean Class Methods ================================================
|
19
|
+
|
18
20
|
# == Class Methods ========================================================
|
19
21
|
|
20
22
|
def self.user
|
@@ -45,6 +47,8 @@ module Current
|
|
45
47
|
BetterRecord::Current.drop_values
|
46
48
|
end
|
47
49
|
|
50
|
+
# == Boolean Methods ======================================================
|
51
|
+
|
48
52
|
# == Instance Methods =====================================================
|
49
53
|
|
50
54
|
end
|
@@ -25,8 +25,12 @@ class <%= class_name %> < <%= parent_class_name.classify %>
|
|
25
25
|
|
26
26
|
# == Callbacks ============================================================
|
27
27
|
|
28
|
+
# == Boolean Class Methods ================================================
|
29
|
+
|
28
30
|
# == Class Methods ========================================================
|
29
31
|
|
32
|
+
# == Boolean Methods ======================================================
|
33
|
+
|
30
34
|
# == Instance Methods =====================================================
|
31
35
|
|
32
36
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: better_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sampson Crowley
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-08-
|
11
|
+
date: 2018-08-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -239,6 +239,8 @@ files:
|
|
239
239
|
- app/models/better_record/base.rb
|
240
240
|
- app/models/better_record/current.rb
|
241
241
|
- app/models/better_record/logged_action.rb
|
242
|
+
- app/models/better_record/model_concerns/has_protected_password.rb
|
243
|
+
- app/models/better_record/model_concerns/has_validated_avatar.rb
|
242
244
|
- app/models/better_record/table_size.rb
|
243
245
|
- app/views/better_record/table_sizes/index.html.erb
|
244
246
|
- app/views/better_record/table_sizes/show.html.erb
|