2020-04-19 14:27:29 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-04-18 15:59:18 -07:00
|
|
|
class Group < ApplicationRecord
|
2015-01-08 04:16:13 -08:00
|
|
|
belongs_to :user
|
2015-01-10 14:29:04 -08:00
|
|
|
has_many :group_members, dependent: :destroy
|
|
|
|
|
2020-04-19 14:27:29 -07:00
|
|
|
validates :name, length: { minimum: 1 }
|
2015-01-14 08:48:21 -08:00
|
|
|
validates :display_name, length: { maximum: 30 }
|
|
|
|
|
2015-01-10 14:29:04 -08:00
|
|
|
before_validation do
|
2015-01-14 08:48:21 -08:00
|
|
|
self.display_name.strip!
|
2015-01-17 08:50:25 -08:00
|
|
|
self.name = self.display_name.parameterize
|
2015-01-14 08:48:21 -08:00
|
|
|
self.name = '-followers-' if self.name == 'followers'
|
2015-01-10 14:29:04 -08:00
|
|
|
end
|
|
|
|
|
2015-01-11 21:41:44 -08:00
|
|
|
alias members group_members
|
|
|
|
|
2015-01-10 14:29:04 -08:00
|
|
|
def add_member(user)
|
|
|
|
GroupMember.create! group: self, user: user
|
|
|
|
end
|
2015-01-11 21:41:44 -08:00
|
|
|
|
|
|
|
def remove_member(user)
|
|
|
|
GroupMember.where(group: self, user: user).first!.destroy
|
|
|
|
end
|
2015-01-13 04:23:12 -08:00
|
|
|
|
|
|
|
# @return [Array] the groups' timeline
|
|
|
|
def timeline
|
|
|
|
Answer.where("user_id in (?)", members.pluck(:user_id)).order(:created_at).reverse_order
|
|
|
|
end
|
2015-01-08 04:16:13 -08:00
|
|
|
end
|