Skip to content

Commit

Permalink
Participatory process groups content blocks (#12)
Browse files Browse the repository at this point in the history
* Upgrade Decidim version to 0.23

* Fix rubocop offenses

* Validate blueprints image file size using upload_maximum_file_size setting for organization

* Add migration to add a reference in blueprints table to content blocks

* Add association with content blocks to Blueprint model and factory

* Allow passing the content block to SaveBlueprints command from form

* Include content_block in BluePrintForm

* Manage blueprints belonging to content block

* Register navigation map content block for participatory process groups homepage

* Include content block in tests for blueprints

* Add test for ContentBlockBlueprints query

* Add tests to check that blueprints association with content blocks

* Use different classes and title for navigation block in groups landing page

* Fix title display in navigation_map cell

* Include content block association in seeds file

* Remove console insertion in cell

* Add test for groups_navigation_map cell
  • Loading branch information
entantoencuanto authored Apr 8, 2021
1 parent ee6f2f0 commit 5199551
Show file tree
Hide file tree
Showing 25 changed files with 207 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<% if translated_title.present? %>
<h2 class="section-heading"><%= decidim_sanitize translated_title %></h2>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module Decidim
module NavigationMaps
module ContentBlocks
class GroupsNavigationMapCell < NavigationMapCell
def section_classes
"section home-section"
end

def wrapper_classes
nil
end

def row_classes
"row column"
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<%= stylesheet_link_tag "decidim/navigation_maps/navigation_maps" %>
<%= render partial: "styles", locals: { blueprints: valid_blueprints } %>

<section class="extended home-section">
<div class="wrapper-home">
<div class="row column text-center">
<section<%= class_tag(section_classes) %>>
<div<%= class_tag(wrapper_classes) %>>
<div<%= class_tag(row_classes) %>>

<% if translated_title.present? %>
<h3 class="heading3"><%= decidim_sanitize translated_title %></h3>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class NavigationMapCell < Decidim::ViewModel
include NavigationMaps::NavigationMapCellHelpers
include Decidim::SanitizeHelper

alias content_block model

view_paths << "#{Decidim::NavigationMaps::Engine.root}/app/cells/decidim/navigation_maps/content_blocks/navigation_map"

def show
Expand All @@ -16,6 +18,24 @@ def show
def translated_title
translated_attribute(model.settings.title)
end

def section_classes
"extended home-section"
end

def wrapper_classes
"wrapper-home"
end

def row_classes
"row column text-center"
end

def class_tag(class_string)
return if class_string.blank?

" class=\"#{class_string}\""
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<!-- Override rails default patch (using post via ajax) -->
<%= hidden_field_tag :_method, "post" %>
<%= hidden_field_tag :action, blueprints_path %>
<%= hidden_field_tag :content_block_id, content_block.id %>

<div class="navigation_maps admin">
<div class="callout" style="display:none" data-closable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class NavigationMapSettingsFormCell < Decidim::ViewModel
view_paths << "#{Decidim::NavigationMaps::Engine.root}/app/cells/decidim/navigation_maps/content_blocks/navigation_map_settings_form"

def blueprint_form(blueprint = nil)
blueprint ||= Blueprint.new
blueprint ||= Blueprint.new(content_block: content_block)
BlueprintForm.from_model(blueprint).with_context(organization: current_organization)
end

Expand Down
8 changes: 7 additions & 1 deletion app/commands/decidim/navigation_maps/save_blueprints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

module Decidim
module NavigationMaps
# This command creates or updates the blueprints for the organization.
# This command creates or updates the blueprints for the organization and
# content block.
class SaveBlueprints < Rectify::Command
# Creates a blueprint.
#
Expand Down Expand Up @@ -42,6 +43,7 @@ def call
def initialize_blueprint(form)
@blueprint = Blueprint.find_or_initialize_by(id: form.id) do |blueprint|
blueprint.organization = @forms.current_organization
blueprint.content_block = content_block
end
end

Expand All @@ -61,6 +63,10 @@ def destroy_areas_except(except)
@blueprint.areas.where.not(area_id: except).destroy_all
end

def content_block
Decidim::ContentBlock.where(organization: @forms.current_organization, manifest_name: :navigation_map).find_by(id: @forms.content_block_id)
end

def create_areas(blueprint)
blueprint.each do |key, area|
a = BlueprintArea.find_or_initialize_by(area_id: key, blueprint: @blueprint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ def save_settings
return unless params[:content_block][:settings]

@form = form(Decidim::Admin::ContentBlockForm).from_params(params)
Decidim::Admin::UpdateContentBlock.call(@form, content_block, :homepage)
Decidim::Admin::UpdateContentBlock.call(@form, content_block, content_block.scope_name&.to_sym)
end

def content_block
@content_block ||= Decidim::ContentBlock.for_scope(:homepage, organization: current_organization).find_by(manifest_name: :navigation_map)
@content_block ||= Decidim::ContentBlock.where(organization: current_organization, manifest_name: :navigation_map).find(params[:content_block_id])
end

# Convert blueprint data to an object
Expand Down
1 change: 1 addition & 0 deletions app/forms/decidim/navigation_maps/blueprint_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class BlueprintForm < Decidim::Form

attribute :blueprint, Object
attribute :id, Integer
attribute :decidim_content_block_id, Integer
attribute :remove, Boolean
attribute :image
translatable_attribute :title, String
Expand Down
1 change: 1 addition & 0 deletions app/forms/decidim/navigation_maps/blueprint_forms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Decidim
module NavigationMaps
class BlueprintForms < Decidim::Form
attribute :blueprints, Array[BlueprintForm]
attribute :content_block_id, Integer
end
end
end
1 change: 1 addition & 0 deletions app/models/decidim/navigation_maps/blueprint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Blueprint < ApplicationRecord
attribute :height, :integer, default: 475

belongs_to :organization, foreign_key: :decidim_organization_id, class_name: "Decidim::Organization"
belongs_to :content_block, foreign_key: :decidim_content_block_id, class_name: "Decidim::ContentBlock"
has_many :areas,
foreign_key: "decidim_navigation_maps_blueprint_id",
class_name: "Decidim::NavigationMaps::BlueprintArea",
Expand Down
20 changes: 20 additions & 0 deletions app/queries/decidim/navigation_maps/content_block_blueprints.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Decidim
module NavigationMaps
# This query finds the published blueprints for a content block
class ContentBlockBlueprints < Rectify::Query
def initialize(content_block)
@content_block = content_block
end

def query
Decidim::NavigationMaps::Blueprint.where(content_block: @content_block)
end

private

attr_reader :content_block
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class AddReferenceToContentBlockToBlueprintsTable < ActiveRecord::Migration[5.2]
def change
add_reference(
:decidim_navigation_maps_blueprints,
:decidim_content_block,
foreign_key: true,
index: { name: "decidim_navigation_maps_constraint_content_block" }
)
end
end
3 changes: 2 additions & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

organization = Decidim::Organization.first

Decidim::ContentBlock.create(
content_block = Decidim::ContentBlock.create(
decidim_organization_id: organization.id,
weight: 1,
scope_name: :homepage,
Expand All @@ -19,6 +19,7 @@

blueprint1 = Decidim::NavigationMaps::Blueprint.create(
organization: organization,
content_block: content_block,
image: File.new(File.join(seeds_root, "antarctica.png")),
title: Decidim::Faker::Localized.sentence(2),
description: Decidim::Faker::Localized.sentence(10)
Expand Down
10 changes: 10 additions & 0 deletions lib/decidim/navigation_maps/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ class Engine < ::Rails::Engine
settings.attribute :title, type: :text, translated: true
end
end

Decidim.content_blocks.register(:participatory_process_group_homepage, :navigation_map) do |content_block|
content_block.cell = "decidim/navigation_maps/content_blocks/groups_navigation_map"
content_block.public_name_key = "decidim.navigation_maps.content_blocks.name"
content_block.settings_form_cell = "decidim/navigation_maps/content_blocks/navigation_map_settings_form"

content_block.settings do |settings|
settings.attribute :title, type: :text, translated: true
end
end
end

initializer "decidim.navigation_maps.add_cells_view_paths" do
Expand Down
8 changes: 4 additions & 4 deletions lib/decidim/navigation_maps/navigation_map_cell_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ module NavigationMapCellHelpers
delegate :available_locales, to: :current_organization

def valid_blueprints
organization_blueprints.where.not(image: [nil, ""]).order(:created_at)
content_block_blueprints.where.not(image: [nil, ""]).order(:created_at)
end

def valid_blueprints?
valid_blueprints.any?
end

def blueprints
organization_blueprints.order(:created_at)
content_block_blueprints.order(:created_at)
end

private

def organization_blueprints
@organization_blueprints ||= OrganizationBlueprints.new(current_organization).query
def content_block_blueprints
@content_block_blueprints ||= (OrganizationBlueprints.new(current_organization) | ContentBlockBlueprints.new(content_block)).query
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/decidim/navigation_maps/test/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
image { Decidim::Dev.test_file("city.jpeg", "image/jpeg") }
title { Decidim::Faker::Localized.word }
description { generate_localized_title }
content_block { create(:content_block, organization: organization) }
end

factory :blueprint_area, class: "Decidim::NavigationMaps::BlueprintArea" do
Expand Down
9 changes: 8 additions & 1 deletion spec/commands/save_blueprints_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ module Decidim::NavigationMaps
subject { described_class.new(forms) }

let(:organization) { create :organization }
let(:content_block) { create :content_block, organization: organization, manifest_name: :navigation_map, scope_name: :homepage }

let(:forms) do
instance_double(
BlueprintForms,
blueprints: blueprints,
current_organization: organization
current_organization: organization,
content_block_id: content_block.id
)
end
let(:blueprints) { [form1, form2] }
Expand Down Expand Up @@ -74,6 +77,10 @@ module Decidim::NavigationMaps
it "creates all the blueprints" do
expect { subject.call }.to change(Blueprint, :count).by(2)
end

it "creates all blueprints belonging to content block" do
expect { subject.call }.to change(Blueprint.where(content_block: content_block), :count).by(2)
end
end

context "when one form is invalid" do
Expand Down
38 changes: 38 additions & 0 deletions spec/content_blocks/groups_navigation_map_cell_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require "spec_helper"

module Decidim::NavigationMaps::ContentBlocks
describe GroupsNavigationMapCell, type: :cell do
subject { cell(group_content_block.cell, group_content_block).call }

let(:organization) { create(:organization) }
let(:group_content_block) do
create(
:content_block,
organization: organization,
manifest_name: :navigation_map,
scope_name: :participatory_process_group_homepage
)
end
let(:organization_homepage_content_block) do
create(
:content_block,
organization: organization,
manifest_name: :navigation_map,
scope_name: :homepage
)
end
let!(:group_blueprint) { create(:blueprint, organization: organization, content_block: group_content_block) }
let!(:organization_homepage_blueprint) { create(:blueprint, organization: organization, content_block: organization_homepage_content_block) }

controller Decidim::PagesController

context "when there are blueprints in the content block" do
it "contains the map" do
expect(subject.to_s).to include(group_blueprint.image.url)
expect(subject.to_s).not_to include(organization_homepage_blueprint.image.url)
end
end
end
end
2 changes: 1 addition & 1 deletion spec/content_blocks/navigation_map_cell_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Decidim::NavigationMaps::ContentBlocks

let(:organization) { create(:organization) }
let(:content_block) { create :content_block, organization: organization, manifest_name: :navigation_map, scope_name: :homepage }
let!(:blueprint) { create(:blueprint, organization: organization) }
let!(:blueprint) { create(:blueprint, organization: organization, content_block: content_block) }

controller Decidim::PagesController

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Decidim::NavigationMaps::ContentBlocks

let(:organization) { create(:organization) }
let(:content_block) { create :content_block, organization: organization, manifest_name: :navigation_map, scope_name: :homepage }
let!(:blueprint) { create(:blueprint, organization: organization) }
let!(:blueprint) { create(:blueprint, organization: organization, content_block: content_block) }
let(:settings) { NavigationMapSettingsFormCell.new }

controller Decidim::Admin::ApplicationController
Expand Down
4 changes: 3 additions & 1 deletion spec/forms/blueprint_forms_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ module Decidim::NavigationMaps
subject { described_class.from_params(attributes).with_context(context) }

let(:organization) { create :organization }
let(:content_block) { create(:content_block, organization: organization) }
let(:attributes) do
{
"blueprints" => []
}
end
let(:context) do
{
"current_organization" => organization
"current_organization" => organization,
"content_block_id" => content_block.id
}
end

Expand Down
17 changes: 11 additions & 6 deletions spec/lib/navigation_map_cell_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@ module Decidim::NavigationMaps
Class.new do
include NavigationMapCellHelpers

def initialize(organization)
def initialize(organization, content_block)
@current_organization = organization
@content_block = content_block
end

attr_accessor :current_organization
attr_accessor :current_organization, :content_block
end
end

let(:instance) { klass.new(organization) }
let(:instance) { klass.new(organization, content_block) }
let(:organization) { create :organization }
let!(:blueprint1) { create(:blueprint, organization: organization, created_at: Time.current.beginning_of_day + 1.minute) }
let!(:blueprint2) { create(:blueprint, image: nil, organization: organization, created_at: Time.current.beginning_of_day) }
let(:content_block) { create :content_block, organization: organization, manifest_name: :navigation_map, scope_name: :homepage }
let(:other_content_block) { create :content_block, organization: organization, manifest_name: :navigation_map }
let!(:blueprint1) { create(:blueprint, organization: organization, created_at: Time.current.beginning_of_day + 1.minute, content_block: content_block) }
let!(:blueprint2) { create(:blueprint, image: nil, organization: organization, created_at: Time.current.beginning_of_day, content_block: content_block) }
let!(:blueprint_on_other_block) { create(:blueprint, organization: organization, content_block: other_content_block) }

describe "#valid_blueprints" do
it "returns one blueprint" do
Expand All @@ -34,8 +38,9 @@ def initialize(organization)
end

describe "#blueprints" do
it "returns two blueprint" do
it "returns two blueprints belonging to content_block" do
expect(instance.blueprints).to eq([blueprint2, blueprint1])
expect(instance.blueprints).not_to include(blueprint_on_other_block)
end
end
end
Expand Down
Loading

0 comments on commit 5199551

Please sign in to comment.