forked from cykod/Webiva
/
user_profile_entry.rb
135 lines (107 loc) · 4.27 KB
/
user_profile_entry.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# Copyright (C) 2010 Cykod LLC
class UserProfileEntry < DomainModel
before_save :add_url
belongs_to :content_model
belongs_to :user_profile_type
belongs_to :end_user
has_many :user_profile_view_entries
named_scope :by_type, lambda { |type_id|
{ :conditions => { :user_profile_type_id => type_id } }
}
named_scope :by_user, lambda { |user_id|
{ :conditions => { :end_user_id => user_id },
:include => :end_user,
:group => :end_user_id
}
}
cached_content :identifier => :url
content_node(:container_type => 'UserProfileType', :container_field => 'user_profile_type_id', :push_value => true, :except => Proc.new() { |e| e.url.blank? }, :published => :published)
def self.find_published_profile(url,profile_type_id)
self.find_by_url_and_user_profile_type_id(url,profile_type_id,:conditions => { :published => true })
end
def self.fetch_entries(user_ids,profile_type_id=nil)
if profile_type_id
UserProfileEntry.by_type(profile_type_id).by_user(user_ids).all
else
UserProfileEntry.by_user(user_ids).all
end
end
def content_node_body(language)
body = self.attributes.values.map { |atr| [1] } + self.end_user.attributes.slice(*@@user_fields).values
if self.content_model_entry
body += self.content_model_entry.attributes.values
end
body += self.end_user.address.attributes.values if self.end_user.address
body.select { |itm| itm.is_a?(String) }.join(" ")
end
def self.fetch_first_entry(user,profile_type_id=nil)
if profile_type_id == nil
type_ids = UserProfileType.match_classes(user.user_class_id)
profile_type = UserProfileType.find_by_id(type_ids[0])
profile_type_id = profile_type.id if profile_type
end
profile_type_id ? self.fetch_entry(user.id,profile_type_id) : nil
end
@@user_fields = %w(name image full_name first_name last_name gender dob source address billing_address work_address shipping_address salutation middle_name lead_source username cell_phone introduction suffix second_image)
def self.fetch_entry(user_id,profile_type_id)
attr= { :user_profile_type_id => profile_type_id, :end_user_id => user_id }
entry = UserProfileEntry.find(:first, :conditions => attr)
if entry.nil? && profile_type = UserProfileType.find_by_id(profile_type_id)
entry = UserProfileEntry.create(attr.merge(:content_model_id => profile_type.content_model_id))
end
entry
end
@@user_fields.each do |fld|
class_eval <<-METHOD
def #{fld}; self.end_user.#{fld}; end
METHOD
end
def add_url
self.url = create_url
end
def content_model_entry
return @content_model_entry if @content_model_entry
return nil unless self.content_model
cls = self.content_model.model_class
model_attributes = { self.user_profile_type.content_model_field_name => self.end_user_id }
@content_model_entry = cls.find(:first,:conditions => model_attributes)
unless @content_model_entry
@content_model_entry = cls.new(model_attributes)
end
@content_model_entry
end
def content_model_entry_cache=(val)
@content_model_entry = val
end
def create_full_name
self.end_user.full_name if self.end_user
end
def mark_view!(user_id)
self.user_profile_view_entries.find_by_end_user_id(user_id) ||
self.user_profile_view_entries.create(:end_user_id => user_id)
end
def content_description(language)
entry_user = EndUser.find_by_id(self.end_user_id)
" \"%s\"'s User Profile" / entry_user.full_name
end
protected
def create_url
return nil unless self.end_user
return nil if self.end_user.first_name.blank? && self.end_user.last_name.blank? && self.end_user.username.blank?
if !self.end_user.username.blank?
url_try = "#{self.end_user.username.downcase}"
else
url_try = "#{self.end_user.first_name.to_s.downcase}-#{self.end_user.last_name.to_s.downcase}"
end
url_try = url_try.to_s.downcase.gsub(/[ _]+/,"-").gsub(/[^a-z+0-9\-]/,"")
url_base = url_try
cnt = 1
idx = 0
while UserProfileEntry.find_by_url(url_try,:conditions => [ "user_profile_type_id=? AND end_user_id!=?",self.user_profile_type_id,self.end_user_id ])
idx += 1 + (idx > 2 ? rand(50 * cnt) : 0)
cnt += 1
url_try = url_base + '-' + idx.to_s
end
url_try
end
end