From aa928ffd8dd2bef7c7d9e68d2f17e2415f73cc20 Mon Sep 17 00:00:00 2001 From: anaresgalla Date: Thu, 15 Feb 2024 14:31:55 -0300 Subject: [PATCH 1/2] 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 2/2] 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'