Sha256: eb921b003b22bdab03232b71db42a63a5e8ddb7f40b65085bfdd466931c43991

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

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",  dependent: :destroy

    # Caching
    field :posts_count, :type => Integer

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

    has_and_belongs_to_many :moderator_groups, :class_name => "MongoidForums::Group", inverse_of: nil

    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


    def moderator?(user)
      return false unless user
      return true if category.moderator?(user)
      moderator_groups.each do |group|
        return true if group.moderator && group.members.include?(user.id)
      end
      false
    end

    def moderators
      array = Array.new
      self.moderator_groups.each do |g|
        array << g.group.members
      end
      return array
    end


  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoid-forums-0.0.4 app/models/mongoid_forums/forum.rb