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