Skip to content

Commit

Permalink
Fixes #11331: Created random generator to fetch nodes randomly as cards
Browse files Browse the repository at this point in the history
  • Loading branch information
KarishmaVanwari committed Aug 5, 2022
1 parent 8b97a11 commit 8ae260f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/controllers/wiki_controller.rb
Expand Up @@ -60,6 +60,11 @@ def show
if !@node.nil? # it's a place page!
@tags = @node.tags
@tags += [Tag.find_by(name: params[:id])] if Tag.find_by(name: params[:id])

tag1, tag2 = @node.normal_tags(:followers).includes(:tag).pluck(:name).first(2)

# get recommendations
@recommendations = Tag.get_recommendations(tag1, tag2)
else # it's a new wiki page!
@title = I18n.t('wiki_controller.new_wiki_page')
if current_user
Expand Down
30 changes: 30 additions & 0 deletions app/models/tag.rb
Expand Up @@ -478,4 +478,34 @@ def span(start, fin)
def range(fin, week)
(fin.to_i - week.weeks.to_i).to_s..(fin.to_i - (week - 1).weeks.to_i).to_s
end

def self.get_recommendations(tag1, tag2)

tag1_content_nids = find_recommended_nodes(tag1)
tag2_content_nids = find_recommended_nodes(tag2)

random_content_nids = tag1_content_nids.sample(3) + tag2_content_nids.sample(3)

Node.where("nid IN (?)", random_content_nids)
end

def self.find_recommended_nodes(tagnames, limit = 10)

date_ranges = [1.years.ago..3.years.ago, 4.years.ago..6.years.ago, 7.years.ago..9.years.ago]

selected_date_range = date_ranges.sample(1)

nodes = Node.where("cached_likes > 20 AND views > 100", status: 1)
.where(created: selected_date_range)
.includes(:tag)
.references(:term_data)
.where('term_data.name IN (?)', tagnames)

Node.where('node.nid IN (?)', nodes.collect(&:nid))
.includes(:revision, :tag)
.references(:node_revisions)
.where(status: 1)
.limit(limit)
.pluck(:nid)
end
end
15 changes: 14 additions & 1 deletion app/views/dashboard_v2/_sidebar.html.erb
Expand Up @@ -55,7 +55,20 @@
</div>
</div>
</div>

<div>
<% if !@recommendations.nil? %>
<% @recommendations.each do |node| %>
<div class="card" style="width: 18rem;">
<img class="card-img-top" src="#" alt="Card image cap">
<div class="card-body">
<h5 class="card-title"><%= node.nid %></h5>
<p class="card-text">Post content here</p>
<a href="#" class="btn btn-primary">Read more button here</a>
</div>
</div>
<% end %>
<% end %>
</div>

<style>

Expand Down

0 comments on commit 8ae260f

Please sign in to comment.