-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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’ll 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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -60,6 +60,11 @@ def show | |||||||||||||||||||||||||||||||||||||||||||
if [email protected]? # 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) | ||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trailing whitespace detected. |
||||||||||||||||||||||||||||||||||||||||||||
# get recommendations | ||||||||||||||||||||||||||||||||||||||||||||
@recommendations = Tag.get_recommendations(tag1, tag2) | ||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 plots2/app/controllers/home_controller.rb Lines 39 to 59 in ca319fc
Please let me know if I'm wrong or missing something. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried placing this piece of code on line 54 in |
||||||||||||||||||||||||||||||||||||||||||||
else # it's a new wiki page! | ||||||||||||||||||||||||||||||||||||||||||||
@title = I18n.t('wiki_controller.new_wiki_page') | ||||||||||||||||||||||||||||||||||||||||||||
if current_user | ||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,7 +55,19 @@ | |
</div> | ||
</div> | ||
</div> | ||
|
||
<div> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could we move this into its own partial - to load with |
||
<% @recommendations ||= Tag.get_recommendations("blog", "three") %> | ||
<% @recommendations.each do |node| %> | ||
<div class="card recommendations" 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 %> | ||
</div> | ||
|
||
<style> | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -294,4 +294,10 @@ def setup | |
assert lat.location_tag? | ||
assert lon.location_tag? | ||
end | ||
|
||
test 'Tag.get_recommendations' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, here's the unit test. Let's ensure it returns something! We could even test how many nodes it returns, or something else. But most importantly that it returns nodes at all! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, see below where I made a further suggestion about the unit test: #11332 (comment) (it got collapsed because I accepted the change) |
||
nodes = Tag.get_recommendations("blog", "test") | ||
assert_not_nil nodes | ||
jywarren marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert nodes.length > 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aha! This assertion failed! So, looks like we have been getting an empty array back: https://github.com/publiclab/plots2/runs/8217990567?check_suite_focus=true#step:5:85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great, so now, we can try to fix that. Once we push a fix up as a commit to this PR, the test will start to pass again -- that's actually "Test Driven Development" -- we set up a specific enough test condition (or "assertion") that our test will pass once it's working properly. The test actually precedes the final solution! |
||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing whitespace detected.