Class: ApplicationRecord
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- ApplicationRecord
show all
- Defined in:
- app/models/application_record.rb
Direct Known Subclasses
ApiClient, Assignment, Chapter, Complement, Content, Course, Discussion, Exam, ExamAuthorization, Exercise, Invitation, Language, Lesson, Message, Organization, Subscription, Upvote, Usage, User
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.aggregate_of(association) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'app/models/application_record.rb', line 53
def self.aggregate_of(association)
class_eval do
define_method(:rebuild!) do |children|
transaction do
self.send(association).all_except(children).delete_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
66
67
68
69
70
71
72
73
74
75
|
# File 'app/models/application_record.rb', line 66
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
|
.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
77
78
79
80
81
|
# File 'app/models/application_record.rb', line 77
def self.update_or_create!(attributes)
obj = first || new
obj.update!(attributes)
obj
end
|
.whitelist_attributes(a_hash, options = {}) ⇒ Object
83
84
85
86
87
|
# File 'app/models/application_record.rb', line 83
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
#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
41
42
43
44
45
|
# File 'app/models/application_record.rb', line 41
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
47
48
49
50
51
|
# File 'app/models/application_record.rb', line 47
def update_and_notify!(data)
update! data
notify!
self
end
|