Class: Heron::Factories::Event

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/models/heron/factories/event.rb

Overview

Factory class to create Events (for the moment only PlateCherrypicked)

Constant Summary collapse

EVENT_CLASSES =
{ BroadcastEvent::PlateCherrypicked::EVENT_TYPE.to_s => BroadcastEvent::PlateCherrypicked }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(params, seed) ⇒ Event

Returns a new instance of Event.



12
13
14
15
# File 'app/models/heron/factories/event.rb', line 12

def initialize(params, seed)
  @params = params
  @seed = seed
end

Instance Method Details

#broadcast_eventObject



17
18
19
20
21
# File 'app/models/heron/factories/event.rb', line 17

def broadcast_event
  return unless event_class

  @broadcast_event ||= event_class.new(seed: @seed, properties: @params[:event])
end

#check_broadcast_eventObject



27
28
29
30
31
32
33
34
# File 'app/models/heron/factories/event.rb', line 27

def check_broadcast_event
  return if errors.count.positive?
  return unless broadcast_event
  return if broadcast_event.valid?

  # In Rails 6.1 object.errors returns ActiveModel::Errors, in Rails 6.0 it returns a Hash
  broadcast_event.errors.each { |error| errors.add error.attribute, error.message }
end

#event_classObject



23
24
25
# File 'app/models/heron/factories/event.rb', line 23

def event_class
  EVENT_CLASSES[@params.dig(:event, :event_type)]
end

#saveObject



36
37
38
39
40
41
42
# File 'app/models/heron/factories/event.rb', line 36

def save
  return false unless valid?
  return false unless broadcast_event

  ActiveRecord::Base.transaction { broadcast_event.save }
  true
end