From af82259df59b96fa043bbde4bb190278ee560347 Mon Sep 17 00:00:00 2001 From: Kaustubh Nair Date: Fri, 16 Nov 2018 00:17:08 +0530 Subject: [PATCH] Show grid of notes with thumbnails (#3970) * add nodeshared module for thumbnails * add title to notes thumbnail * change grid to table * remove unnecessary file --- app/helpers/application_helper.rb | 1 + app/models/concerns/node_shared.rb | 38 ++++++++++++++++++++++++++ app/views/grids/_thumbnail.html.erb | 41 +++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 app/views/grids/_thumbnail.html.erb diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index f01e29a8ba..a4f92a1e21 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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) diff --git a/app/models/concerns/node_shared.rb b/app/models/concerns/node_shared.rb index 5573def2c4..c293895d9b 100644 --- a/app/models/concerns/node_shared.rb +++ b/app/models/concerns/node_shared.rb @@ -9,6 +9,44 @@ def liked_by(uid) likers.collect(&:uid).include?(uid) end + def self.notes_thumbnail_grid(body, _page = 1) + body.gsub(/(?`])(\)?\[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 += '

' if Regexp.last_match(1) == '

' + 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(/(?`])(\)?\[graph\:(\S+)\]/) do |_tagname| diff --git a/app/views/grids/_thumbnail.html.erb b/app/views/grids/_thumbnail.html.erb new file mode 100644 index 0000000000..ff0985205e --- /dev/null +++ b/app/views/grids/_thumbnail.html.erb @@ -0,0 +1,41 @@ + + <% nodes.each_with_index do |node, index| %> + + <% if index % 3 == 0 %> + + <% end %> + + <% if index % 3 == 2 %> + + <%end %> + + <% end %> +
+ + <% if node.main_image %> + image + <% end %> + +

<%=node.title%>

+

+ <% if node.type == 'note' %> + by <%= node.author.name %> <%= node.author.new_author_contributor %> + <%= distance_of_time_in_words(node.created_at, Time.current, { include_seconds: false, scope:'datetime.time_ago_in_words' }) %> + | <%= node.comment_count %> + <% else %> + <%= t('notes._notes.last_edit_by') %> <%= node.latest.author.name %> + <%= distance_of_time_in_words(Time.at(node.latest.timestamp), Time.current, { include_seconds: false, scope: 'datetime.time_ago_in_words' }) %> + <% end %> + | <%= number_with_delimiter(node.totalviews) %> + | <%= node.likes %> +

+ +
+ +