Class: ApplicationRecord
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- ApplicationRecord
show all
- Defined in:
- app/models/application_record.rb
Direct Known Subclasses
ApiClient, Avatar, Certificate, CertificateProgram, Chapter, Complement, Content, Course, Discussion, Exam, ExamAuthorization, ExamAuthorizationRequest, ExamRegistration, Exercise, Invitation, Language, Lesson, Medal, Message, Notification, Organization, Progress, Subscription, Term, Upvote, Usage, User, UserStats
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.active_between(start_date_field, end_date_field, **options) ⇒ Object
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'app/models/application_record.rb', line 137
def self.active_between(start_date_field, end_date_field, **options)
define_singleton_method(:active) do |actually_filter=true|
if actually_filter
self.where("(#{start_date_field} IS NULL OR #{start_date_field} < :now) AND (#{end_date_field} IS NULL OR #{end_date_field} > :now)", now: Time.now)
else
all
end
end
aliased_as = options.delete(:aliased_as)
singleton_class.send(:alias_method, aliased_as, :active) if aliased_as
end
|
.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
|
.enum_prefixed_translations_for(selector) ⇒ Object
161
162
163
164
165
|
# File 'app/models/application_record.rb', line 161
def self.enum_prefixed_translations_for(selector)
send(selector.to_s.pluralize).map do |key, _|
[I18n.t("#{selector}_#{key}", default: key.to_sym), key]
end
end
|
.numbered(*associations) ⇒ Object
106
107
108
109
110
111
112
113
114
115
|
# File 'app/models/application_record.rb', line 106
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
129
130
131
132
133
134
135
|
# File 'app/models/application_record.rb', line 129
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
153
154
155
156
157
158
159
|
# File 'app/models/application_record.rb', line 153
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.deep_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
117
118
119
120
121
|
# File 'app/models/application_record.rb', line 117
def self.update_or_create!(attributes)
obj = first || new
obj.update!(attributes)
obj
end
|
.whitelist_attributes(a_hash, options = {}) ⇒ Object
123
124
125
126
127
|
# File 'app/models/application_record.rb', line 123
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
|
.with_temporary_token(field_name, duration = 2.hours) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'app/models/application_record.rb', line 90
def self.with_temporary_token(field_name, duration = 2.hours)
class_eval do
token_attribute = field_name
token_date_attribute = "#{field_name}_expiration_date"
define_method("generate_#{field_name}!") do
update!(token_attribute => self.class.generate_secure_token, token_date_attribute => duration.from_now)
end
define_method("#{field_name}_matches?") do |token|
actual_token = attribute(token_attribute)
actual_token.present? && token == actual_token && attribute(token_date_attribute)&.future?
end
end
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
|