Sha256: 56f89d3413e319c262d7d58dae49acbf070bf4880189826b61abbf3d5111805b

Contents?: true

Size: 835 Bytes

Versions: 4

Compression:

Stored size: 835 Bytes

Contents

module Enki
  class CommentActivity
    attr_accessor :post

    def initialize(post)
      self.post = post
    end

    def comments
      @comments ||= post.approved_comments.find_recent(:limit => 5)
    end

    def most_recent_comment
      comments.first
    end

    class << self
      def find_recent
        Post.find(:all,
          :group  => "comments.post_id, posts." + Post.column_names.join(", posts."),
          :select => 'posts.*, max(comments.created_at), comments.post_id',
          :joins  => 'INNER JOIN comments ON comments.post_id = posts.id',
          :order  => 'max(comments.created_at) desc',
          :limit  => 5
        ).collect {|post|
          CommentActivity.new(post)
        }.sort_by {|activity|
          activity.most_recent_comment.created_at
        }.reverse
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
enki-engine-0.0.5 app/models/enki/comment_activity.rb
enki-engine-0.0.4 app/models/enki/comment_activity.rb
enki-engine-0.0.3 app/models/enki/comment_activity.rb
enki-engine-0.0.2 app/models/enki/comment_activity.rb