Move to_event method into a concern
This commit is contained in:
parent
64f1b5fa42
commit
f55a676bbf
6 changed files with 82 additions and 35 deletions
29
app/models/concerns/object_event/model_extensions.rb
Normal file
29
app/models/concerns/object_event/model_extensions.rb
Normal file
|
@ -0,0 +1,29 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
||||
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
||||
module ObjectEvent::ModelExtensions
|
||||
extend ActiveSupport::Concern
|
||||
class_methods do
|
||||
# Adds the to_event method to a model. Requires `to_builder` method for creating
|
||||
# the Jbuilder object
|
||||
def object_eventable
|
||||
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
||||
def to_event(event_type, *expand)
|
||||
Jbuilder.new do |event|
|
||||
event.id SecureRandom.uuid
|
||||
event.object 'object_event'
|
||||
event.type event_type
|
||||
event.data do
|
||||
event.object to_builder(*expand)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def to_builder(*expand)
|
||||
raise NotImplementedError.new("to_builder must be implemented in your model")
|
||||
end
|
||||
RUBY
|
||||
end
|
||||
end
|
||||
end
|
|
@ -3,6 +3,8 @@
|
|||
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
||||
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
||||
class EventDiscount < ApplicationRecord
|
||||
include ObjectEvent::ModelExtensions
|
||||
object_eventable
|
||||
# :code,
|
||||
# :event_id,
|
||||
# :name,
|
||||
|
@ -70,15 +72,4 @@ class EventDiscount < ApplicationRecord
|
|||
def publish_delete
|
||||
Houdini.event_publisher.announce(:event_discount_deleted, to_event('event_discount.deleted', :event, :nonprofit, :ticket_levels).attributes!)
|
||||
end
|
||||
|
||||
def to_event(event_type, *expand)
|
||||
Jbuilder.new do |event|
|
||||
event.id SecureRandom.uuid
|
||||
event.object 'object_event'
|
||||
event.type event_type
|
||||
event.data do
|
||||
event.object to_builder(*expand)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -11,4 +11,7 @@ class SupporterNote < ApplicationRecord
|
|||
|
||||
validates :content, length: { minimum: 1 }
|
||||
validates :supporter_id, presence: true
|
||||
|
||||
|
||||
|
||||
end
|
||||
|
|
|
@ -3,7 +3,8 @@
|
|||
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
||||
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
||||
class TagMaster < ApplicationRecord
|
||||
|
||||
include ObjectEvent::ModelExtensions
|
||||
object_eventable
|
||||
# TODO replace with Discard gem
|
||||
define_model_callbacks :discard
|
||||
|
||||
|
@ -60,16 +61,4 @@ private
|
|||
def publish_delete
|
||||
Houdini.event_publisher.announce(:tag_master_deleted, to_event('tag_master.deleted', :nonprofit).attributes!)
|
||||
end
|
||||
|
||||
|
||||
def to_event(event_type, *expand)
|
||||
Jbuilder.new do |event|
|
||||
event.id SecureRandom.uuid
|
||||
event.object 'object_event'
|
||||
event.type event_type
|
||||
event.data do
|
||||
event.object to_builder(*expand)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
||||
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
||||
class TicketLevel < ApplicationRecord
|
||||
include ObjectEvent::ModelExtensions
|
||||
object_eventable
|
||||
# :amount, #integer
|
||||
# :amount_dollars, #accessor, string
|
||||
# :name, #string
|
||||
|
@ -97,15 +99,4 @@ class TicketLevel < ApplicationRecord
|
|||
def publish_delete
|
||||
Houdini.event_publisher.announce(:ticket_level_deleted, to_event('ticket_level.deleted', :event, :nonprofit, :event_discounts).attributes!)
|
||||
end
|
||||
|
||||
def to_event(event_type, *expand)
|
||||
Jbuilder.new do |event|
|
||||
event.id SecureRandom.uuid
|
||||
event.object 'object_event'
|
||||
event.type event_type
|
||||
event.data do
|
||||
event.object to_builder(*expand)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
44
spec/models/concerns/object_event/model_extensions_spec.rb
Normal file
44
spec/models/concerns/object_event/model_extensions_spec.rb
Normal file
|
@ -0,0 +1,44 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
||||
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe ObjectEvent::ModelExtensions do
|
||||
|
||||
let(:event_type) {'model.event_name'}
|
||||
class ClassWithoutToBuilder
|
||||
include ObjectEvent::ModelExtensions
|
||||
object_eventable
|
||||
end
|
||||
|
||||
class ClassWithToBuilder
|
||||
include ObjectEvent::ModelExtensions
|
||||
object_eventable
|
||||
|
||||
def to_builder(*expand)
|
||||
Jbuilder.new do |json|
|
||||
json.id 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it 'raises NotImplementedError when no to_builder is defined by developer' do
|
||||
obj = ClassWithoutToBuilder.new
|
||||
expect { obj.to_event event_type}.to raise_error(NotImplementedError)
|
||||
end
|
||||
|
||||
it 'returns an proper event when to_builder is defined by developer' do
|
||||
obj = ClassWithToBuilder.new
|
||||
expect(obj.to_event event_type).to eq({
|
||||
'id' => kind_of(String),
|
||||
'object' => 'object_event',
|
||||
'type' => event_type,
|
||||
'data' => {
|
||||
'object' => {
|
||||
'id' => 1
|
||||
}
|
||||
}
|
||||
})
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue