docbrown/app/models/mentor_relationship.rb
rhymes 6553f08d94 Rubocop cleanups (#1415)
* Update rubocop-todo.yml with new violations

* Fix Layout/EmptyLine* rules

* Fix Layout/Indentation* rules

* Fix remaining Layout/* rules

* Fix Lint/DuplicateMethods by removing unused accessor

* Fix Lint/IneffectiveAccessModifier

* Fix Lint/MissingCopEnableDirective

* Re-run rubocop auto gen config

* Fix Layout/RescueEnsureAlignment

* Fix Naming/* rules

* Fix some RSpec/* rules

* Fix typos

* Fix RSpec/LetBeforeExamples

* Series should only be an attr_writer, not an attr_accessor

* Fix RSpec/InstanceVariable

* Fix RSpec/InstanceVariable

* Fix RSpec/RepeatedDescription and RSpec/RepeatedExample

* Fix Style/ClassAndModuleChildren

* Fix Style/ConditionalAssignment

* Fix some Style/* rules

* Trigger Travis CI build because failing tests are not failing locally

* Revert "Fix Style/ClassAndModuleChildren"

This reverts commit 1686801d8a1516ba1894f79e24401a20dea65f99.
2019-01-02 11:20:02 -05:00

55 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_uniqueness_of :mentor_id, scope: :mentee_id
after_create :mutual_follow
after_create :send_emails
def check_for_same_user
if mentor_id == mentee_id
errors.add(:mentor_relationship, "Mentor and Mentee cannot be the same person")
end
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