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 friendly id #233

Merged
merged 6 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
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/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.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
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
21 changes: 21 additions & 0 deletions db/migrate/20240215115707_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
15 changes: 14 additions & 1 deletion db/schema.rb

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

2 changes: 1 addition & 1 deletion spec/system/authentication/visitor_logs_in_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions spec/system/connections/user_follows_another_user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.profile.slug)
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)
Expand All @@ -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

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion spec/system/connections/user_views_followers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading
Loading