Sha256: 0d3f79024531cc75455d16f7323f424de98c2760648a7cf7f3fb21e748eff04b

Contents?: true

Size: 995 Bytes

Versions: 3

Compression:

Stored size: 995 Bytes

Contents

module MongoidForums
  class Forum
    include Mongoid::Document
    include MongoidForums::Concerns::Viewable

    belongs_to :category, :class_name => "MongoidForums::Category"
    has_many :topics, :class_name => "MongoidForums::Topic"

    # Caching
    field :posts_count, :type => Integer

    field :name
    validates :name, :presence => true

    field :order, :type => Integer, :default => 0

    def unread_topic_count(user)
      view = View.where(:viewable_id => id, :user_id => user.id).first
      return topics.count unless view.present?
      count = 0
      topics.each do |topics|
        if topics.created_at > view.current_viewed_at
          count+=1
        end
      end
      return count
    end

    def count_of_posts
      topics.inject(0) {|sum, topic| topic.posts.count + sum }
    end

    def increment_posts_count
      if self.posts_count == nil
        self.posts_count = 1
      else
        self.posts_count += 1
      end
      self.save
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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