-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into notifications
- Loading branch information
Showing
14 changed files
with
123 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
(function(window) { | ||
window.Mentions = { | ||
init: function(elements) { | ||
tribute = new Tribute({ | ||
allowSpaces: function() { return false }, | ||
menuItemTemplate: function(item) { | ||
return '<img src="' + item.original.avatar_url + '" width="24px" height="24px" > ' + item.string | ||
}, | ||
noMatchTemplate: function() { return '' }, | ||
values: JSON.parse($('meta[name=mentionable-users]').attr('content')) | ||
}); | ||
|
||
$('[data-behavior~=mentions-scroll]').scroll(function(){ | ||
tribute.hideMenu(); | ||
}); | ||
|
||
tribute.attach(elements); | ||
} | ||
} | ||
})(window); |
15 changes: 0 additions & 15 deletions
15
app/assets/javascripts/snowcrash/modules/mentions.js.coffee
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,47 @@ | ||
# This module can be included anywhere comments are rendered. Any item that is | ||
# commentable can use this module to help render comment collections in views. | ||
# It will load comments and authors without n+1 as well as build avatars for all | ||
# mentions within the collection of comments. Additionally it can be used where | ||
# a single comment is rendered such as activity polling, or comment creation via | ||
# ajax. | ||
module Commented | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
helper_method :commentable, :comments | ||
helper_method :commentable, :comments, :mentions_builder | ||
end | ||
|
||
def commentable | ||
instance_variable_get("@#{controller_name.singularize}") | ||
end | ||
|
||
def comments | ||
@comments ||= commentable.comments.includes(:user) | ||
@comments ||= commentable&.comments&.includes(:user) | ||
end | ||
|
||
def mentioned_users(comments) | ||
# Match any non whitespace that starts with an @ has another @ | ||
emails = comments.inject([]) do |collection, comment| | ||
(collection + comment.scan(/@(\S*@\S*)/)).flatten.uniq | ||
end | ||
|
||
current_project.authors.where(email: emails) | ||
end | ||
|
||
# When called this will scan the provided comments for email addresses and | ||
# search for users of the current project with that address. It then builds | ||
# out a hash of avatars as a lookup and replacement for all comments in the | ||
# thread. | ||
def mentions_builder(comments) | ||
@mentions_builder ||= begin | ||
users = mentioned_users(Array(comments)) | ||
|
||
matcher = /#{users.map { |user| '@' + user.email }.join('|')}/ | ||
rules = users.each_with_object({}) do |user, hash| | ||
hash['@' + user.email] = view_context.avatar_image(user, size: 20, include_name: true, class: 'gravatar gravatar-inline') | ||
end | ||
|
||
[matcher, rules] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module AvatarHelper | ||
def avatar_url(user, options = {}) | ||
if user.nil? | ||
image_path('profile.png') | ||
else | ||
gravatar_id = Digest::MD5.hexdigest(user.email.downcase) | ||
size = options.fetch(:size, 64).to_i | ||
"https://secure.gravatar.com/avatar/#{gravatar_id}.png?r=PG&s=#{size}" | ||
end | ||
end | ||
|
||
def avatar_image(user, options = {}) | ||
alt = options.fetch(:alt, "#{user.email}'s avatar") | ||
fallback_image = options.fetch(:fallback_image, image_path('logo_small.png')) | ||
include_name = options.fetch(:include_name, false) | ||
size = options.fetch(:size, 73) | ||
title = options.fetch(:title, user.email) | ||
klass = options.fetch(:class, 'gravatar') | ||
|
||
content_tag :span, class: klass do | ||
image_tag( | ||
avatar_url(user, size: size), | ||
alt: alt, | ||
data: { fallback_image: fallback_image }, | ||
title: title, | ||
height: size, | ||
width: size, | ||
style: "width: #{size}px; height: #{size}px" | ||
) + (include_name ? ' ' + user.email : '') | ||
end | ||
end | ||
|
||
def comment_avatars(comment) | ||
# Support both concers/commented Comments collection for most views | ||
# as well as rendering 1-off's for polling and ajax requests. | ||
collection = comments ? comments.map(&:content) : Array(comment) | ||
matcher, rules = mentions_builder(collection) | ||
comment.gsub(matcher, rules) | ||
end | ||
|
||
def comment_formatter(comment) | ||
comment_avatars(simple_format(h(comment))).html_safe | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
$('#<%= dom_id(@comment) %> form').hide(); | ||
$('#<%= dom_id(@comment) %> .content').html("<%= j(simple_format(h(@comment.content))) %>"); | ||
$('#<%= dom_id(@comment) %> .content').html("<%= j(comment_formatter(@comment.content)) %>"); | ||
$('#<%= dom_id(@comment) %> .content').show(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters