Skip to content

Commit f2826d4

Browse files
feat(audited): Add types for Audited::Audit and update types for Audited::Auditor::AuditedInstanceMethods (#972)
- Add types for `Audited::Audit` - Update types for `Audited::Auditor::AuditedInstanceMethods` - `Audited::Auditor::AuditedInstanceMethods` requires 3 module-type-parameters, `AuditableClass`, `AuditClass` and `AuditClassRelation` - Update type for `Audited.audit_class=` - `Audited.audit_class=`'s arguments accept only `String`, but `@audit_class` type is `String | AuditClass`, so I replace `attr_writer` into `_method-member_` - cf. https://github.com/collectiveidea/audited/blob/v5.8.0/README.md#custom-audit-model - cf. https://github.com/collectiveidea/audited/blob/v5.8.0/lib/audited.rb#L21-L26
1 parent e2d1e1c commit f2826d4

File tree

5 files changed

+82
-24
lines changed

5 files changed

+82
-24
lines changed

gems/audited/5.8/_test/test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class Post < ActiveRecord::Base
1111
belongs_to :user
1212

1313
audited only: :title, associated_with: :user, max_audits: 10, comment_required: false
14+
15+
def draft? = false
1416
end
1517

1618
class Comment < ActiveRecord::Base
@@ -62,4 +64,20 @@ class User < ActiveRecord::Base
6264
post = Post.new
6365
post.revisions
6466
post.revision(1)
67+
post.revisions - [Post.new]
68+
post.revision(1)
69+
post.revision(:previous)&.draft?
6570
post.revision_at(Date.parse('2016-01-01'))
71+
post.revision_at(Date.parse('2016-01-01'))&.draft?
72+
post.audited_attributes['published_at']
73+
post.own_and_associated_audits.audited_classes
74+
post.combine_audits(post.own_and_associated_audits)
75+
76+
audit = Audited::Audit.new
77+
audit.ancestors.find(1)
78+
audit.undo
79+
audit.new_attributes['action']
80+
audit.old_attributes['action']
81+
Audited::Audit.as_user(User.new) do
82+
Post.create!(title: 'Post audited as user')
83+
end

gems/audited/5.8/_test/test.rbs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
class Post < ActiveRecord::Base
22
extend Audited::Auditor::ClassMethods
33
extend Audited::Auditor::AuditedClassMethods
4-
include Audited::Auditor::AuditedInstanceMethods
4+
include Audited::Auditor::AuditedInstanceMethods[Post, Audited::Audit, Audited::Audit::ActiveRecord_Relation]
5+
6+
def draft?: () -> bool
57
end
68

79
class Comment < ActiveRecord::Base
810
extend Audited::Auditor::ClassMethods
911
extend Audited::Auditor::AuditedClassMethods
10-
include Audited::Auditor::AuditedInstanceMethods
12+
include Audited::Auditor::AuditedInstanceMethods[Post, Audited::Audit, Audited::Audit::ActiveRecord_Relation]
1113
end
1214

1315
class User < ActiveRecord::Base
1416
extend Audited::Auditor::ClassMethods
1517
extend Audited::Auditor::AuditedClassMethods
16-
include Audited::Auditor::AuditedInstanceMethods
18+
include Audited::Auditor::AuditedInstanceMethods[Post, Audited::Audit, Audited::Audit::ActiveRecord_Relation]
1719
end

gems/audited/5.8/audited.rbs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ module Audited
55
attr_accessor self.ignored_default_callbacks: Array[:create | :update | :touch | :destroy]
66
attr_accessor self.max_audits: Integer
77
attr_accessor self.store_synthesized_enums: bool
8-
attr_writer self.audit_class (@audit_class): untyped
8+
9+
def self.audit_class=: (String audit_class) -> void
910

1011
def self.audit_class: () -> untyped
1112

gems/audited/5.8/audited/audit.rbs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
module Audited
2+
class YAMLIfTextColumnType
3+
def self.load: (untyped obj) -> untyped
4+
5+
def self.dump: (untyped obj) -> untyped
6+
7+
def self.text_column?: () -> bool
8+
end
9+
10+
class Audit < ActiveRecord::Base
11+
def ancestors: () -> ActiveRecord_Relation
12+
13+
def revision: () -> untyped
14+
15+
def new_attributes: () -> ActiveSupport::HashWithIndifferentAccess[String, untyped]
16+
17+
def old_attributes: () -> ActiveSupport::HashWithIndifferentAccess[String, untyped]
18+
19+
def undo: () -> void
20+
21+
private
22+
23+
def set_version_number: () -> void
24+
25+
def set_audit_user: () -> void
26+
27+
def set_request_uuid: () -> void
28+
29+
def set_remote_address: () -> void
30+
31+
module ClassMethods
32+
def as_user: [R] (untyped user) { () -> R } -> R
33+
34+
def audited_classes: () -> Array[untyped]
35+
end
36+
37+
extend ClassMethods
38+
39+
class ActiveRecord_Relation < ::ActiveRecord::Relation
40+
include ClassMethods
41+
end
42+
end
43+
end

gems/audited/5.8/audited/auditor.rbs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,7 @@ module Audited
3636
def set_auited_options: (audited_options options) -> void
3737
end
3838

39-
module AuditedInstanceMethods
40-
interface _AuditsToCombine
41-
def last: () -> untyped
42-
def pluck: (*untyped keys) -> Array[untyped]
43-
def unscope: (*untyped args) -> untyped
44-
end
45-
39+
module AuditedInstanceMethods[AuditableClass, AuditClass, AuditClassRelation]
4640
REDACTED: "[REDACTED]"
4741

4842
def save_without_auditing: () -> bool
@@ -53,33 +47,33 @@ module Audited
5347

5448
def with_auditing: [R] () { () -> R } -> R
5549

56-
def revisions: (?Integer from_version) -> Array[untyped]
50+
def revisions: (?Integer from_version) -> Array[AuditableClass]
5751

58-
def revision: (Integer | :previous version) -> void
52+
def revision: (Integer | :previous version) -> AuditableClass?
5953

60-
def revision_at: (ActiveSupport::TimeWithZone | Date | DateTime | Time | String date_or_time) -> void
54+
def revision_at: (ActiveSupport::TimeWithZone | Date | DateTime | Time | String date_or_time) -> AuditableClass?
6155

62-
def audited_attributes: () -> Hash[untyped, untyped]
56+
def audited_attributes: () -> Hash[String, untyped]
6357

64-
def own_and_associated_audits: () -> untyped
58+
def own_and_associated_audits: () -> AuditClassRelation
6559

66-
def combine_audits: (_AuditsToCombine audits_to_combine) -> void
60+
def combine_audits: (AuditClassRelation audits_to_combine) -> void
6761

6862
private
6963

7064
def audited_changes: (?for_touch: bool, ?exclude_readonly_attrs: bool) -> Hash[String, untyped]
7165

72-
def normalize_enum_changes: (Hash[untyped, untyped] changes) -> Hash[untyped, untyped]
66+
def normalize_enum_changes: (Hash[String, untyped] changes) -> Hash[String, untyped]
7367

74-
def redact_values: (Hash[untyped, untyped] filtered_changes) -> Hash[untyped, untyped]
68+
def redact_values: (Hash[String, untyped] filtered_changes) -> Hash[String, untyped]
7569

76-
def filter_encrypted_attrs: (Hash[untyped, untyped] filtered_changes) -> Hash[untyped, untyped]
70+
def filter_encrypted_attrs: (Hash[String, untyped] filtered_changes) -> Hash[String, untyped]
7771

78-
def filter_attr_values: (?audited_changes: Hash[untyped, untyped], ?attrs: Array[untyped], ?placeholder: String) -> Hash[untyped, untyped]
72+
def filter_attr_values: (?audited_changes: Hash[String, untyped], ?attrs: Array[String], ?placeholder: String) -> Hash[String, untyped]
7973

8074
def rails_below?: (String rails_version) -> bool
8175

82-
def audits_to: (?(Integer | :previous)? version) -> untyped
76+
def audits_to: (?(Integer | :previous)? version) -> AuditClassRelation
8377

8478
def audit_create: () -> void
8579

@@ -103,9 +97,9 @@ module Audited
10397

10498
def auditing_enabled: () -> bool
10599

106-
def run_conditional_check: (untyped condition, ?matching: bool) -> bool
100+
def run_conditional_check: (bool | String | Symbol | _Callable condition, ?matching: bool) -> bool
107101

108-
def reconstruct_attributes: (Array[untyped] audits) -> Hash[untyped, untyped]
102+
def reconstruct_attributes: (AuditClassRelation | Array[AuditClass] audits) -> Hash[String, untyped]
109103
end
110104

111105
module AuditedClassMethods

0 commit comments

Comments
 (0)