Sha256: 672ebf9db1a530e345924f4d0d3abe62ef03e5c2031a957b1ed277661d3351d5

Contents?: true

Size: 1.1 KB

Versions: 9

Compression:

Stored size: 1.1 KB

Contents

module ESA
  module Associations
    module EventsExtension
      # This function adds an event only if there were no previous events,
      # or the previous event was a different one, avoiding duplicates.
      def maybe(attrs)
        events = proxy_association.owner.esa_events
        last_event = events.order('time DESC, created_at DESC').first

        # make sure that the previous event was not of the same event type
        if last_event.nil? or last_event.nature != attrs[:nature]
          if attrs[:time].present? and last_event.present? and last_event.time.present? and last_event.time > attrs[:time]
            # we cannot input past events, so let's make it most recent
            attrs[:time] = last_event.time
            e = events.new(attrs)
          else
            e = events.new(attrs)
          end
          e.save
        else
          # we didn't need to add anything
          true
        end
      end

      def hashes
        proxy_association.owner.esa_events.
            map{|e| {:nature => e.nature, :time => e.time}}.
            sort_by{|e| e[:time]}
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
event_sourced_accounting-0.2.6 app/models/esa/associations/events_extension.rb
event_sourced_accounting-0.2.4 app/models/esa/associations/events_extension.rb
event_sourced_accounting-0.2.3 app/models/esa/associations/events_extension.rb
event_sourced_accounting-0.2.2 app/models/esa/associations/events_extension.rb
event_sourced_accounting-0.1.6 app/models/esa/associations/events_extension.rb
event_sourced_accounting-0.1.4 app/models/esa/associations/events_extension.rb
event_sourced_accounting-0.1.3 app/models/esa/associations/events_extension.rb
event_sourced_accounting-0.1.1 app/models/esa/associations/events_extension.rb
event_sourced_accounting-0.1.0 app/models/esa/associations/events_extension.rb