Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #11331: Created random generator to fetch nodes randomly as cards #11332

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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])

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected.

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

@jywarren jywarren Aug 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
tag1, tag2 = @node.normal_tags(:followers).includes(:tag).pluck(:name).first(2)
puts ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
puts @node.inspect
puts ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
tag1, tag2 = @node.normal_tags(:followers).includes(:tag).pluck(:name).first(2)


Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected.

# get recommendations
@recommendations = Tag.get_recommendations(tag1, tag2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @KarishmaVanwari, since you want the random cards to be displayed in the dashboard's sidebar shouldn't this be in home_controller?

def dashboard_v2
@title = I18n.t('dashboard._header.dashboard')
# The new dashboard displays the blog and topics list
if current_user
@blog = Tag.find_nodes_by_type('blog', 'note', 1).limit(1).first
# Tags without the blog tag and everything tag to avoid double display
exclude_tids = Tag.where(name: %w(blog everything)).pluck(:tid)
@pagy, @tag_subscriptions = pagy(
TagSelection
.select('tag_selections.tid, term_data.name')
.where(user_id: current_user.id, following: true)
.where.not(tid: exclude_tids)
.joins("INNER JOIN term_data ON tag_selections.tid = term_data.tid")
.order("term_data.activity_timestamp DESC")
)
@trending_tags = trending
render template: 'dashboard_v2/dashboard'
else
redirect_to '/research'
end
end

Please let me know if I'm wrong or missing something.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we put it in a reusable named methodin application_controller so it can be used in multiple places?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried placing this piece of code on line 54 in home_conroller but it gives the following error -
NoMethodError in HomeController#dashboard_v2
undefined method normal_tags for nil:NilClass .

@TildaDares @jywarren

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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private (on line 464) does not make singleton methods private. Use private_class_method or private inside a class << self block instead.


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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private (on line 464) does not make singleton methods private. Use private_class_method or private inside a class << self block instead.


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>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we move this into its own partial - to load with <%= render partial: "nodes/random" %> and the partial template would be _random.html.erb.

<% if !@recommendations.nil? %>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<% if !@recommendations.nil? %>
<% @recommendations ||= Tag.get_recommendations("water-quality", "air-quality") %>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jywarren, I tried this out, but it still doesn't seem to work as I am unable to find cards as expected on the dashboard.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That may be because you don't have nodes with those tags in your local development environment? Can you try tagging a note (not a wiki, i think this is just returning notes, right?) with "water-quality" or "air-quality"? If that doesn't work I will try running this code to debug. No worries!! Did you want to accept this suggestion and commit it?

<% @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