Skip to content

Commit

Permalink
Merge pull request codeforjapan#50 from takahashim/fix-decidim_awesom…
Browse files Browse the repository at this point in the history
…e_map_helper

fix: `MapHelper#awesome_map_for` returns valid map_center
  • Loading branch information
ayuki-joto authored Jan 16, 2025
2 parents 8c3cdde + 1303f0c commit 438873f
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 0 deletions.
63 changes: 63 additions & 0 deletions config/initializers/decidim_override.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,69 @@ def inline_comments_for(resource, options = {})
end
end

## load MapHelper in decidim_awesome
Decidim::DecidimAwesome::MapHelper # rubocop:disable Lint/Void

module DecidimAwesomeMapHelperPatch
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity:
def awesome_map_for(components, &)
return unless map_utility_dynamic

map = awesome_builder.map_element({ class: "dynamic-map", id: "awesome-map-container" }, &)
help = content_tag(:div, class: "map__skip-container") do
content_tag(:p, t("screen_reader_explanation", scope: "decidim.map.dynamic"), class: "sr-only")
end

html_options = {
class: "awesome-map",
id: "awesome-map",
data: {
"components" => components.map do |component|
{
id: component.id,
type: component.manifest.name,
name: translated_attribute(component.name),
url: Decidim::EngineRouter.main_proxy(component).root_path,
amendments: component.manifest.name == :proposals ? Decidim::Proposals::Proposal.where(component:).only_emendations.count : 0
}
end.to_json,
"hide-controls" => settings_source.try(:hide_controls),
"collapsed" => global_settings.collapse,
"truncate" => global_settings.truncate || 255,
"map-center" => global_settings.map_center.presence&.to_json || "",
"map-zoom" => global_settings.map_zoom || 8,
"menu-merge-components" => global_settings.menu_merge_components,
"menu-amendments" => global_settings.menu_amendments,
"menu-meetings" => global_settings.menu_meetings,
"menu-categories" => global_settings.menu_categories,
"menu-hashtags" => global_settings.menu_hashtags,
"show-not-answered" => step_settings&.show_not_answered,
"show-accepted" => step_settings&.show_accepted,
"show-withdrawn" => step_settings&.show_withdrawn,
"show-evaluating" => step_settings&.show_evaluating,
"show-rejected" => step_settings&.show_rejected
}
}

content_tag(:div, html_options) do
content_tag :div, class: "w-full" do
help + map
end
end
end
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/PerceivedComplexity:
end

module Decidim
module DecidimAwesome
module MapHelper
prepend DecidimAwesomeMapHelperPatch
end
end
end

# Fix I18n.transliterate()
I18n.config.backend.instance_eval do
@transliterators[:ja] = I18n::Backend::Transliterator.get(->(string) { string })
Expand Down
141 changes: 141 additions & 0 deletions spec/cells/content_blocks/map_cell_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# frozen_string_literal: true

require "rails_helper"

module Decidim::DecidimAwesome
describe ContentBlocks::MapCell, type: :cell do
subject { cell(content_block.cell, content_block).call }

let(:organization) { create(:organization) }
let(:content_block) { create(:content_block, organization:, manifest_name: :awesome_map, scope_name: :homepage, settings:) }
let(:settings) { {} }
let!(:participatory_process) { create(:participatory_process, organization:) }
let!(:category) { create(:category, participatory_space: participatory_process) }
let!(:proposal_component) { create(:proposal_component, :with_geocoding_enabled, participatory_space: participatory_process) }
let!(:meeting_component) { create(:meeting_component, participatory_space: participatory_process) }
let!(:proposal) { create(:proposal, component: proposal_component) }
let!(:meeting) { create(:meeting, component: meeting_component) }

controller Decidim::PagesController

before do
allow(controller).to receive(:current_organization).and_return(organization)
end

it "shows the map" do
expect(subject).to have_css("#awesome-map")
expect(subject).to have_content("window.AwesomeMap.categories")
end

it "do not show the title" do
expect(subject).to have_no_css("h3.section-heading")
end

it "uses default height" do
expect(subject).to have_content("height: 500px;")
end

it "uses default data-options" do
expect(subject.to_s).to include('data-truncate="255"')
expect(subject.to_s).to include('data-map-center=""')
expect(subject.to_s).to include('data-map-zoom="8"')
expect(subject.to_s).to include('data-menu-amendments="true"')
expect(subject.to_s).to include('data-menu-meetings="true"')
expect(subject.to_s).to include('data-show-not-answered="true"')
expect(subject.to_s).to include('data-show-accepted="true"')
expect(subject.to_s).to include('data-show-evaluating="true"')
expect(subject.to_s).to include('data-show-withdrawn="false"')
expect(subject.to_s).to include('data-show-rejected="false"')
end

it "uses all components" do
components = JSON.parse(subject.to_s.match(/data-components='(.*)'/)[1])

expect(components.pluck("id")).to include(meeting_component.id)
expect(components.pluck("id")).to include(proposal_component.id)
end

it "uses all categories" do
categories = JSON.parse(subject.to_s.match(/window\.AwesomeMap\.categories = (\[.*\])/)[1])

expect(categories.pluck("id")).to include(category.id)
end

context "when the content block has a title" do
let(:settings) do
{
"title" => { "en" => "Look this beautiful map!" }
}
end

it "shows the title" do
expect(subject).to have_css("h3.section-heading")
expect(subject).to have_content("Look this beautiful map!")
end
end

context "when a height is defined" do
let(:settings) do
{
map_height: 734
}
end

it "uses default height" do
expect(subject).to have_no_content("height: 500px;")
expect(subject).to have_content("height: 734px;")
end
end

context "when data-options are customized" do
let(:settings) do
{
truncate: 123,
map_center: "41.1,2.3",
map_zoom: 12,
menu_amendments: false,
menu_meetings: false,
show_not_answered: false,
show_accepted: false,
show_evaluating: false,
show_withdrawn: true,
show_rejected: true
}
end

it "uses default data-options" do
expect(subject.to_s).to include('data-truncate="123"')
expect(subject.to_s).to include('data-map-center=\'"41.1,2.3"\'')
expect(subject.to_s).to include('data-map-zoom="12"')
expect(subject.to_s).to include('data-menu-amendments="false"')
expect(subject.to_s).to include('data-menu-meetings="false"')
expect(subject.to_s).to include('data-show-not-answered="false"')
expect(subject.to_s).to include('data-show-accepted="false"')
expect(subject.to_s).to include('data-show-evaluating="false"')
expect(subject.to_s).to include('data-show-withdrawn="true"')
expect(subject.to_s).to include('data-show-rejected="true"')
end
end

context "with another organization" do
subject { cell(another_content_block.cell, another_content_block).call }

let(:another_organization) { create(:organization) }
let(:another_content_block) { create(:content_block, organization: another_organization, manifest_name: :awesome_map, scope_name: :homepage, settings:) }
let(:another_participatory_process) { create(:participatory_process, organization: another_organization) }
let!(:another_meeting_component) { create(:meeting_component, participatory_space: another_participatory_process) }

before do
allow(controller).to receive(:current_organization).and_return(another_organization)
end

it "uses its own components" do
components = JSON.parse(subject.to_s.match(/data-components='(.*)'/)[1])

expect(components.pluck("id")).not_to include(meeting_component.id)
expect(components.pluck("id")).not_to include(proposal_component.id)
expect(components.pluck("id")).to include(another_meeting_component.id)
end
end
end
end

0 comments on commit 438873f

Please sign in to comment.