Skip to content

Commit

Permalink
Complete superspace model (#8)
Browse files Browse the repository at this point in the history
* Added locales and image to superspaces admin form

* Added participatory spaces associations

* Added basic layout for show

* Changed update command

* Added styles in admin form

* Added section titles to superspace show

* Fixed rubocop issues

* Update app/commands/decidim/superspaces/admin/create_superspace.rb

* Update app/commands/decidim/superspaces/admin/update_superspace.rb

* Update app/commands/decidim/superspaces/admin/update_superspace.rb

* Update app/models/decidim/superspaces/superspace.rb

* Fixed lint issues

* Fixed command create_superspace tests

* Fixed error when creating a new superspace

* Added new params to tests

* Fixed style in locales

* Fixed rubocop offenses

* Fixed system tests

* Fixed lint issues

---------

Co-authored-by: Jose Illana Lope <[email protected]>
Co-authored-by: Fran Bolívar <[email protected]>
  • Loading branch information
3 people authored Jan 14, 2025
1 parent d54e545 commit 04c7e6e
Show file tree
Hide file tree
Showing 16 changed files with 270 additions and 11 deletions.
4 changes: 4 additions & 0 deletions app/cells/decidim/superspaces/superspace_g_cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class SuperspaceGCell < Decidim::CardGCell
def resource_path
Decidim::Superspaces::Engine.routes.url_helpers.superspace_path(model)
end

def resource_image_url
model.attached_uploader(:hero_image).url
end
end
end
end
20 changes: 19 additions & 1 deletion app/commands/decidim/superspaces/admin/create_superspace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,35 @@ def call

attr_reader :form, :current_user

def create_associations(assemblies_ids, participatory_processes_ids)
Decidim::Assembly.where(id: assemblies_ids).each do |assembly|
@superspace.superspaces_participatory_spaces.create!(
participatory_space: assembly
)
end

Decidim::ParticipatoryProcess.where(id: participatory_processes_ids).each do |process|
@superspace.superspaces_participatory_spaces.create!(
participatory_space: process
)
end
end

def create_superspace!
attributes = {
organization: form.current_organization,
title: form.title
title: form.title,
locale: form.locale,
hero_image: form.hero_image
}

@superspace = Decidim.traceability.create!(
Superspace,
current_user,
attributes
)

create_associations(form.assembly_ids, form.participatory_process_ids)
end
end
end
Expand Down
27 changes: 26 additions & 1 deletion app/commands/decidim/superspaces/admin/update_superspace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,36 @@ def call
attr_reader :form, :superspace, :current_user

def update_superspace!
assembly_ids = form.assembly_ids
participatory_process_ids = form.participatory_process_ids
Decidim.traceability.update!(
superspace,
current_user,
title: form.title
title: form.title,
hero_image: form.hero_image,
locale: form.locale
)
update_associations(assembly_ids, participatory_process_ids)
end

def update_associations(assembly_ids, process_ids)
@superspace.superspaces_participatory_spaces.destroy_all

if assembly_ids.present?
Decidim::Assembly.where(id: assembly_ids).each do |assembly|
@superspace.superspaces_participatory_spaces.create!(
participatory_space: assembly
)
end
end

return if process_ids.blank?

Decidim::ParticipatoryProcess.where(id: process_ids).each do |process|
@superspace.superspaces_participatory_spaces.create!(
participatory_space: process
)
end
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions app/forms/decidim/superspaces/admin/superspace_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ class SuperspaceForm < Decidim::Form
include TranslatableAttributes

translatable_attribute :title, String
attribute :locale
attribute :hero_image
attribute :assembly_ids
attribute :participatory_process_ids

validates :title, translatable_presence: true
validates :locale, presence: true

alias organization current_organization
end
Expand Down
24 changes: 24 additions & 0 deletions app/models/decidim/superspaces/superspace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,38 @@ class Superspace < ApplicationRecord
include Decidim::Loggable
include Decidim::Traceable
include Decidim::TranslatableResource
include Decidim::HasUploadValidations

has_one_attached :hero_image
validates_upload :hero_image, uploader: Decidim::HeroImageUploader

translatable_fields :title

has_many :superspaces_participatory_spaces, foreign_key: "decidim_superspaces_superspace_id", dependent: :destroy

belongs_to :organization,
foreign_key: "decidim_organization_id",
class_name: "Decidim::Organization"

def participatory_spaces
superspaces_participatory_spaces.map(&:participatory_space)
end

def assemblies
find_spaces_by_type("Decidim::Assembly")
end

def participatory_processes
find_spaces_by_type("Decidim::ParticipatoryProcess")
end

def self.log_presenter_class_for(_log) = Decidim::Superspaces::AdminLog::SuperspacePresenter

private

def find_spaces_by_type(type)
superspaces_participatory_spaces.where(participatory_space_type: type).map(&:participatory_space)
end
end
end
end
43 changes: 43 additions & 0 deletions app/packs/stylesheets/decidim/superspaces/superspaces.scss
Original file line number Diff line number Diff line change
@@ -1 +1,44 @@
/* css for decidim_superspaces */

.participatory-spaces-group {
border: 1px solid #e8e8e8;
padding: 1rem;
margin-bottom: 2rem;
border-radius: 4px;

legend {
font-weight: 600;
margin-bottom: 1rem;
padding: 0 0.5rem;
}

.check-boxes {
max-height: 400px;
overflow-y: auto;
padding: 0.5rem;
}

.check-box {
margin-bottom: 0.8rem;

label {
display: flex;
align-items: center;
cursor: pointer;

input[type="checkbox"] {
margin: 0 0.5rem 0 0;
}

.participatory-space-title {
line-height: 1.2;
}
}

&:hover {
.participatory-space-title {
color: var(--primary);
}
}
}
}
54 changes: 54 additions & 0 deletions app/views/decidim/superspaces/admin/superspaces/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,59 @@
<div class="row column">
<%= form.translated :text_field, :title, autofocus: true %>
</div>
<div class="row column">
<%= form.collection_select :locale, localized_locales(Decidim::available_locales), :id, :name %>
</div>
<div class="row column">
<%= form.upload :hero_image, button_class: "button button__sm button__transparent-secondary" %>
</div>
<div class="row column">
<div class="row">
<div class="columns medium-6">
<fieldset class="participatory-spaces-group">
<legend><%= t("assemblies", scope: "decidim.superspaces.models.superspace.fields") %></legend>

<div class="check-boxes">
<% Decidim::Assembly.where(organization: current_organization).each do |assembly| %>
<div class="check-box">
<label>
<%= form.check_box :assembly_ids,
{ multiple: true, label: false, checked: @superspace&.participatory_spaces&.include?(assembly) || false},
assembly.id,
nil
%>
<span class="participatory-space-title">
<%= translated_attribute(assembly.title) %>
</span>
</label>
</div>
<% end %>
</div>
</fieldset>
</div>

<div class="columns medium-6">
<fieldset class="participatory-spaces-group">
<legend><%= t("participatory_processes", scope: "decidim.superspaces.models.superspace.fields") %></legend>

<div class="check-boxes">
<% Decidim::ParticipatoryProcess.where(organization: current_organization).each do |process| %>
<div class="check-box">
<label>
<%= form.check_box :participatory_process_ids,
{ multiple: true, label: false, checked:@superspace&.participatory_spaces&.include?(process) || false },
process.id
%>
<span class="participatory-space-title">
<%= translated_attribute(process.title) %>
</span>
</label>
</div>
<% end %>
</div>
</fieldset>
</div>
</div>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<thead>
<tr>
<th><%= t("models.superspace.fields.title", scope: "decidim.superspaces") %></th>
<th><%= t("models.superspace.fields.locale", scope: "decidim.superspaces") %></th>
<th><%= t("models.superspace.fields.created_at", scope: "decidim.superspaces") %></th>
<th class="actions"><%= t("actions.title", scope: "decidim.superspaces.admin.superspaces") %></th>
</tr>
Expand All @@ -25,6 +26,9 @@
<td>
<%= translated_attribute(superspace.title) %><br>
</td>
<td>
<%= superspace.locale %><br>
</td>
<td>
<%= l superspace.created_at, format: "%d/%m/%Y - %H:%M" %>
</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</div>
<div class="item__edit item__edit-1col">
<div class="item__edit-form">
<%= decidim_form_for(@form, html: { class: "form form-defaults new_superspace" }) do |f| %>
<%= decidim_form_for(@form, html: { class: "form form-defaults new_superspace", multipart: true }) do |f| %>
<div class="card">
<div class="card-section">
<%= render partial: "form", object: f %>
Expand Down
20 changes: 18 additions & 2 deletions app/views/decidim/superspaces/superspaces/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,21 @@
<% end %>

<%= render layout: "layouts/decidim/shared/layout_two_col" do %>
TODO: Add content here
<% end %>
<% if superspace.assemblies.any? %>
<section id="assemblies-grid" class="layout-main__section layout-main__heading">
<h2 class="title-decorator"><%= t("assemblies", scope: "decidim.superspaces.superspaces.show") %></h2>
<% superspace.assemblies.each do |assembly| %>
<%= card_for assembly, highlight: true, size: :g %>
<% end %>
</section>
<% end %>

<% if superspace.participatory_processes.any? %>
<section id="processes-grid" class="layout-main__section layout-main__heading">
<h2 class="title-decorator"><%= t("participatory_processes", scope: "decidim.superspaces.superspaces.show") %></h2>
<% superspace.participatory_processes.each do |process| %>
<%= card_for process, highlight: true, size: :g %>
<% end %>
</section>
<% end %>
<% end %>
6 changes: 6 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,14 @@ en:
models:
superspace:
fields:
assemblies: Assemblies
created_at: Created at
locale: Language
participatory_processes: Participatory Processes
title: Title
superspaces:
index:
title: Superspaces
show:
assemblies: Assemblies
participatory_processes: Participatory Processes
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@ module Admin
let(:organization) { create(:organization) }
let(:current_user) { create(:user, organization:) }
let(:title) { "Superspace title" }
let(:locale) { "en" }
let(:hero_image) { nil }
let(:assembly_ids) { nil }
let(:participatory_process_ids) { nil }

let(:invalid) { false }
let(:form) do
double(
invalid?: invalid,
title: { en: title },
current_organization: organization
current_organization: organization,
hero_image:,
locale:,
assembly_ids:,
participatory_process_ids:
)
end

Expand Down
18 changes: 16 additions & 2 deletions spec/commands/decidim/superspaces/admin/update_superspace_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@ module Admin
let(:title) { "Superspace title" }
let(:superspace) { create(:superspace, organization:) }
let(:invalid) { false }
let(:locale) { "en" }
let(:hero_image) { nil }
let(:assembly_ids) { nil }
let(:participatory_process_ids) { nil }
let(:form) do
double(
invalid?: invalid,
title: { en: title },
current_organization: organization
current_organization: organization,
hero_image:,
locale:,
assembly_ids:,
participatory_process_ids:
)
end

Expand All @@ -36,20 +44,26 @@ module Admin

context "when everything is ok" do
let(:title) { "Superspace title updated" }
let(:locale) { "fr" }

it "updates the title" do
subject.call
expect(translated(superspace.title)).to eq title
end

it "updates the locale" do
subject.call
expect(superspace.locale).to eq locale
end

it "broadcasts ok" do
expect { subject.call }.to broadcast(:ok)
end

it "traces the action", :versioning do
expect(Decidim.traceability)
.to receive(:update!)
.with(superspace, current_user, { title: { en: title } })
.with(superspace, current_user, { title: { en: title }, locale:, hero_image: })
.and_call_original

expect { subject.call }.to change(Decidim::ActionLog, :count)
Expand Down
Loading

0 comments on commit 04c7e6e

Please sign in to comment.