Skip to content

Commit 7ba63be

Browse files
committed
Merge branch 'feat/adiciona-friendly-id' into feat/adiciona-friendlyid-a-outros-models
2 parents aa928ff + 3631964 commit 7ba63be

36 files changed

+77
-66
lines changed

app/controllers/connections_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ def set_connection_and_profile
4848
end
4949

5050
def set_profile
51-
@profile = Profile.find params[:profile_id]
51+
@profile = Profile.friendly.find params[:profile_id]
5252
end
5353
end

app/controllers/invitations_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def show; end
2525
private
2626

2727
def set_invitation
28-
@invitation = Invitation.find(params[:id])
28+
@invitation = Invitation.friendly.find(params[:id])
2929
end
3030

3131
def authorize!

app/controllers/profiles_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def redirect_unauthorized_access
5050
end
5151

5252
def set_profile_and_posts
53-
@profile = Profile.find(params[:id])
53+
@profile = Profile.friendly.find(params[:id])
5454
@posts = current_user == @profile.user ? @profile.posts : @profile.posts.published
5555
end
5656

app/controllers/reports_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def remove_profile
4949
def set_reportable_for_new
5050
reportable_type = params[:reportable_type]
5151
@reportable = Post.friendly.find(reportable_id) if reportable_type == 'Post'
52-
@reportable = Profile.find(reportable_id) if reportable_type == 'Profile'
52+
@reportable = Profile.friendly.find(reportable_id) if reportable_type == 'Profile'
5353
@reportable = Comment.find(reportable_id) if reportable_type == 'Comment'
5454
@reportable = Reply.find(reportable_id) if reportable_type == 'Reply'
5555
end

app/controllers/settings_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def open_to_work
4040
private
4141

4242
def redirect_unauthorized_access
43-
@profile = Profile.find(params[:profile_id])
43+
@profile = Profile.friendly.find(params[:profile_id])
4444
return if current_user == @profile.user
4545

4646
redirect_to root_path, alert: t('alerts.unauthorized')

app/mailers/invitations_mailer.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class InvitationsMailer < ApplicationMailer
22
def received_invitation
3-
profile = Profile.find(params[:profile_id])
3+
profile = Profile.friendly.find(params[:profile_id])
44
project_title = params[:project_title]
55
mail(subject: t('.subject'), to: profile.user.email,
66
body: t('.body', title: project_title))

app/models/invitation.rb

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ class Invitation < ApplicationRecord
1313
after_create :create_notification
1414
after_create :validate_and_approve_pending_request
1515

16+
extend FriendlyId
17+
friendly_id :project_title, use: :slugged
18+
1619
def set_status
1720
self.status = 'pending'
1821
end

app/models/profile.rb

+13-21
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
11
class Profile < ApplicationRecord
22
belongs_to :user
3-
43
has_one_attached :photo
54
has_one :personal_info, dependent: :destroy
6-
75
has_many :professional_infos, dependent: :destroy
86
has_many :education_infos, dependent: :destroy
97
has_many :profile_job_categories, dependent: :destroy
108
has_many :invitations, dependent: :destroy
119
has_many :notifications, dependent: :destroy
1210
has_many :invitation_requests, dependent: :destroy
13-
1411
has_many :posts, through: :user
1512
has_many :job_categories, through: :profile_job_categories
16-
1713
has_many :reports_submitted, class_name: 'Report', dependent: :destroy
1814
has_many :reports_received, class_name: 'Report', as: :reportable, dependent: :destroy
19-
2015
has_many :connections, foreign_key: :followed_profile_id, dependent: :destroy, inverse_of: :followed_profile
21-
2216
has_many :followers, class_name: 'Connection', foreign_key: :followed_profile_id, dependent: :destroy,
2317
inverse_of: :follower
24-
2518
has_many :followed_profiles, class_name: 'Connection', foreign_key: :follower_id, dependent: :destroy,
2619
inverse_of: :followed_profile
27-
2820
accepts_nested_attributes_for :personal_info
2921
accepts_nested_attributes_for :professional_infos
3022
accepts_nested_attributes_for :education_infos
@@ -38,28 +30,33 @@ class Profile < ApplicationRecord
3830
enum privacy: { private_profile: 0, public_profile: 10 }
3931
enum status: { inactive: 0, active: 5 }
4032

33+
extend FriendlyId
34+
friendly_id :profile_permalink, use: :slugged
35+
4136
delegate :full_name, :email, to: :user
4237

38+
def profile_permalink
39+
user.full_name.to_s
40+
end
41+
4342
def self.order_by_premium
4443
joins(user: :subscription).order('subscriptions.status DESC, users.full_name ASC')
4544
end
4645

4746
def self.advanced_search(search_query)
4847
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',
5250
{ term: "%#{sanitize_sql_like(search_query)}%" }
5351
).public_profile.active.uniq
5452
end
5553

5654
def self.get_profile_job_categories_json(query)
5755
profiles = search_by_job_categories(query)
58-
profiles_json = profiles.map do |profile|
56+
profiles.map do |profile|
5957
{ user_id: profile.user_id, full_name: profile.full_name,
6058
job_categories: ProfileJobCategory.generate_profile_job_categories_json(profile.id) }
61-
end
62-
profiles_json.as_json
59+
end.as_json
6360
end
6461

6562
def self.search_by_job_categories(query)
@@ -96,20 +93,15 @@ def self.most_followed(limit)
9693
def inactive!
9794
super
9895
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) }
10397
user.posts.each(&:archived!)
10498
Connection.where(follower: self).or(Connection.where(followed_profile: self)).find_each(&:inactive!)
10599
end
106100

107101
def active!
108102
super
109103
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) }
113105
Connection.where(follower: self).or(Connection.where(followed_profile: self)).find_each(&:active!)
114106
end
115107

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class AddSlugToProfiles < ActiveRecord::Migration[7.1]
2+
def change
3+
add_column :profiles, :slug, :string
4+
add_index :profiles, :slug, unique: true
5+
end
6+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class AddSlugToInvitations < ActiveRecord::Migration[7.1]
2+
def change
3+
add_column :invitations, :slug, :string
4+
add_index :invitations, :slug, unique: true
5+
end
6+
end

db/schema.rb

+5-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/system/authentication/visitor_logs_in_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
click_button class: 'dropdown-toggle'
6060
click_on 'Sair'
6161

62-
expect(current_path).to eq root_path
62+
expect(page).to have_current_path root_path
6363
expect(page).to have_content 'Logout efetuado com sucesso'
6464
end
6565

spec/system/connections/user_follows_another_user_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
expect(mail).to have_received(:deliver_later)
1818

1919
expect(Connection.count).to eq 1
20-
expect(current_path).to eq profile_path(followed)
20+
expect(page).to have_current_path profile_path(followed.profile.slug)
2121
expect(page).to have_content('Agora você está seguindo Eliseu Ramos')
2222
expect(page).not_to have_button('Seguir', exact: true)
2323
expect(page).to have_button('Deixar de Seguir', exact: true)
@@ -28,7 +28,7 @@
2828

2929
visit profile_path(followed.profile)
3030

31-
expect(current_path).to eq new_user_session_path
31+
expect(page).to have_current_path new_user_session_path
3232
expect(page).to have_content 'Para continuar, faça login ou registre-se.'
3333
end
3434

@@ -43,7 +43,7 @@
4343
click_on 'Seguir'
4444

4545
follower_relationship = Connection.last
46-
expect(current_path).to eq profile_path(followed.profile)
46+
expect(page).to have_current_path profile_path(followed.profile)
4747
expect(follower_relationship).to be_active
4848
expect(page).to have_content('Agora você está seguindo Eliseu Ramos')
4949
expect(page).to have_button('Deixar de Seguir', exact: true)

spec/system/connections/user_unfollows_another_user_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
click_on 'Deixar de Seguir'
1313

1414
follower_relationship = Connection.last
15-
expect(current_path).to eq profile_path(followed.profile)
15+
expect(page).to have_current_path profile_path(followed.profile)
1616
expect(follower_relationship).to be_inactive
1717
expect(page).to have_content("Você deixou de seguir #{followed.full_name}")
1818
expect(page).to have_button('Seguir', exact: true)

spec/system/connections/user_views_followers_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
visit profile_following_path(user.profile)
4949

50-
expect(current_path).to eq new_user_session_path
50+
expect(page).to have_current_path new_user_session_path
5151
end
5252
end
5353
end

spec/system/connections/user_views_following_users_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@
4747

4848
visit profile_connections_path(user.profile)
4949

50-
expect(current_path).to eq new_user_session_path
50+
expect(page).to have_current_path new_user_session_path
5151
end
5252
end

spec/system/education_info/user_creates_education_info_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
check 'Exibir no Perfil'
6363
click_on 'Salvar'
6464

65-
expect(current_path).to eq new_user_profile_education_info_path
65+
expect(page).to have_current_path new_user_profile_education_info_path
6666
expect(page).to have_content 'Não foi possível cadastrar formação acadêmica'
6767
expect(page).to have_content 'Instituição não pode ficar em branco'
6868
expect(page).to have_content 'Curso não pode ficar em branco'

spec/system/education_info/user_edits_education_info_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
check 'Exibir no Perfil'
3939
click_on 'Salvar'
4040

41-
expect(current_path).to eq edit_education_info_path(education_info)
41+
expect(page).to have_current_path edit_education_info_path(education_info)
4242
expect(page).to have_content 'Não foi possível atualizar formação acadêmica'
4343
expect(page).to have_content 'Instituição não pode ficar em branco'
4444
expect(page).to have_content 'Curso não pode ficar em branco'

spec/system/invitations/user_views_invitations_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
click_on 'Convites'
1414
end
1515

16-
expect(current_path).to eq invitations_path
16+
expect(page).to have_current_path invitations_path
1717
expect(page).to have_content 'Projeto Cola?Bora!'
1818
expect(page).to have_content 'Um projeto muito legal'
1919
expect(page).to have_content "Expira dia: #{I18n.l(invitation.expiration_date, format: :default)}"
@@ -134,6 +134,6 @@
134134
it 'mas precisa estar logado' do
135135
visit invitations_path
136136

137-
expect(current_path).to eq new_user_session_path
137+
expect(page).to have_current_path new_user_session_path
138138
end
139139
end

spec/system/job_categories/admin_create_job_category_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
it 'e deve estar logado' do
1818
visit job_categories_path
1919

20-
expect(current_path).to eq(new_user_session_path)
20+
expect(page).to have_current_path(new_user_session_path)
2121
expect(page).to have_content('Para continuar, faça login ou registre-se.')
2222
end
2323

spec/system/notifications/user_sees_new_follower_notification_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
visit notifications_path
2323
click_on 'Paulo começou a seguir você'
2424

25-
expect(page).to have_current_path profile_path(follower)
25+
expect(page).to have_current_path profile_path(follower.profile.slug)
2626
expect(Notification.last).to be_clicked
2727
end
2828
end

spec/system/personal_info/user_edits_personal_info_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
it 'e é redirecionado para a tela de login' do
7979
visit edit_user_profile_path
8080

81-
expect(current_path).to eq new_user_session_path
81+
expect(page).to have_current_path new_user_session_path
8282
expect(page).to have_content 'Para continuar, faça login ou registre-se'
8383
end
8484
end

spec/system/posts/user_creates_post_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
it 'apenas quando autenticado' do
55
visit new_post_path
66

7-
expect(current_path).to eq new_user_session_path
7+
expect(page).to have_current_path new_user_session_path
88
end
99

1010
it 'com sucesso' do

spec/system/posts/user_edits_post_spec.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
login_as user
3838
visit edit_post_path(post)
3939

40-
expect(current_path).to eq root_path
40+
expect(page).to have_current_path root_path
4141
expect(page).to have_content 'Você não pode acessar este conteúdo ou realizar esta ação'
4242
end
4343

@@ -70,7 +70,7 @@
7070
post = create(:post, user:, title: 'Post A', content: 'Primeira postagem')
7171

7272
login_as user
73-
visit profile_path(user)
73+
visit profile_path(user.profile.slug)
7474
within "div#post-#{post.id}" do
7575
click_button id: 'pin'
7676
end
@@ -91,7 +91,7 @@
9191
post = create(:post, user:, title: 'Post A', content: 'Primeira postagem', pin: 'pinned')
9292

9393
login_as user
94-
visit profile_path(user)
94+
visit profile_path(user.profile.slug)
9595
within 'div#fixed' do
9696
click_button id: 'unpin'
9797
end
@@ -111,7 +111,7 @@
111111
post2 = create(:post, user:, title: 'Post B', content: 'Segunda postagem')
112112

113113
login_as user
114-
visit profile_path(user)
114+
visit profile_path(user.profile.slug)
115115
within "div#post-#{post.id}" do
116116
click_button id: 'pin'
117117
end
@@ -134,7 +134,7 @@
134134
other_user = create(:user, citizen_id_number: '61328427056', email: '[email protected]')
135135

136136
login_as other_user
137-
visit profile_path(user)
137+
visit profile_path(user.profile.slug)
138138

139139
expect(page).not_to have_content 'Fixar'
140140
expect(page).not_to have_content 'Desafixar'

0 commit comments

Comments
 (0)