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 Supporter < ApplicationRecord
|
2021-02-02 21:04:53 +00:00
|
|
|
include Model::Jbuilder
|
2019-08-06 14:08:51 +00:00
|
|
|
# :search_vectors,
|
|
|
|
# :profile_id, :profile,
|
|
|
|
# :nonprofit_id, :nonprofit,
|
|
|
|
# :full_contact_info, :full_contact_info_id,
|
|
|
|
# :import_id, :import,
|
|
|
|
# :name,
|
|
|
|
# :first_name,
|
|
|
|
# :last_name,
|
|
|
|
# :email,
|
|
|
|
# :address,
|
|
|
|
# :city,
|
|
|
|
# :state_code,
|
|
|
|
# :country,
|
|
|
|
# :phone,
|
|
|
|
# :organization,
|
|
|
|
# :locale,
|
|
|
|
# :zip_code,
|
|
|
|
# :total_raised,
|
|
|
|
# :notes,
|
|
|
|
# :fields,
|
|
|
|
# :anonymous,
|
|
|
|
# :deleted, # bool (flag for soft delete)
|
|
|
|
# :email_unsubscribe_uuid, #string
|
|
|
|
# :is_unsubscribed_from_emails #bool
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
belongs_to :profile
|
|
|
|
belongs_to :nonprofit
|
|
|
|
belongs_to :import
|
2020-06-11 18:28:02 +00:00
|
|
|
|
2018-03-25 17:30:42 +00:00
|
|
|
has_many :payments
|
|
|
|
has_many :offsite_payments
|
|
|
|
has_many :charges
|
|
|
|
has_many :cards, as: :holder
|
|
|
|
has_many :direct_debit_details
|
|
|
|
has_many :donations
|
|
|
|
has_many :supporter_notes, dependent: :destroy
|
|
|
|
has_many :activities, dependent: :destroy
|
|
|
|
has_many :tickets
|
|
|
|
has_many :recurring_donations
|
|
|
|
has_many :tag_joins, dependent: :destroy
|
|
|
|
has_many :tag_masters, through: :tag_joins
|
|
|
|
has_many :custom_field_joins, dependent: :destroy
|
|
|
|
has_many :custom_field_masters, through: :custom_field_joins
|
2021-02-02 21:04:53 +00:00
|
|
|
has_many :transactions
|
2019-07-30 21:29:24 +00:00
|
|
|
belongs_to :merged_into, class_name: 'Supporter', foreign_key: 'merged_into'
|
2020-06-23 20:31:25 +00:00
|
|
|
has_many :merged_from, class_name: 'Supporter', :foreign_key => "merged_into"
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
validates :nonprofit, presence: true
|
|
|
|
scope :not_deleted, -> { where(deleted: false) }
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2021-02-02 21:04:53 +00:00
|
|
|
add_builder_expansion :nonprofit
|
|
|
|
|
2021-01-19 20:00:23 +00:00
|
|
|
# TODO replace with Discard gem
|
|
|
|
define_model_callbacks :discard
|
|
|
|
|
|
|
|
after_discard :publish_deleted
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def profile_picture(size = :normal)
|
|
|
|
return unless profile
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
profile.get_profile_picture(size)
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
def as_json(options = {})
|
|
|
|
h = super(options)
|
2019-07-30 21:29:24 +00:00
|
|
|
h[:pic_tiny] = profile_picture(:tiny)
|
|
|
|
h[:pic_normal] = profile_picture(:normal)
|
|
|
|
h[:url] = profile && Rails.application.routes.url_helpers.profile_path(profile)
|
|
|
|
h
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
2021-01-14 21:33:10 +00:00
|
|
|
def to_builder(*expand)
|
2021-01-19 20:00:23 +00:00
|
|
|
supporter_addresses = [self]
|
2021-02-02 21:04:53 +00:00
|
|
|
init_builder(*expand) do |json|
|
2021-01-14 21:33:10 +00:00
|
|
|
json.object "supporter"
|
2021-01-19 20:00:23 +00:00
|
|
|
json.(self, :id, :name, :organization, :phone, :anonymous, :deleted)
|
|
|
|
if expand.include? :supporter_address
|
|
|
|
json.supporter_addresses supporter_addresses do |i|
|
|
|
|
json.merge! i.to_supporter_address_builder.attributes!
|
|
|
|
end
|
|
|
|
else
|
|
|
|
json.supporter_addresses [id]
|
|
|
|
end
|
|
|
|
|
|
|
|
unless merged_into.nil?
|
|
|
|
if expand.include? :merged_into
|
|
|
|
json.merged_into merged_into.to_builder
|
|
|
|
else
|
|
|
|
json.merged_into merged_into.id
|
|
|
|
end
|
|
|
|
else
|
|
|
|
json.merged_into nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_supporter_address_builder(*expand)
|
|
|
|
Jbuilder.new do |json|
|
|
|
|
json.(self, :id, :address, :state_code, :city, :country, :zip_code, :deleted)
|
|
|
|
json.object 'supporter_address'
|
|
|
|
if expand.include? :supporter
|
|
|
|
json.supporter to_builder
|
|
|
|
else
|
|
|
|
json.supporter id
|
|
|
|
end
|
2021-01-14 21:33:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-25 17:30:42 +00:00
|
|
|
def full_address
|
2019-07-30 21:29:24 +00:00
|
|
|
Format::Address.full_address(address, city, state_code)
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
2021-01-19 20:00:23 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
ADDRESS_ATTRIBUTES = [:address, :city, :state_code, :zip_code, :country]
|
|
|
|
|
|
|
|
def supporter_address_updated?
|
|
|
|
ADDRESS_ATTRIBUTES.any?{|attrib| saved_change_to_attribute?(attrib)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def nonsupporter_address_updated?
|
|
|
|
(saved_changes.keys.map{|i| i.to_sym} - ADDRESS_ATTRIBUTES).any?
|
|
|
|
end
|
|
|
|
|
|
|
|
def publish_create
|
|
|
|
Houdini.event_publisher.announce(:supporter_created, to_event('supporter.created', :supporter_address, :nonprofit).attributes!)
|
|
|
|
Houdini.event_publisher.announce(:supporter_address_created, to_event('supporter_address.created', :supporter, :nonprofit).attributes!)
|
|
|
|
end
|
|
|
|
|
|
|
|
def publish_updated
|
|
|
|
if !deleted
|
|
|
|
if nonsupporter_address_updated?
|
|
|
|
Houdini.event_publisher.announce(:supporter_updated, to_event('supporter.updated', :supporter_address, :nonprofit).attributes!)
|
|
|
|
end
|
|
|
|
if supporter_address_updated?
|
|
|
|
Houdini.event_publisher.announce(:supporter_address_updated, to_event('supporter_address.updated', :supporter, :nonprofit).attributes!)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def publish_deleted
|
|
|
|
Houdini.event_publisher.announce(:supporter_deleted, to_event('supporter.deleted', :supporter_address, :nonprofit, :merged_into).attributes!)
|
|
|
|
Houdini.event_publisher.announce(:supporter_address_deleted, to_event('supporter_address.deleted', :supporter, :nonprofit).attributes!)
|
|
|
|
end
|
|
|
|
|
|
|
|
# we do something custom here since Supporter and SupporterAddress are in the same model
|
|
|
|
def to_event(event_type, *expand)
|
|
|
|
Jbuilder.new do |event|
|
2021-01-21 22:15:24 +00:00
|
|
|
event.id "objevt_" + SecureRandom.alphanumeric(22)
|
2021-01-19 20:00:23 +00:00
|
|
|
event.object 'object_event'
|
|
|
|
event.type event_type
|
|
|
|
event.data do
|
|
|
|
event.object event_type.start_with?('supporter_address') ? to_supporter_address_builder(*expand) : to_builder(*expand)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-02-02 21:04:53 +00:00
|
|
|
|
|
|
|
# def new_builder(*expand)
|
|
|
|
# Jbuilder.new do | json|
|
|
|
|
# expandable_builders.keys.each do |k|
|
|
|
|
# if expand.include? k
|
|
|
|
# json.set! expandable_builders[k][:json_attrib], expandable_builders[k][:expanded].call(self)
|
|
|
|
# else
|
|
|
|
# json.set! expandable_builders[k][:json_attrib], expandable_builders[k][:id].call(self)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# yield(json)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
|
|
|
|
# def expandable_builders
|
|
|
|
# {
|
|
|
|
# nonprofit: {
|
|
|
|
# json_attrib: :nonprofit,
|
|
|
|
# id: ->(model) { model.nonprofit.id},
|
|
|
|
# expanded: ->(model) { model.nonprofit.to_builder }
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
# end
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
2020-06-11 18:28:02 +00:00
|
|
|
|
|
|
|
ActiveSupport.run_load_hooks(:houdini_supporter, Supporter)
|