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
    field :last_modified_by_name
    field :created_by_email
    field :created_by_name
    #
    # Relationships
    #
    belongs_to :last_modified_by, class_name: 'User', optional: true
    belongs_to :created_by, class_name: 'User', optional: true
    has_many Web47core::Config.user_audit_model_log_symbol, dependent: :nullify
    #
    # Callbacks
    #
    after_save :clear_cache
    before_destroy :clear_cache
    before_save :capture_user_info
  end
end

Instance Method Details

#audit_actionObject



205
206
207
# File 'lib/app/models/concerns/standard_model.rb', line 205

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.



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

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



309
310
311
312
313
314
315
316
317
318
319
# File 'lib/app/models/concerns/standard_model.rb', line 309

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



185
186
187
188
189
190
191
192
193
# File 'lib/app/models/concerns/standard_model.rb', line 185

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
  false
end

#created_by_display_nameObject

Safely get the display name of who created this object



221
222
223
224
225
# File 'lib/app/models/concerns/standard_model.rb', line 221

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

#delete_and_log(user) ⇒ Object

Record a delete and delete the thingy



294
295
296
297
# File 'lib/app/models/concerns/standard_model.rb', line 294

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

#destroy_and_log(user) ⇒ Object

Record a destroy and destroy the thingy



286
287
288
289
# File 'lib/app/models/concerns/standard_model.rb', line 286

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



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

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

#log_change(user, changes, action) ⇒ Object

record a change for the object instance



198
199
200
201
202
203
# File 'lib/app/models/concerns/standard_model.rb', line 198

def log_change(user, changes, action)
  Web47core::Config.user_audit_model_log_class.create!(Web47core::Config.user_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



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

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

#remove_blank_secure_fields(params) ⇒ Object



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

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



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

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



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

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



173
174
175
# File 'lib/app/models/concerns/standard_model.rb', line 173

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



155
156
157
# File 'lib/app/models/concerns/standard_model.rb', line 155

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



164
165
166
# File 'lib/app/models/concerns/standard_model.rb', line 164

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



230
231
232
233
234
235
236
237
238
# File 'lib/app/models/concerns/standard_model.rb', line 230

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

  action = audit_action
  attributes[:last_modified_by_id] = user.id
  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



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
  attributes[:last_modified_by_id] = user.id
  result = update!(attributes)
  log_change(user, attributes, action)
  result
end