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, Medal, Message, Organization, Progress, Subscription, Term, Upvote, Usage, User, UserStats
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.aggregate_of(association) ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'app/models/application_record.rb', line 77
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
21
22
23
24
25
26
27
|
# File 'app/models/application_record.rb', line 21
def self.all_except(others)
if others.present?
where.not(id: [others.map(&:id)])
else
all
end
end
|
.defaults(&block) ⇒ Object
16
17
18
19
|
# File 'app/models/application_record.rb', line 16
def self.defaults(&block)
after_initialize :defaults, if: :new_record?
define_method :defaults, &block
end
|
.numbered(*associations) ⇒ Object
90
91
92
93
94
95
96
97
98
99
|
# File 'app/models/application_record.rb', line 90
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
113
114
115
116
117
118
119
|
# File 'app/models/application_record.rb', line 113
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
124
125
126
127
128
129
130
|
# File 'app/models/application_record.rb', line 124
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
29
30
31
32
33
34
|
# File 'app/models/application_record.rb', line 29
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
|
.teaser_on(*args) ⇒ Object
6
7
8
9
10
11
12
13
14
|
# File 'app/models/application_record.rb', line 6
def self.teaser_on(*args)
args.each do |selector|
teaser_selector = "#{selector}_teaser"
define_method teaser_selector do
send(selector)&.markdown_paragraphs&.first
end
markdown_on teaser_selector, skip_sanitization: true
end
end
|
.update_or_create!(attributes) ⇒ Object
101
102
103
104
105
|
# File 'app/models/application_record.rb', line 101
def self.update_or_create!(attributes)
obj = first || new
obj.update!(attributes)
obj
end
|
.whitelist_attributes(a_hash, options = {}) ⇒ Object
107
108
109
110
111
|
# File 'app/models/application_record.rb', line 107
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
60
61
62
63
64
|
# File 'app/models/application_record.rb', line 60
def delete
super
rescue ActiveRecord::InvalidForeignKey => e
raise_foreign_key_error!
end
|
#destroy! ⇒ Object
51
52
53
54
55
56
57
58
|
# File 'app/models/application_record.rb', line 51
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
36
37
38
39
40
41
|
# File 'app/models/application_record.rb', line 36
def save(*)
super
rescue => e
self.errors.add :base, e.message
self
end
|
#save_and_notify! ⇒ Object
66
67
68
69
70
|
# File 'app/models/application_record.rb', line 66
def save_and_notify!
save!
notify!
self
end
|
#save_and_notify_changes! ⇒ Object
43
44
45
46
47
48
49
|
# File 'app/models/application_record.rb', line 43
def save_and_notify_changes!
if changed?
save_and_notify!
else
save!
end
end
|
#update_and_notify!(data) ⇒ Object
72
73
74
75
|
# File 'app/models/application_record.rb', line 72
def update_and_notify!(data)
assign_attributes data
save_and_notify!
end
|