forked from decidim/decidim
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a content block for open debates on the home of a space (decid…
…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
1 parent
9498bcd
commit d73f97e
Showing
14 changed files
with
241 additions
and
3 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
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
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
16 changes: 16 additions & 0 deletions
16
decidim-debates/app/cells/decidim/debates/content_blocks/highlighted_debates/content.erb
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,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> |
49 changes: 49 additions & 0 deletions
49
decidim-debates/app/cells/decidim/debates/content_blocks/highlighted_debates_cell.rb
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,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
12
decidim-debates/app/cells/decidim/debates/debate_g/show.erb
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,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
23
decidim-debates/app/cells/decidim/debates/debate_g_cell.rb
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,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 |
14 changes: 14 additions & 0 deletions
14
decidim-debates/app/cells/decidim/debates/debate_metadata_g_cell.rb
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 @@ | ||
# 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 |
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
51 changes: 51 additions & 0 deletions
51
decidim-debates/spec/cells/decidim/debates/content_blocks/highlighted_debates_cell_spec.rb
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,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
46
decidim-debates/spec/cells/decidim/debates/debate_g_cell_spec.rb
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,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 |
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