From a46808cd51a55836fe93137ea4ae3972489c75d6 Mon Sep 17 00:00:00 2001 From: anaresgalla Date: Thu, 15 Feb 2024 09:02:59 -0300 Subject: [PATCH 1/5] Adiciona Gem Co-authored-by: Paulo Henrique --- Gemfile | 1 + Gemfile.lock | 3 + config/initializers/friendly_id.rb | 107 ++++++++++++++++++ .../20240215115659_add_slug_to_profiles.rb | 6 + ...20240215115707_create_friendly_id_slugs.rb | 21 ++++ db/schema.rb | 15 ++- 6 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 config/initializers/friendly_id.rb create mode 100644 db/migrate/20240215115659_add_slug_to_profiles.rb create mode 100644 db/migrate/20240215115707_create_friendly_id_slugs.rb diff --git a/Gemfile b/Gemfile index 2ae20f8..e1c00d5 100644 --- a/Gemfile +++ b/Gemfile @@ -13,6 +13,7 @@ gem 'devise' gem 'faker' gem 'faraday' +gem 'friendly_id', '~> 5.5.0' gem 'image_processing', '>= 1.2' gem 'jbuilder' gem 'jsbundling-rails' diff --git a/Gemfile.lock b/Gemfile.lock index 3de4bb0..c4cb9c9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -136,6 +136,8 @@ GEM webrick (~> 1.7) websocket-driver (>= 0.6, < 0.8) ffi (1.16.3) + friendly_id (5.5.1) + activerecord (>= 4.0.0) globalid (1.2.1) activesupport (>= 6.1) i18n (1.14.1) @@ -339,6 +341,7 @@ DEPENDENCIES factory_bot_rails faker faraday + friendly_id (~> 5.5.0) image_processing (>= 1.2) jbuilder jsbundling-rails diff --git a/config/initializers/friendly_id.rb b/config/initializers/friendly_id.rb new file mode 100644 index 0000000..5852b58 --- /dev/null +++ b/config/initializers/friendly_id.rb @@ -0,0 +1,107 @@ +# FriendlyId Global Configuration +# +# Use this to set up shared configuration options for your entire application. +# Any of the configuration options shown here can also be applied to single +# models by passing arguments to the `friendly_id` class method or defining +# methods in your model. +# +# To learn more, check out the guide: +# +# http://norman.github.io/friendly_id/file.Guide.html + +FriendlyId.defaults do |config| + # ## Reserved Words + # + # Some words could conflict with Rails's routes when used as slugs, or are + # undesirable to allow as slugs. Edit this list as needed for your app. + config.use :reserved + + config.reserved_words = %w[new edit index session login logout users admin + stylesheets assets javascripts images] + + # This adds an option to treat reserved words as conflicts rather than exceptions. + # When there is no good candidate, a UUID will be appended, matching the existing + # conflict behavior. + + # config.treat_reserved_as_conflict = true + + # ## Friendly Finders + # + # Uncomment this to use friendly finders in all models. By default, if + # you wish to find a record by its friendly id, you must do: + # + # MyModel.friendly.find('foo') + # + # If you uncomment this, you can do: + # + # MyModel.find('foo') + # + # This is significantly more convenient but may not be appropriate for + # all applications, so you must explicitly opt-in to this behavior. You can + # always also configure it on a per-model basis if you prefer. + # + # Something else to consider is that using the :finders addon boosts + # performance because it will avoid Rails-internal code that makes runtime + # calls to `Module.extend`. + # + # config.use :finders + # + # ## Slugs + # + # Most applications will use the :slugged module everywhere. If you wish + # to do so, uncomment the following line. + # + # config.use :slugged + # + # By default, FriendlyId's :slugged addon expects the slug column to be named + # 'slug', but you can change it if you wish. + # + # config.slug_column = 'slug' + # + # By default, slug has no size limit, but you can change it if you wish. + # + # config.slug_limit = 255 + # + # When FriendlyId can not generate a unique ID from your base method, it appends + # a UUID, separated by a single dash. You can configure the character used as the + # separator. If you're upgrading from FriendlyId 4, you may wish to replace this + # with two dashes. + # + # config.sequence_separator = '-' + # + # Note that you must use the :slugged addon **prior** to the line which + # configures the sequence separator, or else FriendlyId will raise an undefined + # method error. + # + # ## Tips and Tricks + # + # ### Controlling when slugs are generated + # + # As of FriendlyId 5.0, new slugs are generated only when the slug field is + # nil, but if you're using a column as your base method can change this + # behavior by overriding the `should_generate_new_friendly_id?` method that + # FriendlyId adds to your model. The change below makes FriendlyId 5.0 behave + # more like 4.0. + # Note: Use(include) Slugged module in the config if using the anonymous module. + # If you have `friendly_id :name, use: slugged` in the model, Slugged module + # is included after the anonymous module defined in the initializer, so it + # overrides the `should_generate_new_friendly_id?` method from the anonymous module. + # + # config.use :slugged + # config.use Module.new { + # def should_generate_new_friendly_id? + # slug.blank? || _changed? + # end + # } + # + # FriendlyId uses Rails's `parameterize` method to generate slugs, but for + # languages that don't use the Roman alphabet, that's not usually sufficient. + # Here we use the Babosa library to transliterate Russian Cyrillic slugs to + # ASCII. If you use this, don't forget to add "babosa" to your Gemfile. + # + # config.use Module.new { + # def normalize_friendly_id(text) + # text.to_slug.normalize! :transliterations => [:russian, :latin] + # end + # } +end diff --git a/db/migrate/20240215115659_add_slug_to_profiles.rb b/db/migrate/20240215115659_add_slug_to_profiles.rb new file mode 100644 index 0000000..e8983ab --- /dev/null +++ b/db/migrate/20240215115659_add_slug_to_profiles.rb @@ -0,0 +1,6 @@ +class AddSlugToProfiles < ActiveRecord::Migration[7.1] + def change + add_column :profiles, :slug, :string + add_index :profiles, :slug, unique: true + end +end diff --git a/db/migrate/20240215115707_create_friendly_id_slugs.rb b/db/migrate/20240215115707_create_friendly_id_slugs.rb new file mode 100644 index 0000000..a09491d --- /dev/null +++ b/db/migrate/20240215115707_create_friendly_id_slugs.rb @@ -0,0 +1,21 @@ +MIGRATION_CLASS = + if ActiveRecord::VERSION::MAJOR >= 5 + ActiveRecord::Migration["#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"] + else + ActiveRecord::Migration + end + +class CreateFriendlyIdSlugs < MIGRATION_CLASS + def change + create_table :friendly_id_slugs do |t| + t.string :slug, null: false + t.integer :sluggable_id, null: false + t.string :sluggable_type, limit: 50 + t.string :scope + t.datetime :created_at + end + add_index :friendly_id_slugs, [:sluggable_type, :sluggable_id] + add_index :friendly_id_slugs, [:slug, :sluggable_type], length: {slug: 140, sluggable_type: 50} + add_index :friendly_id_slugs, [:slug, :sluggable_type, :scope], length: {slug: 70, sluggable_type: 50, scope: 70}, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 1e32535..7712822 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_02_14_151256) do +ActiveRecord::Schema[7.1].define(version: 2024_02_15_115707) do create_table "action_text_rich_texts", force: :cascade do |t| t.string "name", null: false t.text "body" @@ -84,6 +84,17 @@ t.index ["profile_id"], name: "index_education_infos_on_profile_id" end + create_table "friendly_id_slugs", force: :cascade do |t| + t.string "slug", null: false + t.integer "sluggable_id", null: false + t.string "sluggable_type", limit: 50 + t.string "scope" + t.datetime "created_at" + t.index ["slug", "sluggable_type", "scope"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type_and_scope", unique: true + t.index ["slug", "sluggable_type"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type" + t.index ["sluggable_type", "sluggable_id"], name: "index_friendly_id_slugs_on_sluggable_type_and_sluggable_id" + end + create_table "invitation_requests", force: :cascade do |t| t.integer "profile_id", null: false t.text "message" @@ -203,6 +214,8 @@ t.integer "privacy", default: 10 t.integer "status", default: 5 t.boolean "removed", default: false + t.string "slug" + t.index ["slug"], name: "index_profiles_on_slug", unique: true t.index ["user_id"], name: "index_profiles_on_user_id" end From bcf0f1e755a0f5c48315b6787a05d7ff1f42aa0a Mon Sep 17 00:00:00 2001 From: anaresgalla Date: Thu, 15 Feb 2024 10:31:13 -0300 Subject: [PATCH 2/5] Adiciona gem ao Profile Corrige testes Co-authored-by: Rosemilson Barbosa Co-authored-by: Lucas Vasques --- app/controllers/connections_controller.rb | 2 +- app/controllers/profiles_controller.rb | 2 +- app/controllers/reports_controller.rb | 2 +- app/controllers/settings_controller.rb | 2 +- app/mailers/invitations_mailer.rb | 2 +- app/models/profile.rb | 7 +++++++ spec/system/authentication/visitor_logs_in_spec.rb | 2 +- .../connections/user_follows_another_user_spec.rb | 6 +++--- .../connections/user_unfollows_another_user_spec.rb | 2 +- spec/system/connections/user_views_followers_spec.rb | 2 +- .../connections/user_views_following_users_spec.rb | 2 +- .../education_info/user_creates_education_info_spec.rb | 2 +- .../education_info/user_edits_education_info_spec.rb | 2 +- spec/system/invitations/user_views_invitations_spec.rb | 4 ++-- .../job_categories/admin_create_job_category_spec.rb | 2 +- .../user_sees_new_follower_notification_spec.rb | 2 +- .../personal_info/user_edits_personal_info_spec.rb | 2 +- spec/system/posts/user_creates_post_spec.rb | 2 +- spec/system/posts/user_edits_post_spec.rb | 10 +++++----- spec/system/posts/user_views_post_list_spec.rb | 6 +++--- spec/system/posts/visitor_views_post_spec.rb | 2 +- .../user_creates_professional_info_spec.rb | 4 ++-- .../user_edits_professional_info_spec.rb | 4 ++-- spec/system/profile/user_views_profile_spec.rb | 2 +- .../reports/admin_sees_reports_listing_page_spec.rb | 2 +- spec/system/reports/user_report_spec.rb | 4 ++-- spec/system/searches/user_searches_post_by_tag_spec.rb | 6 +++--- spec/system/searches/user_searches_spec.rb | 2 +- .../system/searches/users_searchs_others_users_spec.rb | 2 +- .../settings/user_changes_profile_to_private_spec.rb | 2 +- 30 files changed, 50 insertions(+), 43 deletions(-) diff --git a/app/controllers/connections_controller.rb b/app/controllers/connections_controller.rb index 94979c4..62b4796 100644 --- a/app/controllers/connections_controller.rb +++ b/app/controllers/connections_controller.rb @@ -48,6 +48,6 @@ def set_connection_and_profile end def set_profile - @profile = Profile.find params[:profile_id] + @profile = Profile.friendly.find params[:profile_id] end end diff --git a/app/controllers/profiles_controller.rb b/app/controllers/profiles_controller.rb index cb98688..3ae500c 100644 --- a/app/controllers/profiles_controller.rb +++ b/app/controllers/profiles_controller.rb @@ -50,7 +50,7 @@ def redirect_unauthorized_access end def set_profile_and_posts - @profile = Profile.find(params[:id]) + @profile = Profile.friendly.find(params[:id]) @posts = current_user == @profile.user ? @profile.posts : @profile.posts.published end diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb index 1407fce..ee27b61 100644 --- a/app/controllers/reports_controller.rb +++ b/app/controllers/reports_controller.rb @@ -49,7 +49,7 @@ def remove_profile def set_reportable_for_new reportable_id = params[:reportable] @reportable = Post.find(reportable_id) if params[:reportable_type] == 'Post' - @reportable = Profile.find(reportable_id) if params[:reportable_type] == 'Profile' + @reportable = Profile.friendly.find(reportable_id) if params[:reportable_type] == 'Profile' @reportable = Comment.find(reportable_id) if params[:reportable_type] == 'Comment' @reportable = Reply.find(reportable_id) if params[:reportable_type] == 'Reply' end diff --git a/app/controllers/settings_controller.rb b/app/controllers/settings_controller.rb index fde87ca..d7675e7 100644 --- a/app/controllers/settings_controller.rb +++ b/app/controllers/settings_controller.rb @@ -40,7 +40,7 @@ def open_to_work private def redirect_unauthorized_access - @profile = Profile.find(params[:profile_id]) + @profile = Profile.friendly.find(params[:profile_id]) return if current_user == @profile.user redirect_to root_path, alert: t('alerts.unauthorized') diff --git a/app/mailers/invitations_mailer.rb b/app/mailers/invitations_mailer.rb index 1e1e480..be95dc1 100644 --- a/app/mailers/invitations_mailer.rb +++ b/app/mailers/invitations_mailer.rb @@ -1,6 +1,6 @@ class InvitationsMailer < ApplicationMailer def received_invitation - profile = Profile.find(params[:profile_id]) + profile = Profile.friendly.find(params[:profile_id]) project_title = params[:project_title] mail(subject: t('.subject'), to: profile.user.email, body: t('.body', title: project_title)) diff --git a/app/models/profile.rb b/app/models/profile.rb index 663de5e..a395890 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -38,8 +38,15 @@ class Profile < ApplicationRecord enum privacy: { private_profile: 0, public_profile: 10 } enum status: { inactive: 0, active: 5 } + extend FriendlyId + friendly_id :profile_permalink, use: :slugged + delegate :full_name, :email, to: :user + def profile_permalink + user.full_name.to_s + end + def self.advanced_search(search_query) left_outer_joins(:job_categories, :personal_info, :user).where( 'job_categories.name LIKE :term OR diff --git a/spec/system/authentication/visitor_logs_in_spec.rb b/spec/system/authentication/visitor_logs_in_spec.rb index 08d533f..75eeec6 100644 --- a/spec/system/authentication/visitor_logs_in_spec.rb +++ b/spec/system/authentication/visitor_logs_in_spec.rb @@ -59,7 +59,7 @@ click_button class: 'dropdown-toggle' click_on 'Sair' - expect(current_path).to eq root_path + expect(page).to have_current_path root_path expect(page).to have_content 'Logout efetuado com sucesso' end diff --git a/spec/system/connections/user_follows_another_user_spec.rb b/spec/system/connections/user_follows_another_user_spec.rb index abdea9e..611217c 100644 --- a/spec/system/connections/user_follows_another_user_spec.rb +++ b/spec/system/connections/user_follows_another_user_spec.rb @@ -17,7 +17,7 @@ expect(mail).to have_received(:deliver_later) expect(Connection.count).to eq 1 - expect(current_path).to eq profile_path(followed) + expect(page).to have_current_path profile_path(followed) expect(page).to have_content('Agora você está seguindo Eliseu Ramos') expect(page).not_to have_button('Seguir', exact: true) expect(page).to have_button('Deixar de Seguir', exact: true) @@ -28,7 +28,7 @@ visit profile_path(followed.profile) - expect(current_path).to eq new_user_session_path + expect(page).to have_current_path new_user_session_path expect(page).to have_content 'Para continuar, faça login ou registre-se.' end @@ -43,7 +43,7 @@ click_on 'Seguir' follower_relationship = Connection.last - expect(current_path).to eq profile_path(followed.profile) + expect(page).to have_current_path profile_path(followed.profile) expect(follower_relationship).to be_active expect(page).to have_content('Agora você está seguindo Eliseu Ramos') expect(page).to have_button('Deixar de Seguir', exact: true) diff --git a/spec/system/connections/user_unfollows_another_user_spec.rb b/spec/system/connections/user_unfollows_another_user_spec.rb index 445afbd..bac86ea 100644 --- a/spec/system/connections/user_unfollows_another_user_spec.rb +++ b/spec/system/connections/user_unfollows_another_user_spec.rb @@ -12,7 +12,7 @@ click_on 'Deixar de Seguir' follower_relationship = Connection.last - expect(current_path).to eq profile_path(followed.profile) + expect(page).to have_current_path profile_path(followed.profile) expect(follower_relationship).to be_inactive expect(page).to have_content("Você deixou de seguir #{followed.full_name}") expect(page).to have_button('Seguir', exact: true) diff --git a/spec/system/connections/user_views_followers_spec.rb b/spec/system/connections/user_views_followers_spec.rb index 223e4fd..61c6c57 100644 --- a/spec/system/connections/user_views_followers_spec.rb +++ b/spec/system/connections/user_views_followers_spec.rb @@ -47,7 +47,7 @@ visit profile_following_path(user.profile) - expect(current_path).to eq new_user_session_path + expect(page).to have_current_path new_user_session_path end end end diff --git a/spec/system/connections/user_views_following_users_spec.rb b/spec/system/connections/user_views_following_users_spec.rb index d21543f..909084e 100644 --- a/spec/system/connections/user_views_following_users_spec.rb +++ b/spec/system/connections/user_views_following_users_spec.rb @@ -47,6 +47,6 @@ visit profile_connections_path(user.profile) - expect(current_path).to eq new_user_session_path + expect(page).to have_current_path new_user_session_path end end diff --git a/spec/system/education_info/user_creates_education_info_spec.rb b/spec/system/education_info/user_creates_education_info_spec.rb index 005fa91..fbb60ba 100644 --- a/spec/system/education_info/user_creates_education_info_spec.rb +++ b/spec/system/education_info/user_creates_education_info_spec.rb @@ -62,7 +62,7 @@ check 'Exibir no Perfil' click_on 'Salvar' - expect(current_path).to eq new_user_profile_education_info_path + expect(page).to have_current_path new_user_profile_education_info_path expect(page).to have_content 'Não foi possível cadastrar formação acadêmica' expect(page).to have_content 'Instituição não pode ficar em branco' expect(page).to have_content 'Curso não pode ficar em branco' diff --git a/spec/system/education_info/user_edits_education_info_spec.rb b/spec/system/education_info/user_edits_education_info_spec.rb index 1dc92fc..d6f381a 100644 --- a/spec/system/education_info/user_edits_education_info_spec.rb +++ b/spec/system/education_info/user_edits_education_info_spec.rb @@ -38,7 +38,7 @@ check 'Exibir no Perfil' click_on 'Salvar' - expect(current_path).to eq edit_education_info_path(education_info) + expect(page).to have_current_path edit_education_info_path(education_info) expect(page).to have_content 'Não foi possível atualizar formação acadêmica' expect(page).to have_content 'Instituição não pode ficar em branco' expect(page).to have_content 'Curso não pode ficar em branco' diff --git a/spec/system/invitations/user_views_invitations_spec.rb b/spec/system/invitations/user_views_invitations_spec.rb index dab5e0b..770f2f5 100644 --- a/spec/system/invitations/user_views_invitations_spec.rb +++ b/spec/system/invitations/user_views_invitations_spec.rb @@ -13,7 +13,7 @@ click_on 'Convites' end - expect(current_path).to eq invitations_path + expect(page).to have_current_path invitations_path expect(page).to have_content 'Projeto Cola?Bora!' expect(page).to have_content 'Um projeto muito legal' expect(page).to have_content "Expira dia: #{I18n.l(invitation.expiration_date, format: :default)}" @@ -134,6 +134,6 @@ it 'mas precisa estar logado' do visit invitations_path - expect(current_path).to eq new_user_session_path + expect(page).to have_current_path new_user_session_path end end diff --git a/spec/system/job_categories/admin_create_job_category_spec.rb b/spec/system/job_categories/admin_create_job_category_spec.rb index e95af39..2665fbe 100644 --- a/spec/system/job_categories/admin_create_job_category_spec.rb +++ b/spec/system/job_categories/admin_create_job_category_spec.rb @@ -17,7 +17,7 @@ it 'e deve estar logado' do visit job_categories_path - expect(current_path).to eq(new_user_session_path) + expect(page).to have_current_path(new_user_session_path) expect(page).to have_content('Para continuar, faça login ou registre-se.') end diff --git a/spec/system/notifications/user_sees_new_follower_notification_spec.rb b/spec/system/notifications/user_sees_new_follower_notification_spec.rb index 2e78d64..72b6dd8 100644 --- a/spec/system/notifications/user_sees_new_follower_notification_spec.rb +++ b/spec/system/notifications/user_sees_new_follower_notification_spec.rb @@ -22,7 +22,7 @@ visit notifications_path click_on 'Paulo começou a seguir você' - expect(page).to have_current_path profile_path(follower) + expect(page).to have_current_path profile_path(follower.profile.slug) expect(Notification.last).to be_clicked end end diff --git a/spec/system/personal_info/user_edits_personal_info_spec.rb b/spec/system/personal_info/user_edits_personal_info_spec.rb index c85269a..c12f841 100644 --- a/spec/system/personal_info/user_edits_personal_info_spec.rb +++ b/spec/system/personal_info/user_edits_personal_info_spec.rb @@ -78,7 +78,7 @@ it 'e é redirecionado para a tela de login' do visit edit_user_profile_path - expect(current_path).to eq new_user_session_path + expect(page).to have_current_path new_user_session_path expect(page).to have_content 'Para continuar, faça login ou registre-se' end end diff --git a/spec/system/posts/user_creates_post_spec.rb b/spec/system/posts/user_creates_post_spec.rb index cd0bb75..9734651 100644 --- a/spec/system/posts/user_creates_post_spec.rb +++ b/spec/system/posts/user_creates_post_spec.rb @@ -4,7 +4,7 @@ it 'apenas quando autenticado' do visit new_post_path - expect(current_path).to eq new_user_session_path + expect(page).to have_current_path new_user_session_path end it 'com sucesso' do diff --git a/spec/system/posts/user_edits_post_spec.rb b/spec/system/posts/user_edits_post_spec.rb index f2deff6..425c55b 100644 --- a/spec/system/posts/user_edits_post_spec.rb +++ b/spec/system/posts/user_edits_post_spec.rb @@ -37,7 +37,7 @@ login_as user visit edit_post_path(post) - expect(current_path).to eq root_path + expect(page).to have_current_path root_path expect(page).to have_content 'Você não pode acessar este conteúdo ou realizar esta ação' end @@ -70,7 +70,7 @@ post = create(:post, user:, title: 'Post A', content: 'Primeira postagem') login_as user - visit profile_path(user) + visit profile_path(user.profile.slug) within "div#post-#{post.id}" do click_button id: 'pin' end @@ -91,7 +91,7 @@ post = create(:post, user:, title: 'Post A', content: 'Primeira postagem', pin: 'pinned') login_as user - visit profile_path(user) + visit profile_path(user.profile.slug) within 'div#fixed' do click_button id: 'unpin' end @@ -111,7 +111,7 @@ post2 = create(:post, user:, title: 'Post B', content: 'Segunda postagem') login_as user - visit profile_path(user) + visit profile_path(user.profile.slug) within "div#post-#{post.id}" do click_button id: 'pin' end @@ -134,7 +134,7 @@ other_user = create(:user, citizen_id_number: '61328427056', email: 'visitor@email.com') login_as other_user - visit profile_path(user) + visit profile_path(user.profile.slug) expect(page).not_to have_content 'Fixar' expect(page).not_to have_content 'Desafixar' diff --git a/spec/system/posts/user_views_post_list_spec.rb b/spec/system/posts/user_views_post_list_spec.rb index c5eed89..15659ae 100644 --- a/spec/system/posts/user_views_post_list_spec.rb +++ b/spec/system/posts/user_views_post_list_spec.rb @@ -13,7 +13,7 @@ click_on 'Pesquisar' click_on user.full_name - expect(page).to have_current_path(profile_path(user)) + expect(page).to have_current_path(profile_path(user.profile.slug)) within 'h2#post-list-title' do expect(page).to have_content('Publicações') end @@ -39,7 +39,7 @@ published_at: 2.days.from_now) login_as user - visit profile_path(user) + visit profile_path(user.profile.slug) expect(page).to have_content 'Há 2 dias' end @@ -56,7 +56,7 @@ create(:post, user:, title: 'Conteúdo C', content: 'Primeira postagem') login_as user - visit profile_path(user) + visit profile_path(user.profile.slug) expect(page.body.index('Conteúdo C')).to be < page.body.index('Texto B') expect(page.body.index('Texto B')).to be < page.body.index('Post A') diff --git a/spec/system/posts/visitor_views_post_spec.rb b/spec/system/posts/visitor_views_post_spec.rb index 23aa44f..df5c740 100644 --- a/spec/system/posts/visitor_views_post_spec.rb +++ b/spec/system/posts/visitor_views_post_spec.rb @@ -6,7 +6,7 @@ visit post_path(post) - expect(current_path).to eq post_path(post) + expect(page).to have_current_path post_path(post) expect(page).to have_content 'Título do post' expect(page).to have_content 'Conteúdo do post' expect(page).to have_link "Criado por #{post.user.full_name}", href: profile_path(post.user.profile) diff --git a/spec/system/professional_info/user_creates_professional_info_spec.rb b/spec/system/professional_info/user_creates_professional_info_spec.rb index 24bb8cc..49c1463 100644 --- a/spec/system/professional_info/user_creates_professional_info_spec.rb +++ b/spec/system/professional_info/user_creates_professional_info_spec.rb @@ -42,7 +42,7 @@ click_on 'Salvar' - expect(current_path).to eq new_user_profile_professional_info_path + expect(page).to have_current_path new_user_profile_professional_info_path expect(page).to have_content 'Não foi possível cadastrar experiência profissional' expect(page).to have_content 'Empresa não pode ficar em branco' @@ -67,7 +67,7 @@ click_on 'Salvar' - expect(current_path).to eq new_user_profile_professional_info_path + expect(page).to have_current_path new_user_profile_professional_info_path expect(page).to have_content 'Não foi possível cadastrar experiência profissional' expect(page).to have_content 'Empresa não pode ficar em branco' diff --git a/spec/system/professional_info/user_edits_professional_info_spec.rb b/spec/system/professional_info/user_edits_professional_info_spec.rb index d7db6b6..a72097b 100644 --- a/spec/system/professional_info/user_edits_professional_info_spec.rb +++ b/spec/system/professional_info/user_edits_professional_info_spec.rb @@ -47,7 +47,7 @@ click_on 'Salvar' - expect(current_path).to eq edit_professional_info_path(professional_info) + expect(page).to have_current_path edit_professional_info_path(professional_info) expect(page).to have_content 'Não foi possível atualizar experiência profissional' expect(page).to have_content 'Empresa não pode ficar em branco' @@ -93,7 +93,7 @@ visit edit_professional_info_path(professional_info_user1) - expect(current_path).to eq root_path + expect(page).to have_current_path root_path expect(page).to have_content 'Não foi possível completar sua ação' end end diff --git a/spec/system/profile/user_views_profile_spec.rb b/spec/system/profile/user_views_profile_spec.rb index d7cc611..2b2b8fb 100644 --- a/spec/system/profile/user_views_profile_spec.rb +++ b/spec/system/profile/user_views_profile_spec.rb @@ -91,7 +91,7 @@ it 'e é redirecionado para a página de login' do user = create(:user) visit profile_path(user.profile) - expect(current_path).to eq new_user_session_path + expect(page).to have_current_path new_user_session_path expect(page).to have_content 'Para continuar, faça login ou registre-se' end end diff --git a/spec/system/reports/admin_sees_reports_listing_page_spec.rb b/spec/system/reports/admin_sees_reports_listing_page_spec.rb index 8fd5b7b..2dfff00 100644 --- a/spec/system/reports/admin_sees_reports_listing_page_spec.rb +++ b/spec/system/reports/admin_sees_reports_listing_page_spec.rb @@ -94,7 +94,7 @@ login_as user visit reports_path - expect(current_path).to eq root_path + expect(page).to have_current_path root_path expect(page).to have_content 'Você não têm permissão para realizar essa ação.' end end diff --git a/spec/system/reports/user_report_spec.rb b/spec/system/reports/user_report_spec.rb index 0c1dc3d..e7b70ff 100644 --- a/spec/system/reports/user_report_spec.rb +++ b/spec/system/reports/user_report_spec.rb @@ -30,7 +30,7 @@ login_as user visit new_report_path params: { reportable: post, reportable_type: post.class.name } - expect(current_path).to eq root_path + expect(page).to have_current_path root_path expect(page).to have_content 'Essa publicação não está disponível.' end @@ -205,7 +205,7 @@ visit new_report_path params: { reportable: post, reportable_type: post.class.name } - expect(current_path).to eq new_user_session_path + expect(page).to have_current_path new_user_session_path expect(page).to have_content 'Para continuar, faça login ou registre-se.' end end diff --git a/spec/system/searches/user_searches_post_by_tag_spec.rb b/spec/system/searches/user_searches_post_by_tag_spec.rb index 149e382..e6ea0db 100644 --- a/spec/system/searches/user_searches_post_by_tag_spec.rb +++ b/spec/system/searches/user_searches_post_by_tag_spec.rb @@ -13,10 +13,10 @@ click_on 'tdd' end - expect(current_path).to eq searches_path expect(page).to have_content post.title expect(page).to have_content another_post.title expect(page).not_to have_content other_post.title + expect(current_path).to eq searches_path end it 'ao buscar por uma hashtag no campo de busca da home' do @@ -30,11 +30,11 @@ fill_in 'Buscar', with: '#tdd' click_on 'Pesquisar' - expect(current_path).to eq searches_path expect(page).to have_content post.title expect(page).to have_content another_post.title expect(page).not_to have_content other_post.title expect(page).to have_content '2 Publicações com: #tdd' + expect(current_path).to eq searches_path end it 'deve ser uma busca exata' do @@ -48,10 +48,10 @@ fill_in 'Buscar', with: '#td' click_on 'Pesquisar' - expect(current_path).to eq searches_path expect(page).not_to have_content post.title expect(page).not_to have_content another_post.title expect(page).not_to have_content other_post.title expect(page).to have_content 'Nenhum resultado encontrado com: #td' + expect(current_path).to eq searches_path end end diff --git a/spec/system/searches/user_searches_spec.rb b/spec/system/searches/user_searches_spec.rb index 40daee1..08916e2 100644 --- a/spec/system/searches/user_searches_spec.rb +++ b/spec/system/searches/user_searches_spec.rb @@ -9,7 +9,7 @@ fill_in 'Buscar', with: '' click_on 'Pesquisar' - expect(current_path).to eq root_path + expect(page).to have_current_path root_path expect(page).to have_content 'Você precisa informar um termo para fazer a pesquisa' end end diff --git a/spec/system/searches/users_searchs_others_users_spec.rb b/spec/system/searches/users_searchs_others_users_spec.rb index ed346d7..6811761 100644 --- a/spec/system/searches/users_searchs_others_users_spec.rb +++ b/spec/system/searches/users_searchs_others_users_spec.rb @@ -15,7 +15,6 @@ fill_in 'Buscar', with: 'gErAl' click_on 'Pesquisar' - expect(current_path).to eq searches_path expect(page).to have_content('2 resultados para: gErAl') expect(page).not_to have_content 'Horácio Fernandes' expect(page).to have_link 'Geraldo José' @@ -26,6 +25,7 @@ within 'h2' do expect(page).to have_content 'Resultados da Pesquisa' end + expect(current_path).to eq searches_path end it 'e não encontra nenhum usuário' do diff --git a/spec/system/settings/user_changes_profile_to_private_spec.rb b/spec/system/settings/user_changes_profile_to_private_spec.rb index 43d11bb..b80b5bb 100644 --- a/spec/system/settings/user_changes_profile_to_private_spec.rb +++ b/spec/system/settings/user_changes_profile_to_private_spec.rb @@ -28,10 +28,10 @@ fill_in 'Buscar', with: 'C++' click_on 'Pesquisar' - expect(current_path).to eq searches_path expect(page).to have_content('1 resultado para: C++') expect(page).not_to have_content private_user.full_name expect(page).to have_content public_user.full_name + expect(current_path).to eq searches_path end it 'e dados aparecem aos seguidores' do From 039213f003deee086b9d83d231cbbd91f3903f64 Mon Sep 17 00:00:00 2001 From: anaresgalla Date: Thu, 15 Feb 2024 11:38:44 -0300 Subject: [PATCH 3/5] fix/conserta chamada de profile no report controller --- app/controllers/reports_controller.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb index ee27b61..d3f433d 100644 --- a/app/controllers/reports_controller.rb +++ b/app/controllers/reports_controller.rb @@ -48,10 +48,11 @@ def remove_profile def set_reportable_for_new reportable_id = params[:reportable] - @reportable = Post.find(reportable_id) if params[:reportable_type] == 'Post' - @reportable = Profile.friendly.find(reportable_id) if params[:reportable_type] == 'Profile' - @reportable = Comment.find(reportable_id) if params[:reportable_type] == 'Comment' - @reportable = Reply.find(reportable_id) if params[:reportable_type] == 'Reply' + reportable_type = params[:reportable_type] + @reportable = Post.find(reportable_id) if reportable_type == 'Post' + @reportable = Profile.friendly.find(reportable_id) if reportable_type == 'Profile' + @reportable = Comment.find(reportable_id) if reportable_type == 'Comment' + @reportable = Reply.find(reportable_id) if reportable_type == 'Reply' end def set_reportable_for_create From aa928ffd8dd2bef7c7d9e68d2f17e2415f73cc20 Mon Sep 17 00:00:00 2001 From: anaresgalla Date: Thu, 15 Feb 2024 14:31:55 -0300 Subject: [PATCH 4/5] Adiciona FriendlyId aos posts --- Gemfile | 1 + Gemfile.lock | 3 + app/controllers/comments_controller.rb | 14 ++- app/controllers/posts/likes_controller.rb | 2 +- app/controllers/posts_controller.rb | 2 +- app/controllers/reports_controller.rb | 10 +- app/models/post.rb | 3 + config/initializers/friendly_id.rb | 107 ++++++++++++++++++ .../20240215171014_add_slug_to_posts.rb | 6 + ...20240215171359_create_friendly_id_slugs.rb | 21 ++++ db/schema.rb | 15 ++- 11 files changed, 172 insertions(+), 12 deletions(-) create mode 100644 config/initializers/friendly_id.rb create mode 100644 db/migrate/20240215171014_add_slug_to_posts.rb create mode 100644 db/migrate/20240215171359_create_friendly_id_slugs.rb diff --git a/Gemfile b/Gemfile index 2ae20f8..e1c00d5 100644 --- a/Gemfile +++ b/Gemfile @@ -13,6 +13,7 @@ gem 'devise' gem 'faker' gem 'faraday' +gem 'friendly_id', '~> 5.5.0' gem 'image_processing', '>= 1.2' gem 'jbuilder' gem 'jsbundling-rails' diff --git a/Gemfile.lock b/Gemfile.lock index 3de4bb0..c4cb9c9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -136,6 +136,8 @@ GEM webrick (~> 1.7) websocket-driver (>= 0.6, < 0.8) ffi (1.16.3) + friendly_id (5.5.1) + activerecord (>= 4.0.0) globalid (1.2.1) activesupport (>= 6.1) i18n (1.14.1) @@ -339,6 +341,7 @@ DEPENDENCIES factory_bot_rails faker faraday + friendly_id (~> 5.5.0) image_processing (>= 1.2) jbuilder jsbundling-rails diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 95f998c..180c22e 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -1,15 +1,21 @@ class CommentsController < ApplicationController before_action :authenticate_user! + before_action :set_post def create - post = Post.find(params[:post_id]) comments_params = params.require(:comment).permit(:message) comments_params[:user_id] = current_user.id - comment = post.comments.build(comments_params) + comment = @post.comments.build(comments_params) if comment.save - redirect_to post, notice: t('.success') + redirect_to @post, notice: t('.success') else - redirect_to post, alert: t('.error') + redirect_to @post, alert: t('.error') end end end + +private + +def set_post + @post = Post.friendly.find(params[:post_id]) +end diff --git a/app/controllers/posts/likes_controller.rb b/app/controllers/posts/likes_controller.rb index b0024a0..7175121 100644 --- a/app/controllers/posts/likes_controller.rb +++ b/app/controllers/posts/likes_controller.rb @@ -5,7 +5,7 @@ class LikesController < LikesController private def set_likeable - @likeable = Post.find(params[:post_id]) + @likeable = Post.friendly.find(params[:post_id]) @post = @likeable end end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 7adfc0c..69df32f 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -68,7 +68,7 @@ def post_params end def set_post - @post = Post.find(params[:id]) + @post = Post.friendly.find(params[:id]) end def authorize! diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb index 1407fce..4030ff5 100644 --- a/app/controllers/reports_controller.rb +++ b/app/controllers/reports_controller.rb @@ -47,11 +47,11 @@ def remove_profile private def set_reportable_for_new - reportable_id = params[:reportable] - @reportable = Post.find(reportable_id) if params[:reportable_type] == 'Post' - @reportable = Profile.find(reportable_id) if params[:reportable_type] == 'Profile' - @reportable = Comment.find(reportable_id) if params[:reportable_type] == 'Comment' - @reportable = Reply.find(reportable_id) if params[:reportable_type] == 'Reply' + reportable_type = params[:reportable_type] + @reportable = Post.friendly.find(reportable_id) if reportable_type == 'Post' + @reportable = Profile.find(reportable_id) if reportable_type == 'Profile' + @reportable = Comment.find(reportable_id) if reportable_type == 'Comment' + @reportable = Reply.find(reportable_id) if reportable_type == 'Reply' end def set_reportable_for_create diff --git a/app/models/post.rb b/app/models/post.rb index 51134ac..a3ea31c 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -20,6 +20,9 @@ class Post < ApplicationRecord has_rich_text :content + extend FriendlyId + friendly_id :title, use: :slugged + def self.get_sample(amount) published.sample amount end diff --git a/config/initializers/friendly_id.rb b/config/initializers/friendly_id.rb new file mode 100644 index 0000000..5852b58 --- /dev/null +++ b/config/initializers/friendly_id.rb @@ -0,0 +1,107 @@ +# FriendlyId Global Configuration +# +# Use this to set up shared configuration options for your entire application. +# Any of the configuration options shown here can also be applied to single +# models by passing arguments to the `friendly_id` class method or defining +# methods in your model. +# +# To learn more, check out the guide: +# +# http://norman.github.io/friendly_id/file.Guide.html + +FriendlyId.defaults do |config| + # ## Reserved Words + # + # Some words could conflict with Rails's routes when used as slugs, or are + # undesirable to allow as slugs. Edit this list as needed for your app. + config.use :reserved + + config.reserved_words = %w[new edit index session login logout users admin + stylesheets assets javascripts images] + + # This adds an option to treat reserved words as conflicts rather than exceptions. + # When there is no good candidate, a UUID will be appended, matching the existing + # conflict behavior. + + # config.treat_reserved_as_conflict = true + + # ## Friendly Finders + # + # Uncomment this to use friendly finders in all models. By default, if + # you wish to find a record by its friendly id, you must do: + # + # MyModel.friendly.find('foo') + # + # If you uncomment this, you can do: + # + # MyModel.find('foo') + # + # This is significantly more convenient but may not be appropriate for + # all applications, so you must explicitly opt-in to this behavior. You can + # always also configure it on a per-model basis if you prefer. + # + # Something else to consider is that using the :finders addon boosts + # performance because it will avoid Rails-internal code that makes runtime + # calls to `Module.extend`. + # + # config.use :finders + # + # ## Slugs + # + # Most applications will use the :slugged module everywhere. If you wish + # to do so, uncomment the following line. + # + # config.use :slugged + # + # By default, FriendlyId's :slugged addon expects the slug column to be named + # 'slug', but you can change it if you wish. + # + # config.slug_column = 'slug' + # + # By default, slug has no size limit, but you can change it if you wish. + # + # config.slug_limit = 255 + # + # When FriendlyId can not generate a unique ID from your base method, it appends + # a UUID, separated by a single dash. You can configure the character used as the + # separator. If you're upgrading from FriendlyId 4, you may wish to replace this + # with two dashes. + # + # config.sequence_separator = '-' + # + # Note that you must use the :slugged addon **prior** to the line which + # configures the sequence separator, or else FriendlyId will raise an undefined + # method error. + # + # ## Tips and Tricks + # + # ### Controlling when slugs are generated + # + # As of FriendlyId 5.0, new slugs are generated only when the slug field is + # nil, but if you're using a column as your base method can change this + # behavior by overriding the `should_generate_new_friendly_id?` method that + # FriendlyId adds to your model. The change below makes FriendlyId 5.0 behave + # more like 4.0. + # Note: Use(include) Slugged module in the config if using the anonymous module. + # If you have `friendly_id :name, use: slugged` in the model, Slugged module + # is included after the anonymous module defined in the initializer, so it + # overrides the `should_generate_new_friendly_id?` method from the anonymous module. + # + # config.use :slugged + # config.use Module.new { + # def should_generate_new_friendly_id? + # slug.blank? || _changed? + # end + # } + # + # FriendlyId uses Rails's `parameterize` method to generate slugs, but for + # languages that don't use the Roman alphabet, that's not usually sufficient. + # Here we use the Babosa library to transliterate Russian Cyrillic slugs to + # ASCII. If you use this, don't forget to add "babosa" to your Gemfile. + # + # config.use Module.new { + # def normalize_friendly_id(text) + # text.to_slug.normalize! :transliterations => [:russian, :latin] + # end + # } +end diff --git a/db/migrate/20240215171014_add_slug_to_posts.rb b/db/migrate/20240215171014_add_slug_to_posts.rb new file mode 100644 index 0000000..8b0745c --- /dev/null +++ b/db/migrate/20240215171014_add_slug_to_posts.rb @@ -0,0 +1,6 @@ +class AddSlugToPosts < ActiveRecord::Migration[7.1] + def change + add_column :posts, :slug, :string + add_index :posts, :slug, unique: true + end +end diff --git a/db/migrate/20240215171359_create_friendly_id_slugs.rb b/db/migrate/20240215171359_create_friendly_id_slugs.rb new file mode 100644 index 0000000..a09491d --- /dev/null +++ b/db/migrate/20240215171359_create_friendly_id_slugs.rb @@ -0,0 +1,21 @@ +MIGRATION_CLASS = + if ActiveRecord::VERSION::MAJOR >= 5 + ActiveRecord::Migration["#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"] + else + ActiveRecord::Migration + end + +class CreateFriendlyIdSlugs < MIGRATION_CLASS + def change + create_table :friendly_id_slugs do |t| + t.string :slug, null: false + t.integer :sluggable_id, null: false + t.string :sluggable_type, limit: 50 + t.string :scope + t.datetime :created_at + end + add_index :friendly_id_slugs, [:sluggable_type, :sluggable_id] + add_index :friendly_id_slugs, [:slug, :sluggable_type], length: {slug: 140, sluggable_type: 50} + add_index :friendly_id_slugs, [:slug, :sluggable_type, :scope], length: {slug: 70, sluggable_type: 50, scope: 70}, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 1e32535..d40f7e2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_02_14_151256) do +ActiveRecord::Schema[7.1].define(version: 2024_02_15_171359) do create_table "action_text_rich_texts", force: :cascade do |t| t.string "name", null: false t.text "body" @@ -84,6 +84,17 @@ t.index ["profile_id"], name: "index_education_infos_on_profile_id" end + create_table "friendly_id_slugs", force: :cascade do |t| + t.string "slug", null: false + t.integer "sluggable_id", null: false + t.string "sluggable_type", limit: 50 + t.string "scope" + t.datetime "created_at" + t.index ["slug", "sluggable_type", "scope"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type_and_scope", unique: true + t.index ["slug", "sluggable_type"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type" + t.index ["sluggable_type", "sluggable_id"], name: "index_friendly_id_slugs_on_sluggable_type_and_sluggable_id" + end + create_table "invitation_requests", force: :cascade do |t| t.integer "profile_id", null: false t.text "message" @@ -166,6 +177,8 @@ t.integer "status", default: 0 t.datetime "published_at" t.string "old_status" + t.string "slug" + t.index ["slug"], name: "index_posts_on_slug", unique: true t.index ["user_id"], name: "index_posts_on_user_id" end From 4de95dd071bcd70002890c859fa270687c5bc5fc Mon Sep 17 00:00:00 2001 From: anaresgalla Date: Thu, 15 Feb 2024 15:37:28 -0300 Subject: [PATCH 5/5] Fix/Corrige merge conflitos Co-authored-by: Paulo Henrique --- app/controllers/reports_controller.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb index f4ed5d7..ea5b8d2 100644 --- a/app/controllers/reports_controller.rb +++ b/app/controllers/reports_controller.rb @@ -47,6 +47,7 @@ def remove_profile private def set_reportable_for_new + reportable_id = params[:reportable] reportable_type = params[:reportable_type] @reportable = Post.friendly.find(reportable_id) if reportable_type == 'Post' @reportable = Profile.friendly.find(reportable_id) if reportable_type == 'Profile'