Skip to content

Commit

Permalink
Move remaining categories from some resources to taxonomies (decidim#…
Browse files Browse the repository at this point in the history
…13838)

Co-authored-by: Ivan Vergés <Ivan Vergés>
  • Loading branch information
microstudi authored Jan 23, 2025
1 parent b168209 commit 451c482
Show file tree
Hide file tree
Showing 73 changed files with 204 additions and 902 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ def save
next if cumulative_value.zero?

quantity_value = quantity[key] || 0
category_id, space_type, space_id, related_object_id = key
taxonomy_id, space_type, space_id, related_object_id = key
record = Decidim::Metric.find_or_initialize_by(day: @day.to_s,
metric_type: @metric_name,
organization: @organization,
decidim_category_id: category_id,
decidim_taxonomy_id: taxonomy_id,
participatory_space_type: space_type,
participatory_space_id: space_id,
related_object_type: "Decidim::Component",
Expand All @@ -38,9 +38,9 @@ def query
@query = Decidim::Accountability::Result.select(:decidim_component_id)
.where(component: visible_components_from_spaces(spaces))
.joins(:component)
.left_outer_joins(:category)
.left_outer_joins(:taxonomizations)
@query = @query.where(decidim_accountability_results: { created_at: ..end_time })
@query = @query.group("decidim_categorizations.decidim_category_id",
@query = @query.group("decidim_taxonomizations.taxonomy_id",
:participatory_space_type,
:participatory_space_id,
:decidim_component_id)
Expand Down
45 changes: 25 additions & 20 deletions decidim-accountability/lib/decidim/accountability/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ def call
create_statuses!(component:)

3.times do
categories = create_categories!
taxonomies = create_taxonomies!

categories.each do |category|
create_result!(component:, category:)
taxonomies.each do |taxonomy|
create_result!(component:, taxonomy:)
end
end
end
Expand All @@ -33,8 +33,8 @@ def create_component!
participatory_space:,
settings: {
intro: Decidim::Faker::Localized.wrapped("<p>", "</p>") { Decidim::Faker::Localized.sentence(word_count: 4) },
scopes_enabled: true,
scope_id: participatory_space.scope&.id
scopes_enabled: false,
taxonomy_filters: create_filters!.pluck(:id)
}
}

Expand All @@ -53,32 +53,37 @@ def create_statuses!(component:)
end
end

def create_categories!
parent_category = participatory_space.categories.sample
categories = [parent_category]
def root_taxonomy
@root_taxonomy ||= participatory_space.organization.taxonomies.roots.find_by("name->>'#{I18n.locale}'= ?",
"Categories") || participatory_space.organization.taxonomies.roots.sample
end

def create_taxonomies!
parent_taxonomy = root_taxonomy.children.sample || root_taxonomy.children.create!(name: Decidim::Faker::Localized.sentence(word_count: 5))
taxonomies = [parent_taxonomy]

2.times do
categories << Decidim::Category.create!(
name: Decidim::Faker::Localized.sentence(word_count: 5),
description: Decidim::Faker::Localized.wrapped("<p>", "</p>") do
Decidim::Faker::Localized.paragraph(sentence_count: 3)
end,
parent: parent_category,
participatory_space:
)
taxonomies << if parent_taxonomy.children.count > 1
parent_taxonomy.children.sample
else
parent_taxonomy.children.create!(name: Decidim::Faker::Localized.sentence(word_count: 5))
end
end

categories
taxonomies
end

def create_filters!
root_taxonomy.taxonomy_filters || [create_taxonomy_filter!(root_taxonomy:, taxonomies: root_taxonomy.all_children)]
end

def create_result!(component:, category:)
def create_result!(component:, taxonomy:)
result = Decidim.traceability.create!(
Decidim::Accountability::Result,
admin_user,
{
component:,
scope: participatory_space.organization.scopes.sample,
category:,
taxonomies: [taxonomy],
title: Decidim::Faker::Localized.sentence(word_count: 2),
description: Decidim::Faker::Localized.wrapped("<p>", "</p>") do
Decidim::Faker::Localized.paragraph(sentence_count: 3)
Expand Down
1 change: 0 additions & 1 deletion decidim-accountability/spec/models/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ module Accountability

include_examples "has component"
include_examples "has scope"
include_examples "has category"
include_examples "has taxonomies"
include_examples "has reference"
include_examples "resourceable"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
let(:organization) { create(:organization) }
let(:participatory_space) { create(:participatory_process, organization:) }
let(:component) { create(:accountability_component, :published, participatory_space:) }
let(:taxonomies) { create_list(:taxonomy, 2, :with_parent, organization:) }
let(:day) { Time.zone.yesterday }
let!(:results) { create_list(:result, 5, created_at: day, component:) }
let!(:results) { create_list(:result, 5, created_at: day, taxonomies:, component:) }

include_context "when managing metrics"

context "when executing" do
it "creates new metric records" do
registry = generate_metric_registry

expect(registry.collect(&:day)).to eq([day])
expect(registry.collect(&:cumulative)).to eq([5])
expect(registry.collect(&:quantity)).to eq([5])
expect(registry.collect(&:day)).to eq([day, day])
expect(registry.collect(&:cumulative)).to eq([5, 5])
expect(registry.collect(&:quantity)).to eq([5, 5])
end

it "does not create any record if there is no data" do
Expand All @@ -28,12 +29,12 @@
end

it "updates metric records" do
create(:metric, metric_type: "results", day:, cumulative: 1, quantity: 1, organization:, category: nil, participatory_space:, related_object_type: component.class.name, related_object_id: component.id)
create(:metric, metric_type: "results", day:, cumulative: 1, quantity: 1, organization:, taxonomy: taxonomies.first, participatory_space:, related_object_type: component.class.name, related_object_id: component.id)
registry = generate_metric_registry

expect(Decidim::Metric.count).to eq(1)
expect(registry.collect(&:cumulative)).to eq([5])
expect(registry.collect(&:quantity)).to eq([5])
expect(Decidim::Metric.count).to eq(2)
expect(registry.collect(&:cumulative)).to eq([5, 5])
expect(registry.collect(&:quantity)).to eq([5, 5])
end
end
end
2 changes: 1 addition & 1 deletion decidim-accountability/spec/requests/result_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
describe "home" do
subject { response.body }

it "displays all categories that have top-level results" do
it "displays all taxonomies that have top-level results" do
expect(subject).to include(decidim_escape_translated(taxonomy1.name))
expect(subject).to include(decidim_escape_translated(taxonomy2.name))
expect(subject).not_to include(decidim_escape_translated(child_taxonomy1.name))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ module Filterable
:search_field_predicate,
:adjacent_items

delegate :categories, to: :current_component
delegate :scopes, to: :current_organization
delegate :taxonomies, to: :current_organization
delegate :available_root_taxonomies, to: :current_component
Expand Down Expand Up @@ -195,13 +194,6 @@ def taxonomy_order_or_search?
ransack_params[:taxonomies_part_of_contains].present? || ransack_params[:s]&.include?("taxonomies_name")
end

# A tree of Category IDs. Leaves are `nil`.
def category_ids_hash(categories)
categories.each_with_object({}) do |category, hash|
hash[category.id] = category.subcategories.any? ? category_ids_hash(category.subcategories) : nil
end
end

# A tree of Scope IDs. Leaves are `nil`.
def scope_ids_hash(scopes)
scopes.each_with_object({}) do |scope, hash|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def call
Assembly.transaction do
copy_assembly
copy_assembly_attachments
copy_assembly_categories if @form.copy_categories?
copy_assembly_components if @form.copy_components?
end
end
Expand Down Expand Up @@ -60,7 +59,8 @@ def copy_assembly
participatory_scope: @assembly.participatory_scope,
participatory_structure: @assembly.participatory_structure,
meta_scope: @assembly.meta_scope,
announcement: @assembly.announcement
announcement: @assembly.announcement,
taxonomies: @assembly.taxonomies
)
end

Expand All @@ -72,17 +72,6 @@ def copy_assembly_attachments
end
end

def copy_assembly_categories
@assembly.categories.each do |category|
Category.create!(
name: category.name,
description: category.description,
parent_id: category.parent_id,
participatory_space: @copied_assembly
)
end
end

def copy_assembly_components
@assembly.components.each do |component|
new_component = Component.create!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def import_assembly
assemblies.each do |original_assembly|
Decidim.traceability.perform_action!("import", Assembly, @user) do
@imported_assembly = importer.import(original_assembly, form.current_user, title: form.title, slug: form.slug)
importer.import_categories(original_assembly["assembly_categories"]) if form.import_categories?
importer.import_folders_and_attachments(original_assembly["attachments"]) if form.import_attachments?
importer.import_components(original_assembly["components"]) if form.import_components?
@imported_assembly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class AssemblyCopyForm < Form
mimic :assembly

attribute :slug, String
attribute :copy_categories, Boolean
attribute :copy_components, Boolean

validates :slug, presence: true, format: { with: Decidim::Assembly.slug_format }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class AssemblyImportForm < Form

attribute :slug, String
attribute :import_steps, Boolean, default: false
attribute :import_categories, Boolean, default: true
attribute :import_attachments, Boolean, default: true
attribute :import_components, Boolean, default: true
attribute :document, Decidim::Attributes::Blob
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,33 +68,6 @@ def import(attributes, _user, opts)
end
end

def import_categories(categories)
return if categories.nil?

categories.map do |category_attributes|
category = Decidim.traceability.create!(
Category,
@user,
name: category_attributes["name"],
description: category_attributes["description"],
parent_id: category_attributes["parent_id"],
participatory_space: @imported_assembly
)
next if category_attributes["subcategories"].nil?

category_attributes["subcategories"].map do |subcategory_attributes|
Decidim.traceability.create!(
Category,
@user,
name: subcategory_attributes["name"],
description: subcategory_attributes["description"],
parent_id: category.id,
participatory_space: @imported_assembly
)
end
end
end

def import_folders_and_attachments(attachments)
return if attachments["files"].nil?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
</div>
<div class="card-section">
<div class="row">
<div class="columns">
<%= form.check_box :copy_categories %>
</div>

<div class="columns">
<%= form.check_box :copy_components %>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
</div>
<div class="card-section">
<div class="row">
<div class="columns">
<%= form.check_box :import_categories %>
</div>
<div class="columns">
<%= form.check_box :import_attachments %>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
export.collection do
Decidim::Assembly
.public_spaces
.includes(:area, :scope, :attachment_collections, :categories)
.includes(:area, :scope, :attachment_collections)
end

export.include_in_open_data = true
Expand Down
8 changes: 4 additions & 4 deletions decidim-assemblies/lib/decidim/assemblies/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def call
2.times do
create_taxonomy!(name: ::Faker::Lorem.word, parent: taxonomy)
end
# filters for assemblies only
create_taxonomy_filter!(root_taxonomy: taxonomy,
taxonomies: taxonomy.all_children,
participatory_space_manifests: [:assemblies])

2.times do |_n|
assembly = create_assembly!
Expand All @@ -25,10 +29,6 @@ def call

create_attachments!(attached_to: current_assembly)

2.times do
create_category!(participatory_space: current_assembly)
end

seed_components_manifests!(participatory_space: current_assembly)

Decidim::ContentBlocksCreator.new(current_assembly).create_default!
Expand Down
28 changes: 3 additions & 25 deletions decidim-assemblies/spec/commands/copy_assembly_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,20 @@ module Decidim::Assemblies
let(:user) { create(:user, organization:) }
let(:scope) { create(:scope, organization:) }
let(:errors) { double.as_null_object }
let!(:assembly) { create(:assembly) }
let!(:assembly) { create(:assembly, organization:, taxonomies: [taxonomy]) }
let(:taxonomy) { create(:taxonomy, :with_parent, organization:) }
let!(:component) { create(:component, manifest_name: :dummy, participatory_space: assembly) }
let(:form) do
instance_double(
Admin::AssemblyCopyForm,
invalid?: invalid,
title: { en: "title" },
slug: "copied-slug",
copy_categories?: copy_categories,
copy_components?: copy_components
)
end
let!(:category) do
create(
:category,
participatory_space: assembly
)
end

let(:invalid) { false }
let(:copy_categories) { false }
let(:copy_components) { false }

context "when the form is not valid" do
Expand Down Expand Up @@ -63,6 +56,7 @@ module Decidim::Assemblies
expect(new_assembly.participatory_scope).to eq(old_assembly.participatory_scope)
expect(new_assembly.meta_scope).to eq(old_assembly.meta_scope)
expect(new_assembly.announcement).to eq(old_assembly.announcement)
expect(new_assembly.taxonomies).to eq(old_assembly.taxonomies)
end

it "broadcasts ok" do
Expand All @@ -82,22 +76,6 @@ module Decidim::Assemblies
end
end

context "when copy_categories exists" do
let(:copy_categories) { true }

it "duplicates an assembly and the categories" do
expect { subject.call }.to change(Decidim::Category, :count).by(1)
expect(Decidim::Category.unscoped.distinct.pluck(:decidim_participatory_space_id).count).to eq 2

old_assembly_category = Decidim::Category.unscoped.first
new_assembly_category = Decidim::Category.unscoped.last

expect(new_assembly_category.name).to eq(old_assembly_category.name)
expect(new_assembly_category.description).to eq(old_assembly_category.description)
expect(new_assembly_category.parent).to eq(old_assembly_category.parent)
end
end

context "when copy_components exists" do
let(:copy_components) { true }

Expand Down
Loading

0 comments on commit 451c482

Please sign in to comment.