Skip to content

Commit

Permalink
Merge pull request #237 from TreinaDev/feat/adiciona-friendlyid-a-out…
Browse files Browse the repository at this point in the history
…ros-models

Feat/adiciona friendlyid a outros models
  • Loading branch information
anaresgalla authored Feb 15, 2024
2 parents 7a5227f + 4de95dd commit 7b14478
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 9 deletions.
14 changes: 10 additions & 4 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/controllers/invitations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def show; end
private

def set_invitation
@invitation = Invitation.find(params[:id])
@invitation = Invitation.friendly.find(params[:id])
end

def authorize!
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/posts/likes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def remove_profile
def set_reportable_for_new
reportable_id = params[:reportable]
reportable_type = params[:reportable_type]
@reportable = Post.find(reportable_id) if reportable_type == 'Post'
@reportable = Post.friendly.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'
Expand Down
3 changes: 3 additions & 0 deletions app/models/invitation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class Invitation < ApplicationRecord
after_create :create_notification
after_create :validate_and_approve_pending_request

extend FriendlyId
friendly_id :project_title, use: :slugged

def set_status
self.status = 'pending'
end
Expand Down
3 changes: 3 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20240215171014_add_slug_to_posts.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions db/migrate/20240215173337_add_slug_to_invitations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddSlugToInvitations < ActiveRecord::Migration[7.1]
def change
add_column :invitations, :slug, :string
add_index :invitations, :slug, unique: true
end
end
6 changes: 5 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7b14478

Please sign in to comment.