Skip to content

Commit

Permalink
style: Style up the alignment of the User model and add doc to #follows?
Browse files Browse the repository at this point in the history
  • Loading branch information
nanafox committed Dec 13, 2024
1 parent c2e223f commit 771d075
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :trackable, :lockable
:recoverable, :rememberable, :validatable, :trackable, :lockable

has_many :posts, dependent: :delete_all
has_many :comments, dependent: :delete_all

validates :username, uniqueness: { message: "has already been taken, try another one" }
validates :username,
uniqueness: { message: "has already been taken, try another one" }

# Users that this user is following
has_many :active_relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy
has_many :active_relationships,
class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :active_relationships, source: :followed

# Users following this user
has_many :passive_relationships, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy
has_many :passive_relationships,
class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy
has_many :followers, through: :passive_relationships, source: :follower

# Builds the full name for the current user using their first and last names.
Expand All @@ -28,7 +31,8 @@ def initials
"#{first_name.first}#{last_name.first}"
end

def follows?(other)
followed_users.include?(other)
# Checks whether a user is following another user
def follows?(user)
followed_users.load.include?(user)
end
end

0 comments on commit 771d075

Please sign in to comment.