Module: EventfulRecord

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

Instance Method Summary collapse

Instance Method Details

#has_many_events(&block) ⇒ Object

rubocop:todo Metrics/MethodLength



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

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

      # rubocop:todo Layout/LineLength
      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
      )
      # rubocop:enable Layout/LineLength
    end

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

#has_many_lab_eventsObject



25
26
27
# File 'lib/eventful_record.rb', line 25

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

#has_one_event_with_family(event_family) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/eventful_record.rb', line 29

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