Module: EventfulRecord

Included in:
Asset, Batch, Item, Item, Project, Project, Request, Sample, Study, Study, User
Defined in:
lib/eventful_record.rb

Overview

rubocop:todo Style/Documentation

Instance Method Summary collapse

Instance Method Details

#has_many_events(&block) ⇒ Object

rubocop:todo Metrics/MethodLength


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/eventful_record.rb', line 2

def has_many_events(&block) # rubocop:todo Metrics/MethodLength
  has_many(:events, -> { order(created_at: :asc, id: :asc) }, as: :eventful, dependent: :destroy) do
    def self.event_constructor(name, event_class, event_class_method)
      line = __LINE__ + 1
      class_eval(
        "
        def #{name}(*args)
          #{event_class.name}.#{event_class_method}(self.proxy_association.owner, *args).tap { |event| self << event unless event.eventful.present? }
        end
      ",
        __FILE__,
        line
      )
    end

    class_eval(&block) if block.present?
  end
end

#has_many_lab_events(&block) ⇒ Object


21
22
23
# File 'lib/eventful_record.rb', line 21

def has_many_lab_events(&block)
  has_many(:lab_events, -> { order(created_at: :asc, id: :asc) }, as: :eventful, dependent: :destroy, &block)
end

#has_one_event_with_family(event_family, &block) ⇒ Object


25
26
27
28
29
30
31
32
33
# File 'lib/eventful_record.rb', line 25

def has_one_event_with_family(event_family, &block)
  has_one(
    :"#{event_family}_event",
    lambda { order(id: :desc).where(family: event_family) },
    class_name: 'Event',
    as: :eventful,
    &block
  )
end