Refactor and tests.

This commit is contained in:
taitus
2017-08-10 15:02:02 +02:00
parent 555c47e012
commit 4539c5fa00
36 changed files with 1023 additions and 110 deletions

View File

@@ -33,7 +33,7 @@ module Abilities
can :unmark_featured, Debate
can :comment_as_administrator, [Debate, Comment, Proposal, Poll::Question, Budget::Investment,
Legislation::Question, Legislation::Annotation]
Legislation::Question, Legislation::Annotation, Topic]
can [:search, :create, :index, :destroy], ::Administrator
can [:search, :create, :index, :destroy], ::Moderator

View File

@@ -6,7 +6,7 @@ module Abilities
merge Abilities::Moderation.new(user)
can :comment_as_moderator, [Debate, Comment, Proposal, Budget::Investment, Poll::Question,
Legislation::Question, Legislation::Annotation]
Legislation::Question, Legislation::Annotation, Topic]
end
end
end

View File

@@ -3,4 +3,7 @@ class Community < ActiveRecord::Base
has_one :investment
has_many :topics
def participants
User.community_participants(self)
end
end

View File

@@ -1,4 +1,6 @@
class Topic < ActiveRecord::Base
include Flaggable
acts_as_paranoid column: :hidden_at
include ActsAsParanoidAliases
@@ -7,15 +9,12 @@ class Topic < ActiveRecord::Base
has_many :comments, as: :commentable
after_create :associate_comment
validates :title, presence: true
validates :description, presence: true
validates :author, presence: true
scope :sort_by_newest, -> { order(created_at: :desc) }
scope :sort_by_oldest, -> { order(created_at: :asc) }
scope :sort_by_most_commented, -> { reorder(comments_count: :desc) }
private
def associate_comment
Comment.create(commentable: self, user: self.author, body: self.description_as_comment)
end
end

View File

@@ -315,7 +315,13 @@ class User < ActiveRecord::Base
def self.community_participants(community)
topics_ids = community.topics.pluck(:id)
User.joins(:comments).where("comments.commentable_id IN (?) and comments.commentable_type = 'Topic'", topics_ids)
users_who_commented = User.joins(:comments).where("comments.commentable_id IN (?) and comments.commentable_type = 'Topic'", topics_ids).uniq
author_ids = community.topics.pluck(:author_id)
users_who_authors = User.where("users.id IN (?)", author_ids)
users_participants = users_who_commented + users_who_authors
users_participants.uniq
end
private