forked from codeforjapan/decidim-cfj
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d243a70
commit 75d2acf
Showing
5 changed files
with
97 additions
and
0 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
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> |
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,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 |
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,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 |
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