|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +exec('/usr/bin/env', 'rails', 'runner', $PROGRAM_NAME, *ARGV) unless defined?(Rails) |
| 5 | + |
| 6 | +# Sets Landing Pages |
| 7 | +class SetLandingPages |
| 8 | + def initialize |
| 9 | + @organizations = Decidim::Organization.ids |
| 10 | + @participatory_process_ids = Decidim::ParticipatoryProcess.ids |
| 11 | + @assembly_ids = Decidim::Assembly.ids |
| 12 | + @resources = {} |
| 13 | + end |
| 14 | + |
| 15 | + def self.run = new.run |
| 16 | + |
| 17 | + def run |
| 18 | + handle_participatory_processes |
| 19 | + handle_assemblies |
| 20 | + write_output |
| 21 | + end |
| 22 | + |
| 23 | + def handle_participatory_processes # rubocop:disable Metrics |
| 24 | + ids = @participatory_process_ids |
| 25 | + scope = 'participatory_process_homepage' |
| 26 | + hero = {} |
| 27 | + main_data = {} |
| 28 | + disabled = [] |
| 29 | + |
| 30 | + @organizations.each do |org| |
| 31 | + ids.each do |id| |
| 32 | + hero[id] = enable(*params(org, id, scope, 'hero', 1)) |
| 33 | + main_data[id] = enable(*params(org, id, scope, 'main_data', 1)) |
| 34 | + disabled += disable(params(org, id, scope, nil, nil).first, %w[hero main_data]) |
| 35 | + end |
| 36 | + @resources[:org] ||= {} |
| 37 | + @resources[:org][org] ||= {} |
| 38 | + @resources[:org][org][:participatory_processes] = { ids:, scope:, hero:, main_data:, disabled: } |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + def handle_assemblies # rubocop:disable Metrics |
| 43 | + ids = @assembly_ids |
| 44 | + scope = 'assembly_homepage' |
| 45 | + hero = {} |
| 46 | + main_data = {} |
| 47 | + disabled = [] |
| 48 | + |
| 49 | + @organizations.each do |org| |
| 50 | + ids.each do |id| |
| 51 | + hero[id] = enable(*params(org, id, scope, 'hero', 1)) |
| 52 | + main_data[id] = enable(*params(org, id, scope, 'main_data', 2)) |
| 53 | + |
| 54 | + disabled += disable(params(org, id, scope, nil, nil).first, %w[hero main_data]) |
| 55 | + end |
| 56 | + @resources[:org] ||= {} |
| 57 | + @resources[:org][org] ||= {} |
| 58 | + @resources[:org][org][:assemblies] = { ids:, scope:, hero:, main_data:, disabled: } |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + def write_output |
| 63 | + Rails.root |
| 64 | + .join('tmp/set_landing_page.dump') |
| 65 | + .binwrite(Marshal.dump(@resources)) |
| 66 | + end |
| 67 | + |
| 68 | + private |
| 69 | + |
| 70 | + def params(decidim_organization_id, scoped_resource_id, scope_name, manifest_name, weight) |
| 71 | + find = { decidim_organization_id:, scoped_resource_id:, scope_name: } |
| 72 | + find[:manifest_name] = manifest_name if manifest_name.present? |
| 73 | + |
| 74 | + update = { published_at: DateTime.now } |
| 75 | + update[:weight] = weight if weight.present? |
| 76 | + |
| 77 | + [find, update] |
| 78 | + end |
| 79 | + |
| 80 | + def enable(find_params, update_params) |
| 81 | + Decidim::ContentBlock |
| 82 | + .find_or_create_by(find_params) |
| 83 | + .tap { _1.update(update_params) } |
| 84 | + .id |
| 85 | + end |
| 86 | + |
| 87 | + def disable(find_params, manifests) |
| 88 | + Decidim::ContentBlock |
| 89 | + .where(find_params) |
| 90 | + .where.not(manifest_name: manifests) |
| 91 | + .tap { _1.update_all(published_at: nil, weight: nil) } # rubocop:disable Rails/SkipsModelValidations |
| 92 | + .ids |
| 93 | + end |
| 94 | +end |
| 95 | + |
| 96 | +SetLandingPages.run |
0 commit comments