Skip to content

Commit

Permalink
Show grid of notes with thumbnails (#3970)
Browse files Browse the repository at this point in the history
* add nodeshared module for thumbnails

* add title to notes thumbnail

* change grid to table

* remove unnecessary file
  • Loading branch information
kaustubh-nair authored and jywarren committed Nov 15, 2018
1 parent 9a2e82e commit af82259
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def locale_name_pairs
end

def insert_extras(body)
body = NodeShared.notes_thumbnail_grid(body)
body = NodeShared.notes_grid(body)
body = NodeShared.questions_grid(body)
body = NodeShared.activities_grid(body)
Expand Down
38 changes: 38 additions & 0 deletions app/models/concerns/node_shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,44 @@ def liked_by(uid)
likers.collect(&:uid).include?(uid)
end

def self.notes_thumbnail_grid(body, _page = 1)
body.gsub(/(?<![\>`])(\<p\>)?\[notes\:grid\:(\S+)\]/) do |_tagname|
tagname = Regexp.last_match(2)
exclude = nil
if tagname.include?('!')
exclude = tagname.split('!') - [tagname.split('!').first]
tagname = tagname.split('!').first
end

nodes = Node.where(status: 1, type: 'note')
.includes(:revision, :tag)
.references(:term_data, :node_revisions)
.where('term_data.name = ?', tagname)
.order('node_revisions.timestamp DESC')

if exclude.present?
exclude = Node.where(status: 1, type: 'note')
.includes(:revision, :tag)
.references(:node_revisions, :term_data)
.where('term_data.name IN (?)', exclude)
nodes -= exclude
end
output = ''
output += '<p>' if Regexp.last_match(1) == '<p>'
a = ActionController::Base.new
output += a.render_to_string(template: "grids/_thumbnail",
layout: false,
locals: {
tagname: tagname,
randomSeed: rand(1000).to_s,
className: 'notes-grid-thumbnail' + tagname.parameterize,
nodes: nodes,
type: "notes"
})
output
end
end

# rubular regex: http://rubular.com/r/hBEThNL4qd
def self.graph_grid(body, _page = 1)
body.gsub(/(?<![\>`])(\<p\>)?\[graph\:(\S+)\]/) do |_tagname|
Expand Down
41 changes: 41 additions & 0 deletions app/views/grids/_thumbnail.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<table class="table table-responsive">
<% nodes.each_with_index do |node, index| %>

<% if index % 3 == 0 %>
<tr>
<% end %>
<td>
<a class="img" href="<%= node.path %>">
<% if node.main_image %>
<img style="width:100%;" alt="image" class="img-fluid" src="<%= node.main_image.path(:default) %>">
<% end %>
</a>
<h3 style="padding: 0px; margin: 10px;"><a href="<%=node.path%>" style="padding-top: 0px"><%=node.title%></a></h3>
<p class="meta"><small>
<% if node.type == 'note' %>
by <a href="/profile/<%= node.author.name %>"><%= node.author.name %></a> <%= node.author.new_author_contributor %>
<%= distance_of_time_in_words(node.created_at, Time.current, { include_seconds: false, scope:'datetime.time_ago_in_words' }) %>
| <a href="<%= node.path %>#comments"><i style="color:#888;" class="fa fa-comment-o"></i> <%= node.comment_count %></a>
<% else %>
<%= t('notes._notes.last_edit_by') %> <a href="/profile/<%= node.latest.author.name %>"><%= node.latest.author.name %></a>
<%= distance_of_time_in_words(Time.at(node.latest.timestamp), Time.current, { include_seconds: false, scope: 'datetime.time_ago_in_words' }) %>
<% end %>
| <i class="fa fa-eye"></i> <%= number_with_delimiter(node.totalviews) %> <span class="hidden-xs hidden-sm"><%= t('notes._notes.views') %></span>
| <i style="<% if node.likes > 0 %>color:#db4;<% else %>color:#888;<% end %>" class="fa fa-star-o"></i> <%= node.likes %>
</small></p>

</td>
<% if index % 3 == 2 %>
</tr>
<%end %>

<% end %>
</table>

<style>
.table thead tr th, .table tbody tr td {
border: none;
}


</style>

0 comments on commit af82259

Please sign in to comment.