creddit/app/models/post.rb

32 lines
878 B
Ruby
Raw Normal View History

2015-12-11 15:26:51 -05:00
# models/post.rb
2015-07-14 16:29:22 -04:00
class Post < ActiveRecord::Base
belongs_to :user
belongs_to :subcreddit
2015-07-16 11:57:27 -04:00
has_many :comments
2015-07-20 15:12:26 -04:00
delegate :username, to: :user, prefix: true
2015-12-11 15:26:51 -05:00
delegate :slug, to: :subcreddit, prefix: true
2015-07-20 15:12:26 -04:00
2015-07-14 16:29:22 -04:00
validates :title,
presence: true,
length: { maximum: 300 }
validates :content,
length: { maximum: 15000 }
def to_param
2015-07-16 11:57:27 -04:00
# This "just works" because of the way Rails IDs work. .to_i must be run on
# any incoming ID. "1-title-parameterized" will automatically be converted
# to 1 and "2-title-paramerterized-with-number-2" will automatically be
# converted to 2. This gives us desired functionality without adding code
# to properly handle retrieving based on slug. Hopefully, this does not
# cause issues later on.
2015-07-14 16:29:22 -04:00
"#{self.id}-#{self.title.parameterize}"
end
2015-07-16 11:57:27 -04:00
def comments?
self.comments_count != 0
end
2015-07-14 16:29:22 -04:00
end