1
1
class Profile < ApplicationRecord
2
2
belongs_to :user
3
-
4
3
has_one_attached :photo
5
4
has_one :personal_info , dependent : :destroy
6
-
7
5
has_many :professional_infos , dependent : :destroy
8
6
has_many :education_infos , dependent : :destroy
9
7
has_many :profile_job_categories , dependent : :destroy
10
8
has_many :invitations , dependent : :destroy
11
9
has_many :notifications , dependent : :destroy
12
10
has_many :invitation_requests , dependent : :destroy
13
-
14
11
has_many :posts , through : :user
15
12
has_many :job_categories , through : :profile_job_categories
16
-
17
13
has_many :reports_submitted , class_name : 'Report' , dependent : :destroy
18
14
has_many :reports_received , class_name : 'Report' , as : :reportable , dependent : :destroy
19
-
20
15
has_many :connections , foreign_key : :followed_profile_id , dependent : :destroy , inverse_of : :followed_profile
21
-
22
16
has_many :followers , class_name : 'Connection' , foreign_key : :followed_profile_id , dependent : :destroy ,
23
17
inverse_of : :follower
24
-
25
18
has_many :followed_profiles , class_name : 'Connection' , foreign_key : :follower_id , dependent : :destroy ,
26
19
inverse_of : :followed_profile
27
-
28
20
accepts_nested_attributes_for :personal_info
29
21
accepts_nested_attributes_for :professional_infos
30
22
accepts_nested_attributes_for :education_infos
@@ -38,28 +30,33 @@ class Profile < ApplicationRecord
38
30
enum privacy : { private_profile : 0 , public_profile : 10 }
39
31
enum status : { inactive : 0 , active : 5 }
40
32
33
+ extend FriendlyId
34
+ friendly_id :profile_permalink , use : :slugged
35
+
41
36
delegate :full_name , :email , to : :user
42
37
38
+ def profile_permalink
39
+ user . full_name . to_s
40
+ end
41
+
43
42
def self . order_by_premium
44
43
joins ( user : :subscription ) . order ( 'subscriptions.status DESC, users.full_name ASC' )
45
44
end
46
45
47
46
def self . advanced_search ( search_query )
48
47
left_outer_joins ( :job_categories , :personal_info , :user ) . where (
49
- 'job_categories.name LIKE :term OR
50
- personal_infos.city LIKE :term OR
51
- users.full_name LIKE :term OR users.search_name LIKE :term' ,
48
+ 'job_categories.name LIKE :term OR personal_infos.city LIKE :term OR users.full_name LIKE :term
49
+ OR users.search_name LIKE :term' ,
52
50
{ term : "%#{ sanitize_sql_like ( search_query ) } %" }
53
51
) . public_profile . active . uniq
54
52
end
55
53
56
54
def self . get_profile_job_categories_json ( query )
57
55
profiles = search_by_job_categories ( query )
58
- profiles_json = profiles . map do |profile |
56
+ profiles . map do |profile |
59
57
{ user_id : profile . user_id , full_name : profile . full_name ,
60
58
job_categories : ProfileJobCategory . generate_profile_job_categories_json ( profile . id ) }
61
- end
62
- profiles_json . as_json
59
+ end . as_json
63
60
end
64
61
65
62
def self . search_by_job_categories ( query )
@@ -96,20 +93,15 @@ def self.most_followed(limit)
96
93
def inactive!
97
94
super
98
95
user . update ( old_name : user . full_name , full_name : 'Perfil Desativado' )
99
- user . posts . each do |post |
100
- post . update ( old_status : post . status )
101
- end
102
-
96
+ user . posts . each { |post | post . update ( old_status : post . status ) }
103
97
user . posts . each ( &:archived! )
104
98
Connection . where ( follower : self ) . or ( Connection . where ( followed_profile : self ) ) . find_each ( &:inactive! )
105
99
end
106
100
107
101
def active!
108
102
super
109
103
user . update ( full_name : user . old_name )
110
- user . posts . each do |post |
111
- post . update ( status : post . old_status )
112
- end
104
+ user . posts . each { |post | post . update ( status : post . old_status ) }
113
105
Connection . where ( follower : self ) . or ( Connection . where ( followed_profile : self ) ) . find_each ( &:active! )
114
106
end
115
107
0 commit comments