2020-04-19 23:27:29 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-05-25 18:04:54 +02:00
|
|
|
class List < ApplicationRecord
|
|
|
|
include List::TimelineMethods
|
2020-04-20 23:03:57 +02:00
|
|
|
|
2015-01-08 13:16:13 +01:00
|
|
|
belongs_to :user
|
2020-05-25 18:04:54 +02:00
|
|
|
has_many :list_members, dependent: :destroy
|
2015-01-10 23:29:04 +01:00
|
|
|
|
2020-04-19 23:27:29 +02:00
|
|
|
validates :name, length: { minimum: 1 }
|
2015-01-14 17:48:21 +01:00
|
|
|
validates :display_name, length: { maximum: 30 }
|
|
|
|
|
2015-01-10 23:29:04 +01:00
|
|
|
before_validation do
|
2015-01-14 17:48:21 +01:00
|
|
|
self.display_name.strip!
|
2015-01-17 17:50:25 +01:00
|
|
|
self.name = self.display_name.parameterize
|
2015-01-14 17:48:21 +01:00
|
|
|
self.name = '-followers-' if self.name == 'followers'
|
2015-01-10 23:29:04 +01:00
|
|
|
end
|
|
|
|
|
2020-05-25 18:04:54 +02:00
|
|
|
alias members list_members
|
2015-01-12 06:41:44 +01:00
|
|
|
|
2015-01-10 23:29:04 +01:00
|
|
|
def add_member(user)
|
2020-05-25 18:04:54 +02:00
|
|
|
ListMember.create! list: self, user: user
|
2015-01-10 23:29:04 +01:00
|
|
|
end
|
2015-01-12 06:41:44 +01:00
|
|
|
|
|
|
|
def remove_member(user)
|
2020-05-25 18:04:54 +02:00
|
|
|
ListMember.where(list: self, user: user).first!.destroy
|
2015-01-12 06:41:44 +01:00
|
|
|
end
|
2015-01-08 13:16:13 +01:00
|
|
|
end
|