Commit b9ea1588b85272fa79636ca20d9acbb9b26510c2
1 parent
5af505fc
Exists in
master
and in
2 other branches
Include only topics with discussion categories in random topics
Showing
2 changed files
with
32 additions
and
5 deletions
Show diff stats
lib/proposals_discussion_plugin/discussion.rb
... | ... | @@ -77,6 +77,9 @@ class ProposalsDiscussionPlugin::Discussion < ProposalsDiscussionPlugin::Proposa |
77 | 77 | # order by category.name, random() is used to sort one topic for each category |
78 | 78 | topics = topics.order('articles_categories.category_id, random()') |
79 | 79 | |
80 | + # restrict to discussion categories | |
81 | + topics = topics.where("articles_categories.category_id" => categories) | |
82 | + | |
80 | 83 | topics_ids = topics.to_a |
81 | 84 | |
82 | 85 | self.topics.joins(:categories).where(id: topics_ids).order("categories.name ASC") | ... | ... |
test/unit/discussion_test.rb
... | ... | @@ -15,13 +15,13 @@ class DiscussionTest < ActiveSupport::TestCase |
15 | 15 | topic2 = fast_create(ProposalsDiscussionPlugin::Topic, :parent_id => discussion.id) |
16 | 16 | assert_equivalent [topic1, topic2], discussion.topics |
17 | 17 | end |
18 | - | |
19 | - | |
20 | - | |
18 | + | |
21 | 19 | should 'return list of random topics returning one topic per category' do |
22 | 20 | discussion.save! |
23 | 21 | c1 = create(Category, :name => "Category 1", :environment_id => profile.environment.id) |
24 | 22 | c2 = create(Category, :name => "Category 2", :environment_id => profile.environment.id) |
23 | + discussion.categories << c1 | |
24 | + discussion.categories << c2 | |
25 | 25 | |
26 | 26 | topic1 = fast_create(ProposalsDiscussionPlugin::Topic, :parent_id => discussion.id) |
27 | 27 | topic1.add_category c1 |
... | ... | @@ -41,12 +41,36 @@ class DiscussionTest < ActiveSupport::TestCase |
41 | 41 | |
42 | 42 | assert_equal ["Category 1", "Category 2"],random_topics_categories |
43 | 43 | end |
44 | - | |
44 | + | |
45 | + should 'include only topics with discussion categories in random topics' do | |
46 | + discussion.save! | |
47 | + c1 = create(Category, :name => "Category 1", :environment_id => profile.environment.id) | |
48 | + c2 = create(Category, :name => "Category 2", :environment_id => profile.environment.id) | |
49 | + discussion.categories << c1 | |
50 | + | |
51 | + topic1 = fast_create(ProposalsDiscussionPlugin::Topic, :parent_id => discussion.id) | |
52 | + topic1.add_category c1 | |
53 | + | |
54 | + topic2 = fast_create(ProposalsDiscussionPlugin::Topic, :parent_id => discussion.id) | |
55 | + topic2.add_category c1 | |
56 | + | |
57 | + topic3 = fast_create(ProposalsDiscussionPlugin::Topic, :parent_id => discussion.id, :published => false) | |
58 | + topic3.add_category c2 | |
59 | + | |
60 | + random_topics = discussion.random_topics_one_by_category | |
61 | + | |
62 | + random_topics_categories = random_topics.map {|t| t.categories.map{|c|c.name}}.flatten | |
63 | + | |
64 | + assert_equal ["Category 1"],random_topics_categories | |
65 | + end | |
66 | + | |
45 | 67 | should 'return list of random topics returning 2 topics when having 2 categories' do |
46 | 68 | discussion.save! |
47 | 69 | c1 = create(Category, :name => "Category 1", :environment_id => profile.environment.id) |
48 | 70 | c2 = create(Category, :name => "Category 2", :environment_id => profile.environment.id) |
49 | - | |
71 | + discussion.categories << c1 | |
72 | + discussion.categories << c2 | |
73 | + | |
50 | 74 | topic1 = fast_create(ProposalsDiscussionPlugin::Topic, :parent_id => discussion.id) |
51 | 75 | topic1.add_category c1 |
52 | 76 | ... | ... |