Module: StandardModel

Extended by:
ActiveSupport::Concern
Includes:
App47Logger, Mongoid::Document, Mongoid::Timestamps
Included in:
AuditLog, Cron::Server, Cron::Tab, 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



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

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.



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

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



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

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



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

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



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

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



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

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

#destroy_and_log(user) ⇒ Object

Record a destroy and destroy the thingy



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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