diff --git a/app/helpers/posts_helper.rb b/app/helpers/posts_helper.rb index 378ad5a71..a521b20e8 100644 --- a/app/helpers/posts_helper.rb +++ b/app/helpers/posts_helper.rb @@ -48,10 +48,6 @@ module PostsHelper end end - def post_favlist(post) - post.favorited_users.reverse_each.map {|user| link_to_user(user)}.join(", ").html_safe - end - def is_pool_selected?(pool) return false if params.key?(:q) return false if params.key?(:favgroup_id) diff --git a/app/models/post.rb b/app/models/post.rb index e89d79e34..4e3300515 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -56,6 +56,7 @@ class Post < ApplicationRecord has_many :approvals, :class_name => "PostApproval", :dependent => :destroy has_many :disapprovals, :class_name => "PostDisapproval", :dependent => :destroy has_many :favorites + has_many :favorited_users, through: :favorites, source: :user has_many :replacements, class_name: "PostReplacement", :dependent => :destroy attr_accessor :old_tag_string, :old_parent_id, :old_source, :old_rating, :has_constraints, :disable_versioning, :view_count @@ -802,14 +803,11 @@ class Post < ApplicationRecord false end - # users who favorited this post, ordered by users who favorited it first - def favorited_users - favorited_user_ids = fav_string.scan(/\d+/).map(&:to_i) - visible_users = User.find(favorited_user_ids).select do |user| - Pundit.policy!([CurrentUser.user, nil], user).can_see_favorites? + # Users who publicly favorited this post, ordered by time of favorite. + def visible_favorited_users(viewer) + favorited_users.order("favorites.id DESC").select do |fav_user| + Pundit.policy!([viewer, nil], fav_user).can_see_favorites? end - ordered_users = visible_users.index_by(&:id).slice(*favorited_user_ids).values - ordered_users end def favorite_groups diff --git a/app/views/favorites/_update.js.erb b/app/views/favorites/_update.js.erb index 73b71ec61..0c2ece4aa 100644 --- a/app/views/favorites/_update.js.erb +++ b/app/views/favorites/_update.js.erb @@ -14,7 +14,7 @@ $(".fav-buttons").toggleClass("fav-buttons-false").toggleClass("fav-buttons-true <% if policy(@post).can_view_favlist? %> var fav_count = <%= @post.fav_count %>; - $("#favlist").html("<%= j post_favlist(@post) %>"); + $("#favlist").html("<%= j render "posts/partials/show/favorite_list", post: @post %>"); if (fav_count === 0) { $("#show-favlist-link, #hide-favlist-link, #favlist").hide(); diff --git a/app/views/posts/partials/show/_favorite_list.html.erb b/app/views/posts/partials/show/_favorite_list.html.erb new file mode 100644 index 000000000..863a24a7a --- /dev/null +++ b/app/views/posts/partials/show/_favorite_list.html.erb @@ -0,0 +1,2 @@ +<%# post %> +<%= safe_join(post.visible_favorited_users(CurrentUser.user).map { |user| link_to_user(user) }, ", ") %> diff --git a/app/views/posts/partials/show/_information.html.erb b/app/views/posts/partials/show/_information.html.erb index dcfc2b11c..51ce633b2 100644 --- a/app/views/posts/partials/show/_information.html.erb +++ b/app/views/posts/partials/show/_information.html.erb @@ -37,7 +37,9 @@ <% if policy(post).can_view_favlist? %> <%= link_to "Show »", "#", id: "show-favlist-link", style: ("display: none;" if post.fav_count == 0) %> <%= link_to "« Hide", "#", id: "hide-favlist-link", style: "display: none;" %> - + <% end %>
  • Status: