2015-07-13 02:14:35 -04:00
|
|
|
class Subcreddit < ActiveRecord::Base
|
2015-07-16 00:51:15 -04:00
|
|
|
extend FriendlyId
|
|
|
|
|
2015-07-13 02:14:35 -04:00
|
|
|
belongs_to :owner, class_name: 'User'
|
2015-07-14 16:29:22 -04:00
|
|
|
has_many :posts
|
2015-07-13 02:14:35 -04:00
|
|
|
|
2015-07-16 00:51:15 -04:00
|
|
|
friendly_id :name, use: :slugged
|
|
|
|
|
2015-07-13 02:14:35 -04:00
|
|
|
attr_accessor :closed
|
|
|
|
|
|
|
|
before_save :set_closed_at
|
|
|
|
|
|
|
|
validates :name,
|
|
|
|
presence: true,
|
|
|
|
format: /\A(?! )[a-z0-9 ]*(?<! )\z/i,
|
2015-07-16 00:51:15 -04:00
|
|
|
uniqueness: true, #{ case_sensitive: false },
|
2015-07-13 02:14:35 -04:00
|
|
|
length: { minimum: 3, maximum: 21 }
|
|
|
|
|
|
|
|
validates :closed,
|
|
|
|
format: /\A[01]?\z/
|
|
|
|
|
2015-07-16 00:51:15 -04:00
|
|
|
validate :slug_does_not_have_uuid
|
2015-07-13 02:14:35 -04:00
|
|
|
|
|
|
|
def closed?
|
|
|
|
self.closed_at != nil
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_closed_at
|
|
|
|
if closed == '1' && closed_at == nil
|
|
|
|
self.closed_at = Time.now
|
|
|
|
elsif closed == '0' && closed_at != nil
|
|
|
|
self.closed_at = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-16 00:51:15 -04:00
|
|
|
def slug_does_not_have_uuid
|
|
|
|
if self.slug.match /([a-z0-9]+\-){4}[a-z0-9]+\z/
|
|
|
|
errors.add(:name, 'must be unique')
|
2015-07-13 02:14:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|