2020-04-19 14:27:29 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
class List < ApplicationRecord
|
|
|
|
include List::TimelineMethods
|
2020-04-20 14:03:57 -07:00
|
|
|
|
2015-01-08 04:16:13 -08:00
|
|
|
belongs_to :user
|
2020-05-25 09:04:54 -07:00
|
|
|
has_many :list_members, dependent: :destroy
|
2015-01-10 14:29:04 -08:00
|
|
|
|
2022-07-09 10:03:16 -07:00
|
|
|
validates :name,
|
2022-07-09 10:16:18 -07:00
|
|
|
length: { minimum: 1 },
|
2022-07-09 10:03:16 -07:00
|
|
|
uniqueness: { scope: :user_id }
|
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
|
2022-07-09 10:16:18 -07:00
|
|
|
display_name.strip!
|
|
|
|
self.name = display_name.parameterize
|
|
|
|
self.name = "-followers-" if name == "followers"
|
2015-01-10 14:29:04 -08:00
|
|
|
end
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
alias members list_members
|
2015-01-11 21:41:44 -08:00
|
|
|
|
2015-01-10 14:29:04 -08:00
|
|
|
def add_member(user)
|
2020-05-25 09:04:54 -07:00
|
|
|
ListMember.create! list: self, user: user
|
2015-01-10 14:29:04 -08:00
|
|
|
end
|
2015-01-11 21:41:44 -08:00
|
|
|
|
|
|
|
def remove_member(user)
|
2020-05-25 09:04:54 -07:00
|
|
|
ListMember.where(list: self, user: user).first!.destroy
|
2015-01-11 21:41:44 -08:00
|
|
|
end
|
2015-01-08 04:16:13 -08:00
|
|
|
end
|