Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/adiciona friendlyid a outros models #237

Merged
merged 7 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -339,6 +341,7 @@ DEPENDENCIES
factory_bot_rails
faker
faraday
friendly_id (~> 5.5.0)
image_processing (>= 1.2)
jbuilder
jsbundling-rails
Expand Down
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/connections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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/profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 5 additions & 4 deletions app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.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.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
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion app/mailers/invitations_mailer.rb
Original file line number Diff line number Diff line change
@@ -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))
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
34 changes: 13 additions & 21 deletions app/models/profile.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
class Profile < ApplicationRecord
belongs_to :user

has_one_attached :photo
has_one :personal_info, dependent: :destroy

has_many :professional_infos, dependent: :destroy
has_many :education_infos, dependent: :destroy
has_many :profile_job_categories, dependent: :destroy
has_many :invitations, dependent: :destroy
has_many :notifications, dependent: :destroy
has_many :invitation_requests, dependent: :destroy

has_many :posts, through: :user
has_many :job_categories, through: :profile_job_categories

has_many :reports_submitted, class_name: 'Report', dependent: :destroy
has_many :reports_received, class_name: 'Report', as: :reportable, dependent: :destroy

has_many :connections, foreign_key: :followed_profile_id, dependent: :destroy, inverse_of: :followed_profile

has_many :followers, class_name: 'Connection', foreign_key: :followed_profile_id, dependent: :destroy,
inverse_of: :follower

has_many :followed_profiles, class_name: 'Connection', foreign_key: :follower_id, dependent: :destroy,
inverse_of: :followed_profile

accepts_nested_attributes_for :personal_info
accepts_nested_attributes_for :professional_infos
accepts_nested_attributes_for :education_infos
Expand All @@ -38,28 +30,33 @@ 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.order_by_premium
joins(user: :subscription).order('subscriptions.status DESC, users.full_name ASC')
end

def self.advanced_search(search_query)
left_outer_joins(:job_categories, :personal_info, :user).where(
'job_categories.name LIKE :term OR
personal_infos.city LIKE :term OR
users.full_name LIKE :term OR users.search_name LIKE :term',
'job_categories.name LIKE :term OR personal_infos.city LIKE :term OR users.full_name LIKE :term
OR users.search_name LIKE :term',
{ term: "%#{sanitize_sql_like(search_query)}%" }
).public_profile.active.uniq
end

def self.get_profile_job_categories_json(query)
profiles = search_by_job_categories(query)
profiles_json = profiles.map do |profile|
profiles.map do |profile|
{ user_id: profile.user_id, full_name: profile.full_name,
job_categories: ProfileJobCategory.generate_profile_job_categories_json(profile.id) }
end
profiles_json.as_json
end.as_json
end

def self.search_by_job_categories(query)
Expand Down Expand Up @@ -96,20 +93,15 @@ def self.most_followed(limit)
def inactive!
super
user.update(old_name: user.full_name, full_name: 'Perfil Desativado')
user.posts.each do |post|
post.update(old_status: post.status)
end

user.posts.each { |post| post.update(old_status: post.status) }
user.posts.each(&:archived!)
Connection.where(follower: self).or(Connection.where(followed_profile: self)).find_each(&:inactive!)
end

def active!
super
user.update(full_name: user.old_name)
user.posts.each do |post|
post.update(status: post.old_status)
end
user.posts.each { |post| post.update(status: post.old_status) }
Connection.where(follower: self).or(Connection.where(followed_profile: self)).find_each(&:active!)
end

Expand Down
107 changes: 107 additions & 0 deletions config/initializers/friendly_id.rb
Original file line number Diff line number Diff line change
@@ -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? || <your_column_name_here>_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
6 changes: 6 additions & 0 deletions db/migrate/20240215115659_add_slug_to_profiles.rb
Original file line number Diff line number Diff line change
@@ -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
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
21 changes: 21 additions & 0 deletions db/migrate/20240215181135_create_friendly_id_slugs.rb
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading