Commit 6340d895d9f3b647ac9d1e219fb941c517fb8eff
1 parent
d484e7ea
Exists in
master
and in
7 other branches
added method to return random topics one by category
Showing
2 changed files
with
66 additions
and
0 deletions
Show diff stats
lib/proposals_discussion_plugin/discussion.rb
| ... | ... | @@ -63,5 +63,23 @@ class ProposalsDiscussionPlugin::Discussion < ProposalsDiscussionPlugin::Proposa |
| 63 | 63 | def display_media_panel? |
| 64 | 64 | true |
| 65 | 65 | end |
| 66 | + | |
| 67 | + def random_topics_one_by_category | |
| 68 | + #find topics for an discussion | |
| 69 | + topics = self.topics | |
| 70 | + | |
| 71 | + # join categories | |
| 72 | + topics = topics.joins(:article_categorizations, :categories) | |
| 73 | + | |
| 74 | + # select distinct on categories.name | |
| 75 | + topics = topics.select("distinct on(articles_categories.category_id) articles.id") | |
| 76 | + | |
| 77 | + # order by category.name, random() is used to sort one topic for each category | |
| 78 | + topics = topics.order('articles_categories.category_id, random()') | |
| 79 | + | |
| 80 | + topics_ids = topics.to_a | |
| 81 | + | |
| 82 | + self.topics.joins(:categories).where(id: topics_ids).order("categories.name ASC") | |
| 83 | + end | |
| 66 | 84 | |
| 67 | 85 | end | ... | ... |
test/unit/discussion_test.rb
| ... | ... | @@ -15,6 +15,54 @@ 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 | + | |
| 21 | + should 'return list of random topics returning one topic per category' do | |
| 22 | + discussion.save! | |
| 23 | + c1 = create(Category, :name => "Category 1", :environment_id => profile.environment.id) | |
| 24 | + c2 = create(Category, :name => "Category 2", :environment_id => profile.environment.id) | |
| 25 | + | |
| 26 | + topic1 = fast_create(ProposalsDiscussionPlugin::Topic, :parent_id => discussion.id) | |
| 27 | + topic1.add_category c1 | |
| 28 | + | |
| 29 | + topic2 = fast_create(ProposalsDiscussionPlugin::Topic, :parent_id => discussion.id) | |
| 30 | + topic2.add_category c1 | |
| 31 | + | |
| 32 | + topic3 = fast_create(ProposalsDiscussionPlugin::Topic, :parent_id => discussion.id) | |
| 33 | + topic3.add_category c2 | |
| 34 | + | |
| 35 | + topic4 = fast_create(ProposalsDiscussionPlugin::Topic, :parent_id => discussion.id) | |
| 36 | + topic4.add_category c2 | |
| 37 | + | |
| 38 | + random_topics = discussion.random_topics_one_by_category | |
| 39 | + | |
| 40 | + random_topics_categories = random_topics.map {|t| t.category.name } | |
| 41 | + | |
| 42 | + assert_equal ["Category 1", "Category 2"],random_topics_categories | |
| 43 | + end | |
| 44 | + | |
| 45 | + should 'return list of random topics returning 2 topics when having 2 categories' 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 | + | |
| 50 | + topic1 = fast_create(ProposalsDiscussionPlugin::Topic, :parent_id => discussion.id) | |
| 51 | + topic1.add_category c1 | |
| 52 | + | |
| 53 | + topic2 = fast_create(ProposalsDiscussionPlugin::Topic, :parent_id => discussion.id) | |
| 54 | + topic2.add_category c1 | |
| 55 | + | |
| 56 | + topic3 = fast_create(ProposalsDiscussionPlugin::Topic, :parent_id => discussion.id) | |
| 57 | + topic3.add_category c2 | |
| 58 | + | |
| 59 | + topic4 = fast_create(ProposalsDiscussionPlugin::Topic, :parent_id => discussion.id) | |
| 60 | + topic4.add_category c2 | |
| 61 | + | |
| 62 | + random_topics = discussion.random_topics_one_by_category | |
| 63 | + | |
| 64 | + assert_equal 2,random_topics.count | |
| 65 | + end | |
| 18 | 66 | |
| 19 | 67 | should 'return list of proposals' do |
| 20 | 68 | discussion.save! | ... | ... |