Module: Kushojin::ModelMethods::Callback::ClassMethods

Defined in:
lib/kushojin/model_methods/callback.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



7
8
9
# File 'lib/kushojin/model_methods/callback.rb', line 7

def self.extended(klass)
  klass.class_attribute :kushojin_callbacks, instance_accessor: false
end

Instance Method Details

#record_changes(callbacks = nil, only: []) ⇒ Object

Record changes of the ActiveRecord model.

class User < ApplicationRecord
  record_changes
end

You can pass in a class or an instance to change behaviors of the callbacks.

class CustomCallbacks
  # Must be able to respond to after_create, after_update, and after_destroy.
  def after_create(record); end
  def after_update(record); end
  def after_destroy(record); end
end

class User < ApplicationRecord
  record_changes CustomCallbacks.new
end

Changes is recorded when the model is created, updated and destroyed. The :only option can be used same as filters of controller.

record_changes only: [:create, :destroy]
Options
  • only - Records only for this event. Support event is :create, :update, and :destroy.



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kushojin/model_methods/callback.rb', line 40

def record_changes(callbacks = nil, only: [])
  if kushojin_callbacks
    remove_callbacks
    self.kushojin_callbacks = callbacks if callbacks
  else
    self.kushojin_callbacks = callbacks || RecordChangesCallbacks.new
  end

  record_events = convert_to_record_events(only)
  record_events.each do |event|
    public_send(event, kushojin_callbacks)
  end
end