Module: StateTracking::ClassMethods

Defined in:
lib/state_tracking.rb

Instance Method Summary collapse

Instance Method Details

#_override_aasm_eventsObject



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/state_tracking.rb', line 99

def _override_aasm_events
  aasm.events.each do |name, *args|
    ["#{name}!", "#{name}"].each { |meth|
      previous = instance_method(meth)

      define_method(meth) { |*args, &block|
        _state_tracking_stash_meta(args.last)

        previous.bind(self).(nil, *args, &block)
      }
    }
  end
end

#state_tracking(options = {}, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/state_tracking.rb', line 18

def state_tracking(options={}, &block)
  # We're relying on the fact that the user is not able to set the
  # skip_validation_on_save configuration value to true.
  #
  # If that's set, then the state_tracking column won't be updated.
  aasm(column: :state, &block)

  aasm.states.each do |state|
    callbacks = state.options[:enter] ||= []

    unless callbacks.is_a?(Array)
      callbacks = state.options[:enter] = [callbacks]
    end

    # Need to make sure we don't add multiple Procs. For example, if the
    # classes are reloaded in a development environment.
    #
    # Note: Using is_a? doesn't work. It doesn't recognize previously added
    # StateTracking::StateTrackingProc as actually being instances of
    # StateTracking::StateTrackingProc, so I have to compare by name.
    unless callbacks.any?{|c| c.class.name == 'StateTracking::StateTrackingProc'}
      callbacks << StateTrackingProc.new { |record|
        record.send(:_state_tracking_record_state_change, state.name)
      }
    end
  end

  _override_aasm_events

  # This is required because the instance methods need to override
  # the instance methods that AASM adds.  If the instance methods
  # are simply defined within the StateTracking module, then they
  # get defined first, and then the AASM methods override the
  # StateTracking methods, which is the opposite of what we want.
  self.send(:include, InstanceMethods)

  if options[:fire_events]
    @_state_tracking_fire_states = options[:fire_events] if options[:fire_events].is_a?(Array)

    self.class_eval do
      # Note: We're defining instance methods here.
      after_commit :_state_tracking_fire_events

      def _state_tracking_fire_states
        self.class.instance_variable_get(:@_state_tracking_fire_states)
      end

      def _state_tracking_fire_events
        (@_state_tracking_events_to_fire || []).each do |state|
          next if _state_tracking_fire_states &&
            !_state_tracking_fire_states.include?(state.to_sym)

          primary_key = self.class.primary_key
          name_underscored = self.class.name.underscore
          pkey_value = send(primary_key)
          event_name = "#{name_underscored}_#{state}"

          unless pkey_value
            raise "Can't publish event (#{event_name.inspect}). " \
                  "Primary key (#{primary_key.inspect}) for " \
                  "#{self.class.name.inspect} is nil."
          end

          params = {
            "#{name_underscored}_#{primary_key}" => pkey_value
          }

          Announcer.publish(event_name, params)
        end

        @_state_tracking_events_to_fire = nil
      end

      def _state_tracking_record_state_change(state)
        super
        (@_state_tracking_events_to_fire ||= []) << state.to_s
      end
    end
  end
end