Module: StandardModel

Extended by:
ActiveSupport::Concern
Includes:
App47Logger, Mongoid::Document, Mongoid::Timestamps
Included in:
ApiToken, AuditLog, CommandJob, CommandJobLog, Cron::Server, Cron::Tab, Delayed::Jobs::Metric, Delayed::Jobs::Run, Delayed::Jobs::Worker, Notification, SmtpConfiguration, Template
Defined in:
lib/app/models/concerns/standard_model.rb

Overview

Mixin to standardize the setup of all models, i.e., the standard or base model definitions they should all have

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Methods included from App47Logger

clean_params, #clean_params, delete_parameter_keys, #log_controller_error, log_debug, #log_debug, log_error, #log_error, log_exception, #log_message, log_message, #log_warn, log_warn, mask_parameter_keys, #update_flash_messages

Class Method Details

.included(base) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/app/models/concerns/standard_model.rb', line 13

def self.included(base)
  base.class_eval do
    #
    # Fields
    #
    field :last_modified_by_email, type: String
    field :last_modified_by_name, type: String
    field :created_by_email, type: String
    field :created_by_name, type: String
    #
    # Relationships
    #
    belongs_to :last_modified_by, polymorphic: true, optional: true
    belongs_to :created_by, polymorphic: true, optional: true
    has_many Web47core::Config.audit_model_log_symbol, dependent: :nullify, inverse_of: :model
    #
    # Callbacks
    #
    after_save :clear_cache
    before_destroy :clear_cache
    before_save :capture_user_info
  end
end

Instance Method Details

#audit_actionObject



219
220
221
# File 'lib/app/models/concerns/standard_model.rb', line 219

def audit_action
  new_record? ? AuditLog::CREATE_ACTION : AuditLog::UPDATE_ACTION
end

#auto_strip_attributesObject

Automatically strip any leading or trailing whitespace on model attribute values.



340
341
342
343
344
345
346
347
# File 'lib/app/models/concerns/standard_model.rb', line 340

def auto_strip_attributes
  # Iterate through all attributes and strip those which respond
  # to the method
  attribute_names.each do |name|
    attr = send(name)
    send("#{name}=", attr.strip) if attr.respond_to?(:strip)
  end
end

#capture_user_infoObject



324
325
326
327
328
329
330
331
332
333
334
# File 'lib/app/models/concerns/standard_model.rb', line 324

def 
  auto_strip_attributes
  if last_modified_by.present?
    self.last_modified_by_name = last_modified_by.name
    self.last_modified_by_email = last_modified_by.email
  end
  return unless created_by.present? && new_record?

  self.created_by_name = created_by.name
  self.created_by_email = created_by.email
end

#clear_cacheObject

Clear the cache



199
200
201
202
203
204
205
206
207
# File 'lib/app/models/concerns/standard_model.rb', line 199

def clear_cache
  Rails.cache.delete_matched "*#{id}*"
  return unless respond_to?(:account) && .present? && .is_a?(Account)

  Rails.cache.delete_matched "*#{.id}*"
  true # Force a return of true so that we don't break the callback chain.
rescue StandardError
  true
end

#created_by_display_nameObject

Safely get the display name of who created this object



235
236
237
238
239
# File 'lib/app/models/concerns/standard_model.rb', line 235

def created_by_display_name
  created_by.display_name
rescue StandardError
  "#{created_by_name} (#{created_by_email}) - deleted"
end

#delete_and_log(user) ⇒ Object

Record a delete and delete the thingy



308
309
310
311
# File 'lib/app/models/concerns/standard_model.rb', line 308

def delete_and_log(user)
  log_deletion(user, self)
  delete
end

#destroy_and_log(user) ⇒ Object

Record a destroy and destroy the thingy



300
301
302
303
# File 'lib/app/models/concerns/standard_model.rb', line 300

def destroy_and_log(user)
  log_deletion(user, self)
  destroy
end

#last_modified_by_display_nameObject

Safely get the display name of who last modified this object



226
227
228
229
230
# File 'lib/app/models/concerns/standard_model.rb', line 226

def last_modified_by_display_name
  last_modified_by.display_name
rescue StandardError
  "#{last_modified_by_name} (#{last_modified_by_email}) - deleted"
end

#log_change(user, changes, action = nil) ⇒ Object

record a change for the object instance



212
213
214
215
216
217
# File 'lib/app/models/concerns/standard_model.rb', line 212

def log_change(user, changes, action = nil)
  Web47core::Config.audit_model_log_class.create!(Web47core::Config.audit_model => user,
                                                  model: self,
                                                  action: action,
                                                  changed_values: App47Logger.clean_params(changes).to_json)
end

#log_deletion(user, model) ⇒ Object

Log the deletion, capturing the current values of the record before it is removed from the system



316
317
318
319
320
321
322
# File 'lib/app/models/concerns/standard_model.rb', line 316

def log_deletion(user, model)
  Web47core::Config.audit_model_log_class.create!(Web47core::Config.audit_model => user,
                                                  deleted_oid: model.id,
                                                  model: model,
                                                  action: AuditLog::DELETE_ACTION,
                                                  changed_values: App47Logger.clean_params(attributes).to_json)
end

#remove_blank_secure_fields(params) ⇒ Object



191
192
193
194
# File 'lib/app/models/concerns/standard_model.rb', line 191

def remove_blank_secure_fields(params)
  secure_fields.each { |field| params.delete(field) if params[field].blank? }
  params
end

#save_and_log(user, options = {}) ⇒ Object

Record a save



274
275
276
277
278
279
280
281
282
# File 'lib/app/models/concerns/standard_model.rb', line 274

def save_and_log(user, options = {})
  self.created_by = user if new_record?
  self.last_modified_by = user
  model_changes = changes
  action = audit_action
  result = save(options)
  log_change(user, model_changes, action) if valid?
  result
end

#save_and_log!(user, options = {}) ⇒ Object

Record an save



287
288
289
290
291
292
293
294
295
# File 'lib/app/models/concerns/standard_model.rb', line 287

def save_and_log!(user, options = {})
  self.created_by = user if new_record?
  self.last_modified_by = user
  model_changes = changes
  action = audit_action
  result = save!(options)
  log_change(user, model_changes, action)
  result
end

#secure_fieldsObject

List of secure fields, to add fields in concrete class, simply override this method



187
188
189
# File 'lib/app/models/concerns/standard_model.rb', line 187

def secure_fields
  []
end

#update(params) ⇒ Object Also known as: update_attributes

Remove updates for secure fields that come across as blank to start with and get removed on update



169
170
171
# File 'lib/app/models/concerns/standard_model.rb', line 169

def update(params)
  super(remove_blank_secure_fields(params))
end

#update!(params) ⇒ Object Also known as: update_attributes!

Remove updates for secure fields that come across as blank to start with and get removed on update



178
179
180
# File 'lib/app/models/concerns/standard_model.rb', line 178

def update!(params)
  super(remove_blank_secure_fields(params))
end

#update_and_log(user, attributes) ⇒ Object Also known as: update_attributes_and_log

Record an update



244
245
246
247
248
249
250
251
252
# File 'lib/app/models/concerns/standard_model.rb', line 244

def update_and_log(user, attributes)
  return self if attributes.blank?

  action = audit_action
  self.last_modified_by = user
  result = update(attributes)
  log_change(user, attributes, action) if valid?
  result
end

#update_and_log!(user, attributes) ⇒ Object Also known as: update_attributes_and_log!

Record an update



259
260
261
262
263
264
265
266
267
# File 'lib/app/models/concerns/standard_model.rb', line 259

def update_and_log!(user, attributes)
  return self if attributes.blank?

  action = audit_action
  self.last_modified_by = user
  result = update!(attributes)
  log_change(user, attributes, action)
  result
end