Class: ApplicationRecord
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- ApplicationRecord
show all
- Defined in:
- app/models/application_record.rb
Direct Known Subclasses
ApiClient, Avatar, Chapter, Complement, Content, Course, Discussion, Exam, ExamAuthorization, Exercise, Invitation, Language, Lesson, Message, Organization, Progress, Subscription, Upvote, Usage, User
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.aggregate_of(association) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'app/models/application_record.rb', line 68
def self.aggregate_of(association)
class_eval do
define_method("rebuild_#{association}!") do |children|
transaction do
self.send(association).all_except(children).destroy_all
self.update! association => children
children.each &:save!
end
reload
end
end
end
|
.all_except(others) ⇒ Object
11
12
13
14
15
16
17
|
# File 'app/models/application_record.rb', line 11
def self.all_except(others)
if others.present?
where.not(id: [others.map(&:id)])
else
all
end
end
|
.defaults(&block) ⇒ Object
6
7
8
9
|
# File 'app/models/application_record.rb', line 6
def self.defaults(&block)
after_initialize :defaults, if: :new_record?
define_method :defaults, &block
end
|
.numbered(*associations) ⇒ Object
81
82
83
84
85
86
87
88
89
90
|
# File 'app/models/application_record.rb', line 81
def self.numbered(*associations)
class_eval do
associations.each do |it|
define_method("#{it}=") do |e|
e.merge_numbers!
super(e)
end
end
end
end
|
.organic_on(*selectors) ⇒ Object
104
105
106
107
108
109
110
|
# File 'app/models/application_record.rb', line 104
def self.organic_on(*selectors)
selectors.each do |selector|
define_method("#{selector}_in_organization") do |organization = Organization.current|
send(selector).where(organization: organization)
end
end
end
|
.resource_fields(*fields) ⇒ Object
Partially implements resource-hash protocol, by defining ‘to_resource_h` and helper methods `resource_fields` and `slice_resource_h` using the given fields
115
116
117
118
119
120
121
|
# File 'app/models/application_record.rb', line 115
def self.resource_fields(*fields)
include Mumuki::Domain::Syncable::WithResourceFields
define_singleton_method :resource_fields do
fields
end
end
|
.serialize_symbolized_hash_array(*keys) ⇒ Object
19
20
21
22
23
24
|
# File 'app/models/application_record.rb', line 19
def self.serialize_symbolized_hash_array(*keys)
keys.each do |field|
serialize field
define_method(field) { self[field]&.map { |it| it.symbolize_keys } }
end
end
|
.update_or_create!(attributes) ⇒ Object
92
93
94
95
96
|
# File 'app/models/application_record.rb', line 92
def self.update_or_create!(attributes)
obj = first || new
obj.update!(attributes)
obj
end
|
.whitelist_attributes(a_hash, options = {}) ⇒ Object
98
99
100
101
102
|
# File 'app/models/application_record.rb', line 98
def self.whitelist_attributes(a_hash, options={})
attributes = attribute_names
attributes += reflections.keys if options[:relations]
a_hash.with_indifferent_access.slice(*attributes).except(*options[:except])
end
|
Instance Method Details
#delete ⇒ Object
50
51
52
53
54
|
# File 'app/models/application_record.rb', line 50
def delete
super
rescue ActiveRecord::InvalidForeignKey => e
raise_foreign_key_error!
end
|
#destroy! ⇒ Object
41
42
43
44
45
46
47
48
|
# File 'app/models/application_record.rb', line 41
def destroy!
super
rescue ActiveRecord::RecordNotDestroyed => e
errors[:base].last.try { |it| raise ActiveRecord::RecordNotDestroyed.new it }
raise e
rescue ActiveRecord::InvalidForeignKey => e
raise_foreign_key_error!
end
|
#save ⇒ Object
26
27
28
29
30
31
|
# File 'app/models/application_record.rb', line 26
def save(*)
super
rescue => e
self.errors.add :base, e.message
self
end
|
#save_and_notify! ⇒ Object
56
57
58
59
60
|
# File 'app/models/application_record.rb', line 56
def save_and_notify!
save!
notify!
self
end
|
#save_and_notify_changes! ⇒ Object
33
34
35
36
37
38
39
|
# File 'app/models/application_record.rb', line 33
def save_and_notify_changes!
if changed?
save_and_notify!
else
save!
end
end
|
#update_and_notify!(data) ⇒ Object
62
63
64
65
66
|
# File 'app/models/application_record.rb', line 62
def update_and_notify!(data)
update! data
notify!
self
end
|