Module: ESA::Associations::EventsExtension

Defined in:
app/models/esa/associations/events_extension.rb

Instance Method Summary collapse

Instance Method Details

#hashesObject



26
27
28
29
30
# File 'app/models/esa/associations/events_extension.rb', line 26

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

#maybe(attrs) ⇒ Object

This function adds an event only if there were no previous events, or the previous event was a different one, avoiding duplicates.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/esa/associations/events_extension.rb', line 6

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