Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(component_related_blocks): move shared logic to base cell #32

Merged
merged 14 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ def meeting_path(meeting)

def meetings
@meetings ||= Meetings::Meeting.upcoming.where(
component: meeting_component
component: component || components
).limit(meetings_to_show).order(start_time: :asc)
end

private

def manifest_name
"meetings"
end

# 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
Expand All @@ -40,10 +44,6 @@ def cache_hash
hash.join("/")
end

def meeting_component
@meeting_component ||= (Component.find_by(id: model.settings.component_id) || Component.where(manifest_name: "meetings"))
end

def meetings_to_show
model.settings.count || 3
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
<%= settings_fields.translated :text_field, :link_text, label: t(".link_text") %>
<%= settings_fields.translated :text_field, :link_url, label: t(".link_url") %>
<%= settings_fields.number_field :count, label: t(".count") %>

<% if component %>
<p class="callout secondary">
<%= t(".info", component: translated_attribute(component.name), space: translated_attribute(component.participatory_space.title)) %>
</p>
<% end %>

<%= settings_fields.select :component_id, available_components("meetings") %>
<%= settings_fields.select :component_id, available_components %>
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module ContentBlocks
class AlternativeUpcomingMeetingsSettingsFormCell < BaseCell
alias form model

def component
@component ||= Decidim::Component.find_by(id: form.object.settings.try(:component_id))
def manifest_name
"meetings"
end
end
end
Expand Down
14 changes: 11 additions & 3 deletions app/cells/decidim/alternative_landing/content_blocks/base_cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,22 @@ def participatory_spaces
].flatten.compact
end

def available_components(manifest_name)
Decidim::Component.published.where(participatory_space: participatory_spaces, manifest_name: manifest_name).map do |component|
def available_components
@available_components ||= components.where(manifest_name: manifest_name).map do |component|
["#{translated_attribute(component.name)} (#{translated_attribute(component.participatory_space.title)})", component.id]
end.unshift [t(".all"), nil]
end

def component
@component ||= Decidim::Component.find_by(id: form.object.settings.try(:component_id))
@component ||= components.find_by(id: (defined?(form) ? form.object : model).settings.try(:component_id))
end

def components
@components ||= Decidim::Component.where(participatory_space: participatory_spaces)
fblupi marked this conversation as resolved.
Show resolved Hide resolved
end

def manifest_name
raise NotImplementedError
end

def colors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ def post_path(post)

def posts
@posts ||= Blogs::Post.where(
component: blog_components.find_by(id: model.settings.component_id) || blog_components
component: component || components
).limit(posts_to_show).order(created_at: :desc)
end

private

def manifest_name
"blogs"
end

# 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
Expand All @@ -39,10 +43,6 @@ def cache_hash
hash.join("/")
end

def blog_components
@blog_components ||= Component.published.where(manifest_name: "blogs")
end

def posts_to_show
model.settings.count || 3
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
<%= settings_fields.translated :text_field, :link_text, label: t(".link_text") %>
<%= settings_fields.translated :text_field, :link_url, label: t(".link_url") %>
<%= settings_fields.number_field :count, label: t(".count") %>

<% if component %>
<p class="callout secondary">
<%= t(".info", component: translated_attribute(component.name), space: translated_attribute(component.participatory_space.title)) %>
</p>
<% end %>

<%= settings_fields.select :component_id, available_components("blogs") %>
<%= settings_fields.select :component_id, available_components %>
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ module AlternativeLanding
module ContentBlocks
class LatestBlogPostsSettingsFormCell < BaseCell
alias form model

def manifest_name
"blogs"
end
end
end
end
Expand Down
18 changes: 16 additions & 2 deletions lib/decidim/alternative_landing/test/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,37 @@
factory :latest_blog_posts_block, parent: :alternative_landing_content_block do
manifest_name { :latest_blog_posts }

transient do
component_id { nil }
count { 3 }
end

settings do
{
title: generate_localized_title,
link_text: Decidim::Faker::Localized.word,
link_url: Decidim::Faker::Localized.literal("https://decidim.org")
link_url: Decidim::Faker::Localized.literal("https://decidim.org"),
component_id: component_id,
count: count
}
end
end

factory :alternative_upcoming_meetings_block, parent: :alternative_landing_content_block do
manifest_name { :alternative_upcoming_meetings }

transient do
component_id { nil }
count { 3 }
end

settings do
{
title: generate_localized_title,
link_text: Decidim::Faker::Localized.word,
link_url: Decidim::Faker::Localized.literal("https://decidim.org")
link_url: Decidim::Faker::Localized.literal("https://decidim.org"),
component_id: component_id,
count: count
}
end
end
Expand Down
10 changes: 6 additions & 4 deletions spec/shared/system_homepage_examples.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# frozen_string_literal: true

shared_examples "render all stack block elements" do |type|
let!(:content_block) do
type == "stack-horizontal" ? create(:stack_horizontal_block, organization: organization) : create(:stack_vertical_block, organization: organization)
let(:manifest_name) { type.gsub("-", "_") }
let(:content_block) do
Decidim::ContentBlock.find_by(organization: organization, manifest_name: manifest_name)
end

it "renders all elements" do
Expand Down Expand Up @@ -54,8 +55,9 @@
end

shared_examples "render all cover block elements" do |type|
let!(:content_block) do
type == "cover-full" ? create(:cover_full_block, organization: organization) : create(:cover_half_block, organization: organization)
let(:manifest_name) { type.gsub("-", "_") }
let(:content_block) do
Decidim::ContentBlock.find_by(organization: organization, manifest_name: manifest_name)
end

it "renders all elements" do
Expand Down
10 changes: 6 additions & 4 deletions spec/system/admin_homepage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

let(:organization) { create(:organization) }
let(:user) { create(:user, :admin, :confirmed, organization: organization) }
let!(:meeting) { create :meeting }
let!(:post) { create(:post) }
let(:blogs_component) { create(:component, manifest_name: "blogs", organization: organization) }
let(:meetings_component) { create(:component, manifest_name: "meetings", organization: organization) }
let!(:post) { create(:post, component: blogs_component) }
let!(:meeting) { create :meeting, component: meetings_component }

before do
switch_to_host(organization.host)
Expand Down Expand Up @@ -51,10 +53,10 @@
end

context "when editing a persisted content block" do
let!(:alternative_upcoming_meetings_block) { create :alternative_upcoming_meetings_block, organization: organization, scope_name: :homepage }
let!(:alternative_upcoming_meetings_block) { create :alternative_upcoming_meetings_block, organization: organization, scope_name: :homepage, component_id: meetings_component.id }
let!(:cover_full_block) { create :content_block, organization: organization, manifest_name: "cover_full", scope_name: :homepage }
let!(:cover_half_block) { create :cover_half_block, organization: organization, scope_name: :homepage }
let!(:latest_blog_posts_block) { create :latest_blog_posts_block, organization: organization, scope_name: :homepage }
let!(:latest_blog_posts_block) { create :latest_blog_posts_block, organization: organization, scope_name: :homepage, component_id: blogs_component.id }
let!(:stack_horizontal_block) { create :stack_horizontal_block, organization: organization, scope_name: :homepage }
let!(:stack_vertical_block) { create :stack_vertical_block, organization: organization, scope_name: :homepage }
let!(:tiles_block) { create :tiles_block, organization: organization, scope_name: :homepage }
Expand Down
Loading