Skip to content

Commit

Permalink
Add new content block: Last Comment
Browse files Browse the repository at this point in the history
  • Loading branch information
takahashim committed Dec 23, 2024
1 parent d243a70 commit 75d2acf
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app/cells/decidim/content_blocks/last_comment/show.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<section id="last_comment" class="home__section">
<div class="home__section-header">
<h2 class="home__section-title">
<%= t("decidim.content_blocks.last_comment.title") %>
</h2>
<%= link_to last_activities_path, class: "button button__sm button__text-secondary" do %>
<%= t("view_all", scope: "decidim.content_blocks.last_comment") %>
<%= icon "arrow-right-line", class: "fill-current" %>
<% end %>
</div>
<% valid_comments.each_slice(4) do |column_activities| %>
<%= cell "decidim/activities", column_activities %>
<% end %>
</section>
67 changes: 67 additions & 0 deletions app/cells/decidim/content_blocks/last_comment_cell.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# frozen_string_literal: true

module Decidim
module ContentBlocks
# A cell to be rendered as a content block with the latest comments performed
# in a Decidim Organization.
class LastCommentCell < Decidim::ViewModel
include Decidim::Core::Engine.routes.url_helpers

def show
return if valid_comments.empty?

render
end

# The comments to be displayed at the content block.
#
# We need to build the collection this way because an ActionLog has
# polymorphic relations to different kind of models, and these models
# might not be available (a proposal might have been hidden or withdrawn).
#
# Since these conditions can't always be filtered with a database search
# we ask for more comments than we actually need and then loop until there
# are enough of them.
#
# Returns an Array of ActionLogs.
def valid_comments
return @valid_comments if defined?(@valid_comments)

valid_comments_count = 0
@valid_comments = []

comments.includes([:user]).each do |comment|
break if valid_comments_count == comments_to_show

if comment.visible_for?(current_user)
@valid_comments << comment
valid_comments_count += 1
end
end

@valid_comments
end

private

# A MD5 hash of model attributes because is needed because
# it ensures the cache version value will always be the same size
def cache_hash
hash = []
hash << "decidim/content_blocks/last_comment"
hash << Digest::MD5.hexdigest(valid_comments.map(&:cache_key_with_version).to_s)
hash << I18n.locale.to_s

hash.join(Decidim.cache_key_separator)
end

def comments
@comments ||= Decidim::LastActivity.new(current_organization, current_user: current_user).query.where(resource_type: 'Decidim::Comments::Comment').limit(comments_to_show * 6)
end

def comments_to_show
options[:comments_count] || 16
end
end
end
end
7 changes: 7 additions & 0 deletions config/initializers/content_block.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

Decidim.content_blocks.register(:homepage, :last_comment) do |content_block|
content_block.cell = "decidim/content_blocks/last_comment"
content_block.public_name_key = "decidim.content_blocks.last_comment.name"
content_block.default!
end
5 changes: 5 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ en:
title: Title
body: Body
decidim:
content_blocks:
last_comment:
name: 最近のコメント
title: 最近のコメント
view_all: すべて表示
debates:
debates:
versions:
Expand Down
4 changes: 4 additions & 0 deletions config/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ ja:
last_activity:
name: 最近のアクティビティ
title: 最近のアクティビティ
last_comment:
name: 最近のコメント
title: 最近のコメント
view_all: すべて表示
debates:
debates:
filters:
Expand Down

0 comments on commit 75d2acf

Please sign in to comment.