Skip to content

Commit

Permalink
Create a content block for open debates on the home of a space (decid…
Browse files Browse the repository at this point in the history
…im#13510)

* add content blocks for debates

* fix debetes order

* add specs

* change HighlightedDebatesCell

* change HighlightedDebatesCell

* refactor HighlightedDebatesCell

* Update decidim-debates/spec/cells/decidim/debates/content_blocks/highlighted_debates_cell_spec.rb

Co-authored-by: Alexandru Emil Lupu <[email protected]>

* fix test

---------

Co-authored-by: Alexandru Emil Lupu <[email protected]>
  • Loading branch information
antopalidi and alecslupu authored Oct 11, 2024
1 parent 9498bcd commit d73f97e
Show file tree
Hide file tree
Showing 14 changed files with 241 additions and 3 deletions.
1 change: 1 addition & 0 deletions config/i18n-tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ ignore_unused:
- decidim.participatory_processes.content_blocks.*.name
- decidim.participatory_process_groups.content_blocks.*.name
- decidim.assemblies.content_blocks.*.name
- decidim.debates.content_blocks.*.name
- decidim.initiatives.content_blocks.*.name
- decidim.meetings.content_blocks.*.name
- decidim.blogs.content_blocks.*.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,24 @@ def self.register!
content_block.public_name_key = "decidim.application.photos.related_photos"
end

register_highlighted_debates
register_highlighted_meetings
register_highlighted_posts
register_highlighted_proposals
register_highlighted_results
register_related_processes
end

def self.register_highlighted_debates
return unless Decidim.module_installed?(:debates)

Decidim.content_blocks.register(:assembly_homepage, :highlighted_debates) do |content_block|
content_block.cell = "decidim/debates/content_blocks/highlighted_debates"
content_block.public_name_key = "decidim.debates.content_blocks.highlighted_debates.name"
content_block.component_manifest_name = "debates"
end
end

def self.register_highlighted_meetings
return unless Decidim.module_installed?(:meetings)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ module Decidim
module Blogs
module ContentBlocks
class HighlightedPostsCell < Decidim::ContentBlocks::HighlightedElementsCell
include Decidim::ComponentPathHelper
include Decidim::CardHelper

def show
render unless posts_count.zero?
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Decidim
module ContentBlocks
class HighlightedElementsCell < BaseCell
include Decidim::ContentBlocks::HasRelatedComponents
include Decidim::ComponentPathHelper
include Decidim::CardHelper

def published_components
@published_components ||= if model.settings.try(:component_id).present?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div class="content-block__title">
<h2 class="h2 decorator"><%= single_component ? translated_attribute(single_component.name) : t("name", scope: "decidim.components.debates") %></h2>
<div class="label text-lg"><%= items_count %></div>
<% if single_component %>
<%= link_to decidim_debates.root_path, class: "button button__sm button__text-secondary" do %>
<span><%= t("decidim.debates.content_blocks.highlighted_debates.see_all") %></span>
<%= icon "arrow-right-line" %>
<% end %>
<% end %>
</div>

<div class="participatory-space__block-grid">
<% debates_to_render.each do |debate| %>
<%= cell "decidim/debates/debate_g", debate %>
<% end %>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

module Decidim
module Debates
module ContentBlocks
class HighlightedDebatesCell < Decidim::ContentBlocks::HighlightedElementsCell
def show
render unless items_count.zero?
end

private

def debates
@debates ||= Decidim::Debates::Debate.open.where(component: published_components).updated_at_desc
end

def decidim_debates
return unless single_component

Decidim::EngineRouter.main_proxy(single_component)
end

def single_component
@single_component ||= published_components.one? ? published_components.first : nil
end

def debates_to_render
@debates_to_render ||= debates.includes([:author, :component]).limit(limit)
end

def items_count
debates_to_render.size
end

def cache_hash
hash = []
hash << "decidim/debates/content_blocks/highlighted_debates"
hash << debates.cache_key_with_version
hash << I18n.locale.to_s
hash.join(Decidim.cache_key_separator)
end

def limit
3
end
end
end
end
end
12 changes: 12 additions & 0 deletions decidim-debates/app/cells/decidim/debates/debate_g/show.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<%= link_to resource_path, class: classes[:default], id: resource_id do %>
<div class="<%= classes[:text] %>">
<%= content_tag title_tag, title, class: title_class %>
<%= description if show_description? %>

<% if metadata_cell.present? %>
<div class="<%= classes[:metadata] %>">
<%= cell metadata_cell, resource, links: false, **options %>
</div>
<% end %>
</div>
<% end %>
23 changes: 23 additions & 0 deletions decidim-debates/app/cells/decidim/debates/debate_g_cell.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module Decidim
module Debates
# This cell renders the Grid (:g) debate card
# for a given instance of a Debate
class DebateGCell < Decidim::CardGCell
def show
render
end

private

def show_description?
true
end

def metadata_cell
"decidim/debates/debate_metadata_g"
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Decidim
module Debates
# This cell renders metadata for an instance of a Debate
class DebateMetadataGCell < Decidim::Debates::DebateCardMetadataCell
private

def debate_items
[author_item, comments_count_item]
end
end
end
end
1 change: 1 addition & 0 deletions decidim-debates/app/models/decidim/debates/debate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Debate < Debates::ApplicationRecord
index_on_create: ->(debate) { debate.visible? },
index_on_update: ->(debate) { debate.visible? })

scope :updated_at_desc, -> { order(arel_table[:updated_at].desc) }
scope :open, -> { where(closed_at: nil) }
scope :closed, -> { where.not(closed_at: nil) }
scope :authored_by, ->(author) { where(author:) }
Expand Down
4 changes: 4 additions & 0 deletions decidim-debates/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ en:
close: "%{user_name} closed the %{resource_name} debate on the %{space_name} space"
create: "%{user_name} created the %{resource_name} debate on the %{space_name} space"
update: "%{user_name} updated the %{resource_name} debate on the %{space_name} space"
content_blocks:
highlighted_debates:
name: Debates
see_all: See all debates
debate_m:
commented_time_ago: Commented %{time} ago
debates:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

require "spec_helper"

describe Decidim::Debates::ContentBlocks::HighlightedDebatesCell, type: :cell do
subject { cell("decidim/debates/content_blocks/highlighted_debates", content_block).call }

controller Decidim::Debates::DebatesController

let(:organization) { create(:organization) }
let(:participatory_space) { create(:participatory_process, organization:) }
let(:component) { create(:debates_component, participatory_space:) }
let(:manifest_name) { :highlighted_debates }
let(:scope_name) { :participatory_process_homepage }
let(:content_block) { create(:content_block, organization:, manifest_name:, scope_name:, scoped_resource_id: participatory_space.id) }

context "with 1 open debate" do
let!(:debate) { create(:debate, title: { en: "Debate title" }, component:) }

it "renders the open debate" do
expect(subject).to have_content("Debate title")
expect(subject).to have_css(".card__grid", count: 1)
end
end

context "with 4 debates (3 open, 1 closed)" do
let!(:debate_old) { create(:debate, title: { en: "Old Debate" }, component:, created_at: 1.year.ago, updated_at: 1.year.ago, closed_at: nil) }
let!(:debate1) { create(:debate, title: { en: "Recent Debate 1" }, component:, created_at: 3.days.ago, updated_at: 3.days.ago) }
let!(:debate2) { create(:debate, title: { en: "Recent Debate 2" }, component:, created_at: 2.days.ago, updated_at: 2.days.ago) }
let!(:debate3) { create(:debate, title: { en: "Recent Debate 3" }, component:, created_at: 1.day.ago, updated_at: 1.day.ago) }
let!(:debate_closed) { create(:debate, title: { en: "Closed Debate" }, component:, closed_at: 1.day.ago) }

it "renders only 3 most recent open debates" do
expect(subject).to have_no_content("Closed Debate")

expect(subject).to have_no_content("Old Debate")

expect(subject).to have_css(".card__grid", count: 3)
expect(subject).to have_content("Recent Debate 1")
expect(subject).to have_content("Recent Debate 2")
expect(subject).to have_content("Recent Debate 3")
end
end

context "with no debates" do
it "renders nothing" do
expect(subject).to have_no_content("Debate title")
expect(subject).to have_no_css(".card__grid", count: 1)
end
end
end
46 changes: 46 additions & 0 deletions decidim-debates/spec/cells/decidim/debates/debate_g_cell_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

require "spec_helper"

module Decidim::Debates
describe DebateGCell, type: :cell do
controller Decidim::Debates::DebatesController

subject { cell_html }

let(:my_cell) { cell("decidim/debates/debate_g", debate) }
let(:cell_html) { my_cell.call }
let(:created_at) { 1.month.ago }
let(:component) { create(:debates_component) }
let!(:debate) { create(:debate, description: { en: "Description for test" }, component:, created_at:) }
let(:model) { debate }
let(:user) { create(:user, organization: debate.participatory_space.organization) }

before do
allow(controller).to receive(:current_user).and_return(user)
end

context "when rendering" do
it "renders the grid card" do
expect(subject).to have_css("[id^='debates__debate']")
end

it "renders the description" do
expect(subject).to have_content("Description for test")
end

it "renders the title" do
expect(subject).to have_content(translated_attribute(model.title))
end

context "when the description has a link" do
let!(:debate) { create(:debate, description:, component:, created_at:) }
let(:description) { { en: "This is a description with a link to <a href='http://example.org'>example.org</a>" } }

it "renders the description" do
expect(subject).to have_content("This is a description with a link to example.org")
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,24 @@ def self.register!
end
end

register_highlighted_debates
register_highlighted_meetings
register_highlighted_posts
register_highlighted_proposals
register_highlighted_results
register_related_assemblies
end

def self.register_highlighted_debates
return unless Decidim.module_installed?(:debates)

Decidim.content_blocks.register(:participatory_process_homepage, :highlighted_debates) do |content_block|
content_block.cell = "decidim/debates/content_blocks/highlighted_debates"
content_block.public_name_key = "decidim.debates.content_blocks.highlighted_debates.name"
content_block.component_manifest_name = "debates"
end
end

def self.register_highlighted_meetings
return unless Decidim.module_installed?(:meetings)

Expand Down

0 comments on commit d73f97e

Please sign in to comment.