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


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

def has_many_events(&block)
  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


17
18
19
# File 'lib/eventful_record.rb', line 17

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


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

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