-
Notifications
You must be signed in to change notification settings - Fork 57
/
person.rb
117 lines (90 loc) · 3.27 KB
/
person.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class Person < ActiveRecord::Base
require 'open-uri'
require 'digest/md5'
include SearchEngine
attr_protected :user_id
has_paper_trail :ignore => [:user_id, :delta]
acts_as_taggable_on :tags, :technologies, :mentor_topic_tags, :mentee_topic_tags
sortable
default_serialization_options :include => { :projects => {:include => [:tags, :technologies]},
:groups => {:include => [:tags, :technologies]},
:companies => {:include => [:tags, :technologies]},
:tags => {},
:technologies => {}}
import_image_from_url_as :photo, :gravatar => true
customizable_slug_from :name
belongs_to :user
accepts_nested_attributes_for :user, :update_only => true
has_many :project_memberships
has_many :projects, :through => :project_memberships
has_many :group_memberships
has_many :groups, :through => :group_memberships
has_many :employments
has_many :companies, :through => :employments
validates_presence_of :name
before_save :attach_to_matching_user
scope :claimed, where('user_id IS NOT null')
scope :unclaimed, where('user_id IS null')
scope :mentors, where('interested_mentor IS true')
scope :mentees, where('interested_mentee IS true')
def email
read_attribute(:email) || user.try(:email)
end
private
def matching_user
@matching_user ||= Authentication.where( :provider => self.imported_from_provider,
:uid => self.imported_from_id ).first.try(:user)
end
def attach_to_matching_user
if self.user.nil? && matching_user.present?
self.user = matching_user
end
end
SAMPLE_PERSON = {
:email => "sample@sample.org",
:url => "http://sample.org/~sample",
:twitter => "sample",
:bio => "I am a sample person's profile.",
:name => "Sample Person",
:location => "Portland, Oregon",
:reviewed => true,
}
def self.find_sample
return self.where(:email => User::SAMPLE_USER[:email]).first
end
def self.find_or_create_sample(create_backreference=true)
person = self.find_sample
unless person
person = self.create!(SAMPLE_PERSON)
end
if create_backreference && ! person.user
User.find_or_create_sample(false)
person.reload!
end
return person
end
end
# == Schema Information
#
# Table name: people
#
# id :integer(4) not null, primary key
# email :string(255)
# twitter :string(255)
# url :string(255)
# bio :text
# created_at :datetime
# updated_at :datetime
# user_id :integer(4)
# name :string(255)
# imported_from_provider :string(255)
# imported_from_id :string(255)
# location :string(255)
# photo_file_name :string(255)
# photo_content_type :string(255)
# photo_file_size :integer(4)
# photo_updated_at :datetime
# reviewed :boolean(1) default(FALSE)
# imported_from_screen_name :string(255)
# delta :boolean(1) default(TRUE), not null
#