Class: Person
Instance Attribute Summary collapse
Class Method Summary
collapse
-
.create_dummy_for(organization) ⇒ Object
-
.dummy_for(organization) ⇒ Object
-
.find_by_email_and_organization(email, organization) ⇒ Object
-
.find_by_import(import) ⇒ Object
-
.find_by_organization(organization) ⇒ Object
-
.find_dupes_in(organization) ⇒ Object
-
.find_or_create(customer, organization) ⇒ Object
You can pass any object as first param as long as it responds to .first_name, .last_name, and .email.
-
.first_or_create(email, organization, attributes = nil, options = {}, &block) ⇒ Object
We’re overriding this in order to excapsulate what is needed to find a unique person.
-
.mergables ⇒ Object
An array of has_many associations that should be merged when a person record is merged with another When an has_many association is added, it must be added here if the association is to be merged.
-
.merge(winner, loser) ⇒ Object
-
.recent(organization, limit = 10) ⇒ Object
Instance Method Summary
collapse
included
#calculate_lifetime_donations, #lifetime_orders
#calculate_lifetime_value, #lifetime_orders
Instance Attribute Details
#skip_commit ⇒ Object
Returns the value of attribute skip_commit.
5
6
7
|
# File 'app/models/person.rb', line 5
def skip_commit
@skip_commit
end
|
#skip_sync_to_mailchimp ⇒ Object
Returns the value of attribute skip_sync_to_mailchimp.
5
6
7
|
# File 'app/models/person.rb', line 5
def skip_sync_to_mailchimp
@skip_sync_to_mailchimp
end
|
Class Method Details
.create_dummy_for(organization) ⇒ Object
184
185
186
187
188
189
190
191
|
# File 'app/models/person.rb', line 184
def self.create_dummy_for(organization)
create({
:first_name => "Anonymous",
:email => "[email protected]",
:dummy => true,
:organization_id => organization.id
})
end
|
.dummy_for(organization) ⇒ Object
175
176
177
178
179
180
181
182
|
# File 'app/models/person.rb', line 175
def self.dummy_for(organization)
dummy = find(:first, :conditions => { :organization_id => organization.id, :dummy => true })
if dummy.nil?
create_dummy_for(organization)
else
dummy
end
end
|
.find_by_email_and_organization(email, organization) ⇒ Object
166
167
168
169
|
# File 'app/models/person.rb', line 166
def self.find_by_email_and_organization(email, organization)
return nil if email.blank?
find(:first, :conditions => { :email => email, :organization_id => organization.id })
end
|
.find_by_import(import) ⇒ Object
123
124
125
|
# File 'app/models/person.rb', line 123
def self.find_by_import(import)
where('import_id = ?', import.id)
end
|
.find_by_organization(organization) ⇒ Object
171
172
173
|
# File 'app/models/person.rb', line 171
def self.find_by_organization(organization)
find_by_organization_id(organization.id)
end
|
.find_dupes_in(organization) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'app/models/person.rb', line 42
def self.find_dupes_in(organization)
hash = {}
Person.where(:organization_id => organization.id)
.where(:import_id => nil)
.includes([:tickets, :actions, :notes, :orders]).each do |p|
if hash[p.dupe_code].nil?
hash[p.dupe_code] = Array.wrap(p)
else
hash[p.dupe_code] << p
end
end
hash
end
|
.find_or_create(customer, organization) ⇒ Object
You can pass any object as first param as long as it responds to .first_name, .last_name, and .email
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
# File 'app/models/person.rb', line 215
def self.find_or_create(customer, organization)
if (customer.respond_to? :person_id) && (!customer.person_id.nil?)
return Person.find(customer.person_id)
elsif (customer.is_a? Person) && (!customer.id.nil?)
person = Person.where(:id => customer.id).where(:organization_id => organization.id).first
return person if person
end
person = Person.find_by_email_and_organization(customer.email, organization)
if person.nil?
params = {
:first_name => customer.first_name,
:last_name => customer.last_name,
:email => customer.email,
:organization_id => organization.id
}
person = Person.create(params)
end
person
end
|
.first_or_create(email, organization, attributes = nil, options = {}, &block) ⇒ Object
We’re overriding this in order to excapsulate what is needed to find a unique person
DO NOT CALL THIS METHOD WITH A BLANK EMAIL
207
208
209
|
# File 'app/models/person.rb', line 207
def self.first_or_create(email, organization, attributes=nil, options ={}, &block)
Person.where(:email => email).where(:organization_id => organization.id).first_or_create(attributes, options, &block)
end
|
.mergables ⇒ Object
An array of has_many associations that should be merged when a person record is merged with another When an has_many association is added, it must be added here if the association is to be merged
Tickets are a special case
62
63
64
|
# File 'app/models/person.rb', line 62
def self.mergables
[:actions, :phones, :notes, :orders]
end
|
.merge(winner, loser) ⇒ Object
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'app/models/person.rb', line 131
def self.merge(winner, loser)
unless winner.organization == loser.organization
raise "Trying to merge two people [#{winner.id}] [#{loser.id}] from different organizations [#{winner.organization.id}] [#{winner.organization.id}]"
end
mergables.each do |mergable|
loser.send(mergable).each do |m|
m.person = winner
m.save!
end
end
loser.tickets.each do |ticket|
ticket.update_column(:buyer_id, winner.id)
end
loser.tags.each do |t|
winner.tag_list << t.name unless winner.tag_list.include? t.name
end
winner.lifetime_value += loser.lifetime_value
winner.lifetime_donations += loser.lifetime_donations
winner.do_not_email = true if loser.do_not_email?
new_lists = loser.subscribed_lists - winner.subscribed_lists
winner.subscribed_lists = winner.subscribed_lists.concat(loser.subscribed_lists).uniq
winner.save!
loser.destroy!
mailchimp_kit = winner.organization.kits.mailchimp
MailchimpSyncJob.merged_person(mailchimp_kit, loser.email, winner.id, new_lists) if mailchimp_kit
return winner
end
|
.recent(organization, limit = 10) ⇒ Object
127
128
129
|
# File 'app/models/person.rb', line 127
def self.recent(organization, limit = 10)
Person.where(:organization_id => organization).order('updated_at DESC').limit(limit)
end
|
Instance Method Details
#add_phone_if_missing(new_phone) ⇒ Object
Will add a phone number ot this record if the number doesn’t already exist
258
259
260
261
262
|
# File 'app/models/person.rb', line 258
def add_phone_if_missing(new_phone)
if (!new_phone.blank? and phones.where("number = ?", new_phone).empty?)
phones.create(:number => new_phone, :kind => "Other")
end
end
|
#create_subscribed_lists_notes!(user) ⇒ Object
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
# File 'app/models/person.rb', line 275
def create_subscribed_lists_notes!(user)
if previous_changes["do_not_email"]
new_note("#{user.email} changed do not email to #{do_not_email}",Time.now,user,organization.id)
end
if previous_changes["subscribed_lists"]
mailchimp_kit.attached_lists.each do |list|
old_lists = previous_changes["subscribed_lists"][0]
if !old_lists.include?(list[:list_id]) && subscribed_lists.include?(list[:list_id])
new_note("#{user.email} changed subscription status of the MailChimp list #{list[:list_name]} to subscribed",Time.now,user,organization.id)
elsif old_lists.include?(list[:list_id]) && !subscribed_lists.include?(list[:list_id])
new_note("#{user.email} changed subscription status of the MailChimp list #{list[:list_name]} to unsubscribed",Time.now,user,organization.id)
end
end
end
end
|
#destroy ⇒ Object
34
35
36
|
# File 'app/models/person.rb', line 34
def destroy
update_column(:deleted_at, Time.now)
end
|
#destroy! ⇒ Object
30
31
32
|
# File 'app/models/person.rb', line 30
def destroy!
destroy
end
|
#dupe_code ⇒ Object
38
39
40
|
# File 'app/models/person.rb', line 38
def dupe_code
"#{first_name} | #{last_name} | #{email}"
end
|
#has_nothing? ⇒ Boolean
70
71
72
|
# File 'app/models/person.rb', line 70
def has_nothing?
actions.empty? && phones.empty? && notes.empty? && orders.empty? && tickets.empty? && address.nil? && import_id.nil?
end
|
#has_something? ⇒ Boolean
66
67
68
|
# File 'app/models/person.rb', line 66
def has_something?
!has_nothing?
end
|
#new_note(text, occurred_at, user, organization_id) ⇒ Object
264
265
266
267
268
269
270
271
272
273
|
# File 'app/models/person.rb', line 264
def new_note(text, occurred_at, user, organization_id)
note = notes.create({
:text => text,
:occurred_at => Time.now
})
note.user_id = user.id
note.organization_id = organization_id
note.save
note
end
|
#person_info ⇒ Object
Bad name. This will respond with true if there is something in first_name, last_name, or email. False otherwise.
309
310
311
|
# File 'app/models/person.rb', line 309
def person_info
!(first_name.blank? and last_name.blank? and email.blank?)
end
|
#starred_actions ⇒ Object
193
194
195
|
# File 'app/models/person.rb', line 193
def starred_actions
Action.where({ :person_id => id, :starred => true }).order(:occurred_at)
end
|
#to_s ⇒ Object
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
# File 'app/models/person.rb', line 292
def to_s
if first_name.present? && last_name.present?
"#{first_name} #{last_name}"
elsif first_name.present?
first_name.to_s
elsif last_name.present?
last_name.to_s
elsif email.present?
email.to_s
elsif id.present?
"No Name ##{id}"
else
"No Name"
end
end
|
#unstarred_actions ⇒ Object
197
198
199
|
# File 'app/models/person.rb', line 197
def unstarred_actions
Action.where({ :person_id => id }).order('occurred_at desc').select{|a| a.unstarred?}
end
|
#update_address(new_address, time_zone, user = nil, updated_by = nil) ⇒ Object
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
# File 'app/models/person.rb', line 240
def update_address(new_address, time_zone, user = nil, updated_by = nil)
unless new_address.nil?
new_address = Address.unhash(new_address)
new_address.person = self
@address = Address.find_or_create(id)
if !@address.update_with_note(self, user, new_address, time_zone, updated_by)
::Rails.logger.error "Could not update address from payment"
return false
end
self.address = @address
save
end
true
end
|