2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-12 20:03:43 +00:00
|
|
|
# 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
|
2019-02-01 19:40:24 +00:00
|
|
|
class SupporterNote < ApplicationRecord
|
2021-01-14 21:33:10 +00:00
|
|
|
include ObjectEvent::ModelExtensions
|
|
|
|
object_eventable
|
2019-08-06 14:08:51 +00:00
|
|
|
# :content,
|
|
|
|
# :supporter_id, :supporter
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
belongs_to :supporter
|
|
|
|
has_many :activities, as: :attachment, dependent: :destroy
|
2021-01-14 21:33:10 +00:00
|
|
|
belongs_to :user
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
validates :content, length: { minimum: 1 }
|
|
|
|
validates :supporter_id, presence: true
|
2021-01-14 21:33:10 +00:00
|
|
|
# TODO replace with Discard gem
|
|
|
|
define_model_callbacks :discard
|
2021-01-14 18:36:40 +00:00
|
|
|
|
2021-01-14 21:33:10 +00:00
|
|
|
after_discard :publish_deleted
|
2021-01-14 18:36:40 +00:00
|
|
|
|
2021-01-14 21:33:10 +00:00
|
|
|
after_create_commit :publish_create
|
|
|
|
after_update_commit :publish_updated
|
|
|
|
|
|
|
|
# TODO replace with discard gem
|
|
|
|
def discard!
|
|
|
|
run_callbacks(:discard) do
|
|
|
|
self.deleted = true
|
|
|
|
save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_builder(*expand)
|
|
|
|
Jbuilder.new do |json|
|
|
|
|
json.(self, :id, :deleted, :content)
|
|
|
|
json.object 'supporter_note'
|
|
|
|
|
|
|
|
if expand.include? :nonprofit
|
|
|
|
json.nonprofit supporter.nonprofit.to_builder
|
|
|
|
else
|
|
|
|
json.nonprofit supporter.nonprofit.id
|
|
|
|
end
|
|
|
|
|
|
|
|
if expand.include? :supporter
|
|
|
|
json.supporter supporter.to_builder
|
|
|
|
else
|
|
|
|
json.supporter supporter.id
|
|
|
|
end
|
|
|
|
|
|
|
|
if expand.include? :user
|
|
|
|
json.user user&.to_builder
|
|
|
|
else
|
|
|
|
json.user user&.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def publish_create
|
|
|
|
Houdini.event_publisher.announce(:supporter_note_created, to_event('supporter_note.created', :supporter, :nonprofit, :user).attributes!)
|
|
|
|
end
|
|
|
|
|
|
|
|
def publish_updated
|
|
|
|
if !deleted
|
|
|
|
Houdini.event_publisher.announce(:supporter_note_updated, to_event('supporter_note.updated', :supporter, :nonprofit, :user).attributes!)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def publish_deleted
|
|
|
|
Houdini.event_publisher.announce(:supporter_note_deleted, to_event('supporter_note.deleted', :supporter, :nonprofit, :user).attributes!)
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|