docbrown/app/models/mentor_relationship.rb
Abraham Williams 9cb40e546b Enables Rails cops (#2186)
* Enable Rails cops

* Fix Rails/DynamicFindBy

* Fix Rails/HttpStatus

* Fix Rails/Blank

* Fix Rails/RequestReferer

* Fix Rails/ActiveRecordAliases

* Fix Rails/FindBy

* Fix Rails/Presence

* Fix Rails/Delegate

* Fix Rails/Validation

* Fix Rails/PluralizationGrammar

* Fix Rails/Present

* Fix Rails/Output

* Fix Rails/Blank

* Fix Rails/FilePath

* Fix Rails/InverseOf

* Fix Rails/LexicallyScopedActionFilter

* Add Rails/OutputSafety to TODO

* Add Rails/HasManyOrHasOneDependent to TODO

* Add Rails/SkipsModelValidations to TODO
2019-03-25 09:25:55 -04:00

53 lines
1.7 KiB
Ruby

class MentorRelationship < ApplicationRecord
belongs_to :mentor, class_name: "User"
belongs_to :mentee, class_name: "User"
validates :mentor, presence: true
validates :mentee, presence: true
validate :check_for_same_user
validates :mentor_id, uniqueness: { scope: :mentee_id }
after_create :mutual_follow
after_create :send_emails
def check_for_same_user
errors.add(:mentor_relationship, "Mentor and Mentee cannot be the same person") if mentor_id == mentee_id
end
def self.unmatched_mentees
sql = "SELECT mentee_form_updated_at,
users.id,
mentor_relationships.mentor_id
FROM users
LEFT JOIN notes ON users.id = notes.noteable_id
LEFT JOIN mentor_relationships ON users.id = mentor_relationships.mentee_id
WHERE seeking_mentorship IS TRUE
AND mentor_relationships.mentor_id IS null"
record_ids = ActiveRecord::Base.connection.execute(sql).pluck("id")
User.where(id: record_ids).where.not(mentee_description: nil)
end
def self.unmatched_mentors
sql = "SELECT mentor_form_updated_at,
users.id,
mentor_relationships.mentor_id
FROM users
LEFT JOIN notes ON users.id = notes.noteable_id
LEFT JOIN mentor_relationships ON users.id = mentor_relationships.mentor_id
WHERE offering_mentorship IS TRUE
AND mentor_relationships.mentee_id IS null"
record_ids = ActiveRecord::Base.connection.execute(sql).pluck("id")
User.where(id: record_ids)
end
private
def mutual_follow
mentor.follow(mentee)
mentee.follow(mentor)
end
def send_emails
NotifyMailer.mentee_email(mentee, mentor).deliver
NotifyMailer.mentor_email(mentor, mentee).deliver
end
end