Skip to content

Instantly share code, notes, and snippets.

@takahashim
Created November 7, 2022 10:00
Show Gist options
  • Save takahashim/e186e0eb9af90b939a69cc9e9ec94bb5 to your computer and use it in GitHub Desktop.
Save takahashim/e186e0eb9af90b939a69cc9e9ec94bb5 to your computer and use it in GitHub Desktop.
git diff 4370eec49fdc6400c5603890c85c45a0e235d8d6 decidim-comments > decidim-comments.diff
diff --git a/decidim-comments/app/cells/decidim/comments/comments/show.erb b/decidim-comments/app/cells/decidim/comments/comments/show.erb
index 982366a..f21dc52 100644
--- a/decidim-comments/app/cells/decidim/comments/comments/show.erb
+++ b/decidim-comments/app/cells/decidim/comments/comments/show.erb
@@ -20,6 +20,11 @@
<%= cell("decidim/comments/comment_thread", comment, order: order) %>
<% end %>
</div>
+ <% if comment_limited? %>
+ <%= link_to commentable_path(limit: 0), class: "button primary expanded" do %>
+ <span><%= t("decidim.components.comments.show_all_comments") %></a></span>
+ <% end %>
+ <% end %>
<%= add_comment %>
<%= user_comments_blocked_warning %>
</div>
diff --git a/decidim-comments/app/cells/decidim/comments/comments_cell.rb b/decidim-comments/app/cells/decidim/comments/comments_cell.rb
index 5635461..a3d847f 100644
--- a/decidim-comments/app/cells/decidim/comments/comments_cell.rb
+++ b/decidim-comments/app/cells/decidim/comments/comments_cell.rb
@@ -46,7 +46,7 @@ def comments
if single_comment?
[single_comment]
else
- SortedComments.for(model, order_by: order)
+ SortedComments.for(model, order_by: order, limit: limit)
end
end
@@ -78,6 +78,10 @@ def order
options[:order] || "older"
end
+ def limit
+ options[:limit] || nil
+ end
+
def decidim
Decidim::Core::Engine.routes.url_helpers
end
@@ -153,6 +157,16 @@ def blocked_comments_for_unauthorized_user_warning_link
t("decidim.components.comments.blocked_comments_for_unauthorized_user_warning")
end
end
+
+ def comment_limited?
+ if options[:limit]
+ return false if options[:limit].to_i == 0
+
+ comments_count > options[:limit].to_i
+ else
+ comments_count > Decidim::Comments::SortedComments.comments_limit
+ end
+ end
end
end
end
diff --git a/decidim-comments/app/controllers/decidim/comments/comments_controller.rb b/decidim-comments/app/controllers/decidim/comments/comments_controller.rb
index fd394c3..e8cf0fb 100644
--- a/decidim-comments/app/controllers/decidim/comments/comments_controller.rb
+++ b/decidim-comments/app/controllers/decidim/comments/comments_controller.rb
@@ -11,7 +11,7 @@ class CommentsController < Decidim::Comments::ApplicationController
before_action :set_commentable, except: [:destroy, :update]
before_action :ensure_commentable!, except: [:destroy, :update]
- helper_method :root_depth, :commentable, :order, :reply?, :reload?
+ helper_method :root_depth, :commentable, :order, :limit, :reply?, :reload?
def index
enforce_permission_to :read, :comment, commentable: commentable
@@ -19,6 +19,7 @@ def index
@comments = SortedComments.for(
commentable,
order_by: order,
+ limit: limit,
after: params.fetch(:after, 0).to_i
)
@comments_count = commentable.comments_count
@@ -158,7 +159,21 @@ def reply?(comment)
end
def order
- params.fetch(:order, "older")
+ params_order = params.fetch(:order, nil)
+ if params_order
+ if cookies[Decidim.config.consent_cookie_name].present? # cookies_accepted?
+ cookies['comment_default_order'] = params_order
+ end
+ params_order
+ elsif cookies['comment_default_order'] && cookies[Decidim.config.consent_cookie_name].present? # cookies_accepted?
+ cookies['comment_default_order']
+ else
+ 'older'
+ end
+ end
+
+ def limit
+ params.fetch(:limit, nil)
end
def reload?
diff --git a/decidim-comments/app/queries/decidim/comments/sorted_comments.rb b/decidim-comments/app/queries/decidim/comments/sorted_comments.rb
index 3f50f26..4a74ae7 100644
--- a/decidim-comments/app/queries/decidim/comments/sorted_comments.rb
+++ b/decidim-comments/app/queries/decidim/comments/sorted_comments.rb
@@ -4,8 +4,14 @@ module Decidim
module Comments
# A class used to find comments for a commentable resource
class SortedComments < Rectify::Query
+ COMMENTS_LIMIT = 100
+
attr_reader :commentable
+ def self.comments_limit
+ Rails.application.config.respond_to?(:default_comments_limit) ? Rails.application.config.default_comments_limit : COMMENTS_LIMIT
+ end
+
# Syntactic sugar to initialize the class and return the queried objects.
#
# commentable - a resource that can have comments
@@ -34,6 +40,17 @@ def query
.not_hidden
.includes(:author, :user_group, :up_votes, :down_votes)
+ limit = (@options[:limit] || Decidim::Comments::SortedComments.comments_limit).to_i
+ if limit.positive?
+ if scope.is_a?(Array)
+ scope = scope.take(limit)
+ else
+ scope = scope.limit(limit)
+ end
+ else
+ # no limit
+ end
+
case @options[:order_by]
when "older"
order_by_older(scope)
diff --git a/decidim-comments/config/locales/en.yml b/decidim-comments/config/locales/en.yml
index 546f750..eea2121 100644
--- a/decidim-comments/config/locales/en.yml
+++ b/decidim-comments/config/locales/en.yml
@@ -99,6 +99,7 @@ en:
title:
one: "%{count} comment"
other: "%{count} comments"
+ show_all_comments: "Show all comments"
down_vote_button:
text: I disagree with this comment
edit_comment_modal_form:
diff --git a/decidim-comments/config/locales/ja.yml b/decidim-comments/config/locales/ja.yml
index e1758c9..a41f3ea 100644
--- a/decidim-comments/config/locales/ja.yml
+++ b/decidim-comments/config/locales/ja.yml
@@ -95,6 +95,7 @@ ja:
single_comment_warning_title: コメントを1件のみ表示しています
title:
other: "%{count} 件のコメント"
+ show_all_comments: "すべてのコメントを表示する"
down_vote_button:
text: このコメントに同意しません
edit_comment_modal_form:
diff --git a/decidim-comments/lib/decidim/comments/comments_helper.rb b/decidim-comments/lib/decidim/comments/comments_helper.rb
index e1969bd..ffe770a 100644
--- a/decidim-comments/lib/decidim/comments/comments_helper.rb
+++ b/decidim-comments/lib/decidim/comments/comments_helper.rb
@@ -23,12 +23,14 @@ def comments_for(resource, options = {})
def inline_comments_for(resource, options = {})
return unless resource.commentable?
+ limit = options[:limit] || params[:limit]
cell(
"decidim/comments/comments",
resource,
machine_translations: machine_translations_toggled?,
single_comment: params.fetch("commentId", nil),
- order: options[:order],
+ limit: limit,
+ order: options[:order] || cookies['comment_default_order'],
polymorphic: options[:polymorphic]
).to_s
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment