From e806d8764f81dc831ce70ddbb5a54625a243efc9 Mon Sep 17 00:00:00 2001 From: Eric Schultz Date: Thu, 21 Jan 2021 16:15:24 -0600 Subject: [PATCH] Create HoudIDs for stringy ids --- app/models/campaign_gift_option.rb | 2 +- .../concerns/object_event/model_extensions.rb | 11 ++++++-- app/models/event_discount.rb | 2 +- app/models/supporter.rb | 2 +- app/models/supporter_note.rb | 2 +- app/models/tag_master.rb | 2 +- app/models/ticket_level.rb | 2 +- .../Nonprofit/Transaction.ts | 4 +-- docs/event_definitions/common.ts | 10 ++++---- spec/models/campaign_gift_option_spec.rb | 12 ++++----- .../object_event/model_extensions_spec.rb | 25 ++++++++++++++++--- spec/models/event_discount_spec.rb | 6 ++--- spec/models/supporter_note_spec.rb | 6 ++--- spec/models/supporter_spec.rb | 13 +++++----- spec/models/tag_master_spec.rb | 4 +-- spec/models/ticket_level_spec.rb | 12 ++++----- 16 files changed, 71 insertions(+), 44 deletions(-) diff --git a/app/models/campaign_gift_option.rb b/app/models/campaign_gift_option.rb index 2a601ff5..0d2e1684 100644 --- a/app/models/campaign_gift_option.rb +++ b/app/models/campaign_gift_option.rb @@ -4,7 +4,7 @@ # Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE class CampaignGiftOption < ApplicationRecord include ObjectEvent::ModelExtensions - object_eventable + object_eventable :cgo # :amount_one_time, #int (cents) # :amount_recurring, #int (cents) # :amount_dollars, #str, gets converted to amount diff --git a/app/models/concerns/object_event/model_extensions.rb b/app/models/concerns/object_event/model_extensions.rb index 1dedd9db..9613a397 100644 --- a/app/models/concerns/object_event/model_extensions.rb +++ b/app/models/concerns/object_event/model_extensions.rb @@ -4,14 +4,21 @@ # 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 + def object_eventable(prefix) class_eval <<-RUBY, __FILE__, __LINE__ + 1 + def object_prefix + :#{prefix.to_s} + end + + def to_event(event_type, *expand) Jbuilder.new do |event| - event.id SecureRandom.uuid + event.id "objevt_" + SecureRandom.alphanumeric(22) event.object 'object_event' event.type event_type event.data do diff --git a/app/models/event_discount.rb b/app/models/event_discount.rb index b22570d4..be54b0d3 100644 --- a/app/models/event_discount.rb +++ b/app/models/event_discount.rb @@ -4,7 +4,7 @@ # Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE class EventDiscount < ApplicationRecord include ObjectEvent::ModelExtensions - object_eventable + object_eventable :evtdisc # :code, # :event_id, # :name, diff --git a/app/models/supporter.rb b/app/models/supporter.rb index 29368f81..008c0abc 100644 --- a/app/models/supporter.rb +++ b/app/models/supporter.rb @@ -180,7 +180,7 @@ class Supporter < ApplicationRecord # we do something custom here since Supporter and SupporterAddress are in the same model def to_event(event_type, *expand) Jbuilder.new do |event| - event.id SecureRandom.uuid + event.id "objevt_" + SecureRandom.alphanumeric(22) event.object 'object_event' event.type event_type event.data do diff --git a/app/models/supporter_note.rb b/app/models/supporter_note.rb index 16463b50..35423b30 100644 --- a/app/models/supporter_note.rb +++ b/app/models/supporter_note.rb @@ -4,7 +4,7 @@ # Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE class SupporterNote < ApplicationRecord include ObjectEvent::ModelExtensions - object_eventable + object_eventable :suppnote # :content, # :supporter_id, :supporter diff --git a/app/models/tag_master.rb b/app/models/tag_master.rb index 72cc2fbb..7c36b650 100644 --- a/app/models/tag_master.rb +++ b/app/models/tag_master.rb @@ -4,7 +4,7 @@ # Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE class TagMaster < ApplicationRecord include ObjectEvent::ModelExtensions - object_eventable + object_eventable :tagmstr # TODO replace with Discard gem define_model_callbacks :discard diff --git a/app/models/ticket_level.rb b/app/models/ticket_level.rb index dbddb64a..5adc2038 100644 --- a/app/models/ticket_level.rb +++ b/app/models/ticket_level.rb @@ -4,7 +4,7 @@ # Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE class TicketLevel < ApplicationRecord include ObjectEvent::ModelExtensions - object_eventable + object_eventable :tktlvl # :amount, #integer # :amount_dollars, #accessor, string # :name, #string diff --git a/docs/event_definitions/Nonprofit/Transaction.ts b/docs/event_definitions/Nonprofit/Transaction.ts index eff774b8..791c003f 100644 --- a/docs/event_definitions/Nonprofit/Transaction.ts +++ b/docs/event_definitions/Nonprofit/Transaction.ts @@ -1,9 +1,9 @@ // License: LGPL-3.0-or-later -import type { Amount, HoudiniObject, IdType, UuidType } from "../common"; +import type { Amount, HoudiniObject, IdType, HoudID } from "../common"; import type Nonprofit from './'; -export interface Transaction extends HoudiniObject { +export interface Transaction extends HoudiniObject { amount: Amount; nonprofit: IdType | Nonprofit; object: 'transaction'; diff --git a/docs/event_definitions/common.ts b/docs/event_definitions/common.ts index c4b2edcc..413a42ac 100644 --- a/docs/event_definitions/common.ts +++ b/docs/event_definitions/common.ts @@ -9,7 +9,7 @@ export type IdType = number; /** * an identifier for HoudiniObjects which is unique among all HoudiniObjects. */ -export type UuidType = string; +export type HoudID = string; /** * Describes a monetary value in the minimum unit for this current. Corresponds to Money class in @@ -60,7 +60,7 @@ export type RecurrenceRule = { * Every object controlled by the Houdini event publisher must meet this standard interface * and will inherit from it. */ -export interface HoudiniObject { +export interface HoudiniObject { /** * An IdType which unique which uniquely identifies this object * from all other similar objects @@ -73,7 +73,7 @@ export interface HoudiniObject { } -type HoudiniObjectOfAllIds = HoudiniObject | HoudiniObject; +type HoudiniObjectOfAllIds = HoudiniObject | HoudiniObject; /** * An event published by Houdini * @@ -90,9 +90,9 @@ export interface HoudiniEvent kind_of(String), + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => 'campaign_gift_option.created', 'data' => { @@ -84,7 +84,7 @@ RSpec.describe CampaignGiftOption, type: :model do it 'announces create for second example' do expect(Houdini.event_publisher).to receive(:announce).with(:campaign_create, anything).ordered expect(Houdini.event_publisher).to receive(:announce).with(:campaign_gift_option_created, { - 'id' => kind_of(String), + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => 'campaign_gift_option.created', 'data' => { @@ -146,7 +146,7 @@ RSpec.describe CampaignGiftOption, type: :model do expect(Houdini.event_publisher).to receive(:announce).with(:campaign_create, anything).ordered expect(Houdini.event_publisher).to receive(:announce).with(:campaign_gift_option_created, anything).ordered expect(Houdini.event_publisher).to receive(:announce).with(:campaign_gift_option_updated, { - 'id' => kind_of(String), + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => 'campaign_gift_option.updated', 'data' => { @@ -191,7 +191,7 @@ RSpec.describe CampaignGiftOption, type: :model do expect(Houdini.event_publisher).to receive(:announce).with(:campaign_create, anything).ordered expect(Houdini.event_publisher).to receive(:announce).with(:campaign_gift_option_created, anything).ordered expect(Houdini.event_publisher).to receive(:announce).with(:campaign_gift_option_updated, { - 'id' => kind_of(String), + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => 'campaign_gift_option.updated', 'data' => { @@ -238,7 +238,7 @@ RSpec.describe CampaignGiftOption, type: :model do expect(Houdini.event_publisher).to receive(:announce).with(:campaign_create, anything).ordered expect(Houdini.event_publisher).to receive(:announce).with(:campaign_gift_option_created, anything).ordered expect(Houdini.event_publisher).to receive(:announce).with(:campaign_gift_option_deleted, { - 'id' => kind_of(String), + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => 'campaign_gift_option.deleted', 'data' => { @@ -285,7 +285,7 @@ RSpec.describe CampaignGiftOption, type: :model do expect(Houdini.event_publisher).to receive(:announce).with(:campaign_create, anything).ordered expect(Houdini.event_publisher).to receive(:announce).with(:campaign_gift_option_created, anything).ordered expect(Houdini.event_publisher).to receive(:announce).with(:campaign_gift_option_deleted, { - 'id' => kind_of(String), + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => 'campaign_gift_option.deleted', 'data' => { diff --git a/spec/models/concerns/object_event/model_extensions_spec.rb b/spec/models/concerns/object_event/model_extensions_spec.rb index f234d055..8ee2f802 100644 --- a/spec/models/concerns/object_event/model_extensions_spec.rb +++ b/spec/models/concerns/object_event/model_extensions_spec.rb @@ -9,12 +9,12 @@ RSpec.describe ObjectEvent::ModelExtensions do let(:event_type) {'model.event_name'} class ClassWithoutToBuilder include ObjectEvent::ModelExtensions - object_eventable + object_eventable :cwotb end class ClassWithToBuilder include ObjectEvent::ModelExtensions - object_eventable + object_eventable :cwtb def to_builder(*expand) Jbuilder.new do |json| @@ -22,16 +22,20 @@ RSpec.describe ObjectEvent::ModelExtensions do end end end + + it 'raises NotImplementedError when no to_builder is defined by developer' do obj = ClassWithoutToBuilder.new + expect(obj.object_prefix).to eq :cwotb 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.object_prefix).to eq :cwtb expect(obj.to_event event_type).to eq({ - 'id' => kind_of(String), + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => event_type, 'data' => { @@ -41,4 +45,19 @@ RSpec.describe ObjectEvent::ModelExtensions do } }) end + + it 'raises without object_prefix' do + expect do + class ClassWithoutEventablePrefix + include ObjectEvent::ModelExtensions + object_eventable + + def to_builder(*expand) + Jbuilder.new do |json| + json.id 1 + end + end + end + end.to raise_error(ArgumentError) + end end \ No newline at end of file diff --git a/spec/models/event_discount_spec.rb b/spec/models/event_discount_spec.rb index 974d3cf1..9403393a 100644 --- a/spec/models/event_discount_spec.rb +++ b/spec/models/event_discount_spec.rb @@ -54,7 +54,7 @@ RSpec.describe EventDiscount, type: :model do it 'announces create' do expect(Houdini.event_publisher).to receive(:announce).with(:ticket_level_created, anything).ordered expect(Houdini.event_publisher).to receive(:announce).with(:event_discount_created, { - 'id' => kind_of(String), + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => 'event_discount.created', 'data' => { @@ -116,7 +116,7 @@ RSpec.describe EventDiscount, type: :model do expect(Houdini.event_publisher).to receive(:announce).with(:ticket_level_created, anything).ordered expect(Houdini.event_publisher).to receive(:announce).with(:event_discount_created, anything).ordered expect(Houdini.event_publisher).to receive(:announce).with(:event_discount_updated, { - 'id' => kind_of(String), + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => 'event_discount.updated', 'data' => { @@ -179,7 +179,7 @@ RSpec.describe EventDiscount, type: :model do expect(Houdini.event_publisher).to receive(:announce).with(:ticket_level_created, anything).ordered expect(Houdini.event_publisher).to receive(:announce).with(:event_discount_created, anything).ordered expect(Houdini.event_publisher).to receive(:announce).with(:event_discount_deleted, { - 'id' => kind_of(String), + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => 'event_discount.deleted', 'data' => { diff --git a/spec/models/supporter_note_spec.rb b/spec/models/supporter_note_spec.rb index e0ddb66f..d8127fe1 100644 --- a/spec/models/supporter_note_spec.rb +++ b/spec/models/supporter_note_spec.rb @@ -34,7 +34,7 @@ RSpec.describe SupporterNote, type: :model do expect(Houdini.event_publisher).to receive(:announce).with(:supporter_created, anything) expect(Houdini.event_publisher).to receive(:announce).with(:supporter_address_created, anything) expect(Houdini.event_publisher).to receive(:announce).with(:supporter_note_created, { - 'id' => kind_of(String), + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => 'supporter_note.created', 'data' => { @@ -65,7 +65,7 @@ RSpec.describe SupporterNote, type: :model do expect(Houdini.event_publisher).to receive(:announce).with(:supporter_address_created, anything) expect(Houdini.event_publisher).to receive(:announce).with(:supporter_note_created, anything).ordered expect(Houdini.event_publisher).to receive(:announce).with(:supporter_note_updated, { - 'id' => kind_of(String), + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => 'supporter_note.updated', 'data' => { @@ -98,7 +98,7 @@ RSpec.describe SupporterNote, type: :model do expect(Houdini.event_publisher).to receive(:announce).with(:supporter_address_created, anything) expect(Houdini.event_publisher).to receive(:announce).with(:supporter_note_created, anything).ordered expect(Houdini.event_publisher).to receive(:announce).with(:supporter_note_deleted, { - 'id' => kind_of(String), + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => 'supporter_note.deleted', 'data' => { diff --git a/spec/models/supporter_spec.rb b/spec/models/supporter_spec.rb index 1aed4b69..59a059e5 100644 --- a/spec/models/supporter_spec.rb +++ b/spec/models/supporter_spec.rb @@ -61,7 +61,7 @@ RSpec.describe Supporter, type: :model do }) expect(Houdini.event_publisher).to receive(:announce).with(:supporter_created, { - 'id' => kind_of(String), + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => 'supporter.created', 'data' => { @@ -89,7 +89,7 @@ RSpec.describe Supporter, type: :model do }) expect(Houdini.event_publisher).to receive(:announce).with(:supporter_deleted, { - 'id' => kind_of(String), + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => 'supporter.deleted', 'data' => { @@ -111,7 +111,7 @@ RSpec.describe Supporter, type: :model do expect(Houdini.event_publisher).to receive(:announce).with(:supporter_address_created, { - 'id' => kind_of(String), + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => 'supporter_address.created', 'data' => { @@ -139,7 +139,7 @@ RSpec.describe Supporter, type: :model do }) expect(Houdini.event_publisher).to receive(:announce).with(:supporter_address_deleted, { - 'id' => kind_of(String), + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => 'supporter_address.deleted', 'data' => { @@ -157,7 +157,8 @@ RSpec.describe Supporter, type: :model do expect(Houdini.event_publisher).to receive(:announce).with(:supporter_created, anything).ordered expect(Houdini.event_publisher).to receive(:announce).with(:supporter_address_created, anything).ordered - expect(Houdini.event_publisher).to receive(:announce).with(:supporter_updated, { 'id' => kind_of(String), + expect(Houdini.event_publisher).to receive(:announce).with(:supporter_updated, { + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => 'supporter.updated', 'data' => { @@ -180,7 +181,7 @@ RSpec.describe Supporter, type: :model do expect(Houdini.event_publisher).to receive(:announce).with(:supporter_address_created, anything).ordered expect(Houdini.event_publisher).to receive(:announce).with(:supporter_address_updated, { - 'id' => kind_of(String), + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => 'supporter_address.updated', 'data' => { diff --git a/spec/models/tag_master_spec.rb b/spec/models/tag_master_spec.rb index ef8e519b..61feb57b 100644 --- a/spec/models/tag_master_spec.rb +++ b/spec/models/tag_master_spec.rb @@ -15,7 +15,7 @@ RSpec.describe TagMaster, type: :model do it 'announces create' do expect(Houdini.event_publisher).to receive(:announce).with(:tag_master_created, { - 'id' => kind_of(String), + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => 'tag_master.created', 'data' => { @@ -35,7 +35,7 @@ RSpec.describe TagMaster, type: :model do it 'announces deleted' do expect(Houdini.event_publisher).to receive(:announce).with(:tag_master_created, anything).ordered expect(Houdini.event_publisher).to receive(:announce).with(:tag_master_deleted, { - 'id' => kind_of(String), + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => 'tag_master.deleted', 'data' => { diff --git a/spec/models/ticket_level_spec.rb b/spec/models/ticket_level_spec.rb index 0864edab..4b7d49c3 100644 --- a/spec/models/ticket_level_spec.rb +++ b/spec/models/ticket_level_spec.rb @@ -44,7 +44,7 @@ RSpec.describe TicketLevel, type: :model do it 'announces create' do expect(Houdini.event_publisher).to receive(:announce).with(:ticket_level_created, { - 'id' => kind_of(String), + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => 'ticket_level.created', 'data' => { @@ -85,7 +85,7 @@ RSpec.describe TicketLevel, type: :model do it 'announces create' do expect(Houdini.event_publisher).to receive(:announce).with(:ticket_level_created, { - 'id' => kind_of(String), + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => 'ticket_level.created', 'data' => { @@ -131,7 +131,7 @@ RSpec.describe TicketLevel, type: :model do it 'announces updated' do expect(Houdini.event_publisher).to receive(:announce).with(:ticket_level_created, anything).ordered expect(Houdini.event_publisher).to receive(:announce).with(:ticket_level_updated, { - 'id' => kind_of(String), + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => 'ticket_level.updated', 'data' => { @@ -177,7 +177,7 @@ RSpec.describe TicketLevel, type: :model do it 'announces updated' do expect(Houdini.event_publisher).to receive(:announce).with(:ticket_level_created, anything).ordered expect(Houdini.event_publisher).to receive(:announce).with(:ticket_level_updated, { - 'id' => kind_of(String), + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => 'ticket_level.updated', 'data' => { @@ -226,7 +226,7 @@ RSpec.describe TicketLevel, type: :model do it 'announces deleted' do expect(Houdini.event_publisher).to receive(:announce).with(:ticket_level_created, anything).ordered expect(Houdini.event_publisher).to receive(:announce).with(:ticket_level_deleted, { - 'id' => kind_of(String), + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => 'ticket_level.deleted', 'data' => { @@ -268,7 +268,7 @@ RSpec.describe TicketLevel, type: :model do it 'announces deleted' do expect(Houdini.event_publisher).to receive(:announce).with(:ticket_level_created, anything).ordered expect(Houdini.event_publisher).to receive(:announce).with(:ticket_level_deleted, { - 'id' => kind_of(String), + 'id' => match(/objevt_[a-zA-Z0-9]{22}/), 'object' => 'object_event', 'type' => 'ticket_level.deleted', 'data' => {