Sha256: d7d8a554b9e35b93966d1f3c50cef55a69e6da1e9dd1b84495db1c837a7094c3

Contents?: true

Size: 1.61 KB

Versions: 9

Compression:

Stored size: 1.61 KB

Contents

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

    belongs_to :category, :class_name => "MongoidForums::Category"
    validates :category, :presence => true

    has_many :topics, :class_name => "MongoidForums::Topic",  dependent: :destroy

    # Caching
    field :posts_count, :type => Integer

    field :name

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

    validates :category, :name, :presence => true
    field :position, :type => Integer, :default => 0
    validates :position, numericality: { only_integer: true }

    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

9 entries across 9 versions & 2 rubygems

Version Path
mongoid-forums-1.0.7 app/models/mongoid_forums/forum.rb
mongoid-forums-1.0.6 app/models/mongoid_forums/forum.rb
ack-mongoid-forums-1.0.5 app/models/mongoid_forums/forum.rb
mongoid-forums-1.0.4 app/models/mongoid_forums/forum.rb
mongoid-forums-1.0.3 app/models/mongoid_forums/forum.rb
mongoid-forums-1.0.2 app/models/mongoid_forums/forum.rb
mongoid-forums-1.0.1 app/models/mongoid_forums/forum.rb
mongoid-forums-1.0.0 app/models/mongoid_forums/forum.rb
mongoid-forums-0.0.11 app/models/mongoid_forums/forum.rb