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
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'app/models/application_record.rb', line 52
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
17
18
19
20
21
22
23
|
# File 'app/models/application_record.rb', line 17
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
|
.name_model_as(other) ⇒ Object
11
12
13
14
15
|
# File 'app/models/application_record.rb', line 11
def self.name_model_as(other)
define_singleton_method :model_name do
other.model_name
end
end
|
.numbered(*associations) ⇒ Object
65
66
67
68
69
70
71
72
73
74
|
# File 'app/models/application_record.rb', line 65
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
|
.update_or_create!(attributes) ⇒ Object
76
77
78
79
80
|
# File 'app/models/application_record.rb', line 76
def self.update_or_create!(attributes)
obj = first || new
obj.update!(attributes)
obj
end
|
.whitelist_attributes(a_hash, options = {}) ⇒ Object
82
83
84
|
# File 'app/models/application_record.rb', line 82
def self.whitelist_attributes(a_hash, options={})
a_hash.slice(*attribute_names).except(*options[:except])
end
|
Instance Method Details
#save ⇒ Object
25
26
27
28
29
30
|
# File 'app/models/application_record.rb', line 25
def save(*)
super
rescue => e
self.errors.add :base, e.message
self
end
|
#save_and_notify! ⇒ Object
40
41
42
43
44
|
# File 'app/models/application_record.rb', line 40
def save_and_notify!
save!
notify!
self
end
|
#save_and_notify_changes! ⇒ Object
32
33
34
35
36
37
38
|
# File 'app/models/application_record.rb', line 32
def save_and_notify_changes!
if changed?
save_and_notify!
else
save!
end
end
|
#update_and_notify!(data) ⇒ Object
46
47
48
49
50
|
# File 'app/models/application_record.rb', line 46
def update_and_notify!(data)
update! data
notify!
self
end
|