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
- #audit_action ⇒ Object
-
#auto_strip_attributes ⇒ Object
Automatically strip any leading or trailing whitespace on model attribute values.
- #capture_user_info ⇒ Object
-
#clear_cache ⇒ Object
Clear the cache.
-
#created_by_display_name ⇒ Object
Safely get the display name of who created this object.
-
#delete_and_log(user) ⇒ Object
Record a delete and delete the thingy.
-
#destroy_and_log(user) ⇒ Object
Record a destroy and destroy the thingy.
-
#last_modified_by_display_name ⇒ Object
Safely get the display name of who last modified this object.
-
#log_change(user, changes, action = nil) ⇒ Object
record a change for the object instance.
-
#log_deletion(user, model) ⇒ Object
Log the deletion, capturing the current values of the record before it is removed from the system.
- #remove_blank_secure_fields(params) ⇒ Object
-
#save_and_log(user, options = {}) ⇒ Object
Record a save.
-
#save_and_log!(user, options = {}) ⇒ Object
Record an save.
-
#secure_fields ⇒ Object
List of secure fields, to add fields in concrete class, simply override this method.
-
#update(params) ⇒ Object
(also: #update_attributes)
Remove updates for secure fields that come across as blank to start with and get removed on update.
-
#update!(params) ⇒ Object
(also: #update_attributes!)
Remove updates for secure fields that come across as blank to start with and get removed on update.
-
#update_and_log(user, attributes) ⇒ Object
(also: #update_attributes_and_log)
Record an update.
-
#update_and_log!(user, attributes) ⇒ Object
(also: #update_attributes_and_log!)
Record an update.
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_action ⇒ Object
223 224 225 |
# File 'lib/app/models/concerns/standard_model.rb', line 223 def audit_action new_record? ? AuditLog::CREATE_ACTION : AuditLog::UPDATE_ACTION end |
#auto_strip_attributes ⇒ Object
Automatically strip any leading or trailing whitespace on model attribute values.
344 345 346 347 348 349 350 351 |
# File 'lib/app/models/concerns/standard_model.rb', line 344 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_info ⇒ Object
328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/app/models/concerns/standard_model.rb', line 328 def capture_user_info 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_cache ⇒ Object
Clear the cache
203 204 205 206 207 208 209 210 211 |
# File 'lib/app/models/concerns/standard_model.rb', line 203 def clear_cache Rails.cache.delete_matched "*#{id}*" return unless respond_to?(:account) && account.present? && account.is_a?(Account) Rails.cache.delete_matched "*#{account.id}*" true # Force a return of true so that we don't break the callback chain. rescue StandardError true end |
#created_by_display_name ⇒ Object
Safely get the display name of who created this object
239 240 241 242 243 |
# File 'lib/app/models/concerns/standard_model.rb', line 239 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
312 313 314 315 |
# File 'lib/app/models/concerns/standard_model.rb', line 312 def delete_and_log(user) log_deletion(user, self) delete end |
#destroy_and_log(user) ⇒ Object
Record a destroy and destroy the thingy
304 305 306 307 |
# File 'lib/app/models/concerns/standard_model.rb', line 304 def destroy_and_log(user) log_deletion(user, self) destroy end |
#last_modified_by_display_name ⇒ Object
Safely get the display name of who last modified this object
230 231 232 233 234 |
# File 'lib/app/models/concerns/standard_model.rb', line 230 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
216 217 218 219 220 221 |
# File 'lib/app/models/concerns/standard_model.rb', line 216 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
320 321 322 323 324 325 326 |
# File 'lib/app/models/concerns/standard_model.rb', line 320 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
195 196 197 198 |
# File 'lib/app/models/concerns/standard_model.rb', line 195 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
278 279 280 281 282 283 284 285 286 |
# File 'lib/app/models/concerns/standard_model.rb', line 278 def save_and_log(user, = {}) self.created_by = user if new_record? self.last_modified_by = user model_changes = changes action = audit_action result = save() log_change(user, model_changes, action) if valid? result end |
#save_and_log!(user, options = {}) ⇒ Object
Record an save
291 292 293 294 295 296 297 298 299 |
# File 'lib/app/models/concerns/standard_model.rb', line 291 def save_and_log!(user, = {}) self.created_by = user if new_record? self.last_modified_by = user model_changes = changes action = audit_action result = save!() log_change(user, model_changes, action) result end |
#secure_fields ⇒ Object
List of secure fields, to add fields in concrete class, simply override this method
191 192 193 |
# File 'lib/app/models/concerns/standard_model.rb', line 191 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
173 174 175 |
# File 'lib/app/models/concerns/standard_model.rb', line 173 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
182 183 184 |
# File 'lib/app/models/concerns/standard_model.rb', line 182 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
248 249 250 251 252 253 254 255 256 |
# File 'lib/app/models/concerns/standard_model.rb', line 248 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
263 264 265 266 267 268 269 270 271 |
# File 'lib/app/models/concerns/standard_model.rb', line 263 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 |