Support embedded link counts via data-* attribute

This commit is contained in:
Robin Ward 2014-01-13 13:37:55 -05:00
parent 488319a5d1
commit bcc7f3aba4
2 changed files with 16 additions and 6 deletions

View file

@ -28,11 +28,14 @@ class EmbedController < ApplicationController
def count def count
topic_embeds = TopicEmbed.where(embed_url: params[:embed_url]).includes(:topic).all urls = params[:embed_url].map {|u| u.sub(/#discourse-comments$/, '') }
topic_embeds = TopicEmbed.where(embed_url: urls).includes(:topic).references(:topic)
by_url = {} by_url = {}
topic_embeds.each do |te| topic_embeds.each do |te|
by_url["#{te.embed_url}#discourse-comments"] = I18n.t('embed.replies', count: te.topic.posts_count - 1) url = te.embed_url
url = "#{url}#discourse-comments" unless params[:embed_url].include?(url)
by_url[url] = I18n.t('embed.replies', count: te.topic.posts_count - 1)
end end
respond_to do |format| respond_to do |format|

View file

@ -8,13 +8,20 @@
countFor = []; countFor = [];
for(var i=0; i<links.length; i++) { for(var i=0; i<links.length; i++) {
var href = links[i].href; var link = links[i],
href = link.href;
if (href && href.length) { if (href && href.length) {
var m = /^(.*)#discourse-comments$/.exec(href); if (/#discourse-comments$/.test(href)) {
if (m && m[1]) { countFor.push(m[1]); } countFor.push(href);
} }
} }
//
var dataEmbed = link.getAttribute('data-discourse-embed-url');
if (dataEmbed && dataEmbed.length) {
countFor.push(dataEmbed);
}
}
// JSONP callback to update counts // JSONP callback to update counts
window.discourseUpdateCounts = function(result) { window.discourseUpdateCounts = function(result) {
if (result && result.counts) { if (result && result.counts) {