Skip to content

Commit

Permalink
Support comments limit again
Browse files Browse the repository at this point in the history
  • Loading branch information
takahashim committed Dec 26, 2021
1 parent e33df8c commit 9a57871
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ruby RUBY_VERSION

gem "decidim", "0.24.3"

# gem "decidim-comments", path: "decidim-comments"
gem "decidim-comments", path: "decidim-comments"

gem "decidim-decidim_awesome", "~> 0.7.0"

Expand Down
13 changes: 9 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
PATH
remote: decidim-comments
specs:
decidim-comments (0.24.3)
decidim-core (= 0.24.3)
jquery-rails (~> 4.4)
redcarpet (~> 3.5, >= 3.5.1)

PATH
remote: decidim-user_extension
specs:
Expand Down Expand Up @@ -195,10 +203,6 @@ GEM
decidim-core (= 0.24.3)
kaminari (~> 1.2, >= 1.2.1)
searchlight (~> 4.1)
decidim-comments (0.24.3)
decidim-core (= 0.24.3)
jquery-rails (~> 4.4)
redcarpet (~> 3.5, >= 3.5.1)
decidim-core (0.24.3)
active_link_to (~> 1.0)
anchored (~> 1.1)
Expand Down Expand Up @@ -885,6 +889,7 @@ DEPENDENCIES
bootsnap (~> 1.3)
byebug (~> 11.0)
decidim (= 0.24.3)
decidim-comments!
decidim-decidim_awesome (~> 0.7.0)
decidim-dev (= 0.24.3)
decidim-user_extension!
Expand Down
5 changes: 5 additions & 0 deletions decidim-comments/app/cells/decidim/comments/comments/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
16 changes: 15 additions & 1 deletion decidim-comments/app/cells/decidim/comments/comments_cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -76,6 +76,10 @@ def order
options[:order] || "older"
end

def limit
options[:limit] || nil
end

def decidim
Decidim::Core::Engine.routes.url_helpers
end
Expand Down Expand Up @@ -129,6 +133,16 @@ def user_comments_blocked?

!model.user_allowed_to_comment?(current_user)
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ class CommentsController < Decidim::Comments::ApplicationController
before_action :set_commentable
before_action :ensure_commentable!

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

@comments = SortedComments.for(
commentable,
order_by: order,
limit: limit,
after: params.fetch(:after, 0).to_i
)
@comments_count = commentable.comments.count
Expand Down Expand Up @@ -104,6 +105,10 @@ def order
params.fetch(:order, "older")
end

def limit
params.fetch(:limit, nil)
end

def reload?
params.fetch(:reload, 0).to_i == 1
end
Expand Down
17 changes: 17 additions & 0 deletions decidim-comments/app/queries/decidim/comments/sorted_comments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -40,6 +46,17 @@ def query
)
end

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)
Expand Down
1 change: 1 addition & 0 deletions decidim-comments/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ en:
title:
one: "%{count} comment"
other: "%{count} comments"
show_all_comments: "Show all comments"
down_vote_button:
text: I disagree with this comment
up_vote_button:
Expand Down
1 change: 1 addition & 0 deletions decidim-comments/config/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ ja:
single_comment_warning_title: コメントを1件のみ表示しています
title:
other: "%{count} 件のコメント"
show_all_comments: "すべてのコメントを表示する"
down_vote_button:
text: このコメントに同意しません
up_vote_button:
Expand Down
2 changes: 2 additions & 0 deletions decidim-comments/lib/decidim/comments/comments_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ def comments_for(resource)
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),
limit: limit,
order: options[:order]
).to_s
end
Expand Down

0 comments on commit 9a57871

Please sign in to comment.