Sha256: e3ef4d16dfa4a388a21d615e5669254bf68159bf916a978f9bb4a9b9fbb330b8

Contents?: true

Size: 877 Bytes

Versions: 3

Compression:

Stored size: 877 Bytes

Contents

module MongoidForums
  class Post
    include Mongoid::Document
    include Mongoid::Timestamps

    before_save :set_topic_last_post_at

    belongs_to :topic, :class_name => "MongoidForums::Topic"

    belongs_to :user, :class_name => MongoidForums.user_class.to_s

    belongs_to :reply_to, :class_name => "MongoidForums::Post"

    has_many :replies, :class_name => "MongoidForums::Post",
                       :foreign_key => "reply_to_id",
                       :dependent   => :nullify

    field :text, type: String
    validates :text, :presence => true

    class << self
      def by_created_at
        order_by([:created_at, :asc])
      end

      def by_updated_at
        order_by([:updated_at, :desc])
      end
    end


    protected
      def set_topic_last_post_at
        self.topic.update_attribute(:last_post_at, self.created_at)
      end
    end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mongoid-forums-0.0.3 app/models/mongoid_forums/post.rb
mongoid-forums-0.0.2 app/models/mongoid_forums/post.rb
mongoid-forums-0.0.1 app/models/mongoid_forums/post.rb