From 153f9481613ed765007f0bf4640316c478a8f4e6 Mon Sep 17 00:00:00 2001 From: Laura Jaime Date: Wed, 12 Jan 2022 11:03:33 +0100 Subject: [PATCH] Reorganize rake tasks --- lib/file_manager.rb | 15 +++++++ lib/tasks/civi_crm.rake | 92 ++--------------------------------------- lib/tasks/erc_auth.rake | 79 +++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 89 deletions(-) create mode 100644 lib/file_manager.rb create mode 100644 lib/tasks/erc_auth.rake diff --git a/lib/file_manager.rb b/lib/file_manager.rb new file mode 100644 index 0000000..1062c7f --- /dev/null +++ b/lib/file_manager.rb @@ -0,0 +1,15 @@ +module FileManager + def write_config_yaml!(filename, content) + File.write(filepath(filename), content.to_yaml) + puts "File generated 'config/civi_crm/#{filename}.yml'" + end + + def filepath(filename) + Dir.mkdir("config/civi_crm") unless File.directory?("config/civi_crm") + Rails.root.join("config", "civi_crm", "#{filename}.yml").to_s + end + + def load_config_yaml(filename) + YAML.load_file(filepath(filename)) + end +end diff --git a/lib/tasks/civi_crm.rake b/lib/tasks/civi_crm.rake index 8aafdcc..6e56da8 100644 --- a/lib/tasks/civi_crm.rake +++ b/lib/tasks/civi_crm.rake @@ -1,8 +1,9 @@ # frozen_string_literal: true - -require "csv" +require "file_manager" namespace :civi_crm do + include FileManager + task init: ["import:all", "generate:all", "create:scopes"] namespace :import do @@ -65,79 +66,6 @@ namespace :civi_crm do end end - namespace :import_by_csv do - task all: [:comarcals, :regionals, :locals, :local_comarcal_relationships, :local_regional_relationships] - - desc "Generates a YAML file with the CSV with 'Comarcal' tag" - task comarcals: :environment do - csv_text = File.read("tmp/scopes_codes.csv") - csv = CSV.parse(csv_text, headers: true) - - csv = csv.select { |row| row["type"] == "FC" } - csv = csv.map(&:to_h) - - result = get_code_and_display_name(csv) - write_config_yaml!("comarcals", result) - end - - desc "Generates a YAML file with the CSV with 'Regional' tag" - task regionals: :environment do - csv_text = File.read("tmp/scopes_codes.csv") - csv = CSV.parse(csv_text, headers: true) - - csv = csv.select { |row| row["type"] == "FR" } - csv = csv.map(&:to_h) - - result = get_code_and_display_name(csv) - write_config_yaml!("regionals", result) - end - - desc "Generates a YAML file with the CSV with 'Locals' tag" - task locals: :environment do - csv_text = File.read("tmp/scopes_codes.csv") - csv = CSV.parse(csv_text, headers: true) - - csv = csv.select { |row| row["type"] == "SL" } - csv = csv.map(&:to_h) - - result = get_code_and_display_name(csv) - write_config_yaml!("locals", result) - end - - desc "Generates a YAML file with the relationship between CSV 'Local' and 'Comarcal'" - task local_comarcal_relationships: :environment do - local_ids = load_config_yaml("locals").keys - comarcal_ids = load_config_yaml("comarcals").keys - result = get_code_relationships(local_ids, comarcal_ids, "comarcal") - write_config_yaml!("local_comarcal_relationships", result) - end - - desc "Generates a YAML file with the relationship between CSV 'Local' and 'Regional'" - task local_regional_relationships: :environment do - local_ids = load_config_yaml("locals").keys - regional_ids = load_config_yaml("regionals").keys - result = get_code_relationships(local_ids, regional_ids, "regional") - write_config_yaml!("local_regional_relationships", result) - end - - def get_code_and_display_name(body) - body.map! { |element| { element["code"] => element["name"] } }.reduce({}, :merge) - end - - def get_code_relationships(local_ids, _filter_ids, scope_type) - local_ids.each_with_object({}) do |element, memo| - csv_text = File.read("tmp/scopes_hierarchy.csv") - csv = CSV.parse(csv_text, headers: true) - - csv = csv.select { |row| row["SL"] == element }.first.to_h - next if csv.blank? - - memo[csv["SL"]] = csv["FC"] if scope_type == "comarcal" - memo[csv["SL"]] = csv["FR"] if scope_type == "regional" - end - end - end - namespace :generate do task all: [:comarcal_exceptions, :decidim_scopes_mapping] @@ -198,18 +126,4 @@ namespace :civi_crm do end end end - - def write_config_yaml!(filename, content) - File.write(filepath(filename), content.to_yaml) - puts "File generated 'config/civi_crm/#{filename}.yml'" - end - - def filepath(filename) - Dir.mkdir("config/civi_crm") unless File.directory?("config/civi_crm") - Rails.root.join("config", "civi_crm", "#{filename}.yml").to_s - end - - def load_config_yaml(filename) - YAML.load_file(filepath(filename)) - end end diff --git a/lib/tasks/erc_auth.rake b/lib/tasks/erc_auth.rake new file mode 100644 index 0000000..265cdd3 --- /dev/null +++ b/lib/tasks/erc_auth.rake @@ -0,0 +1,79 @@ +require "csv" +require "file_manager" + +namespace :erc_auth do + include FileManager + + namespace :csv_import do + task all: [:comarcals, :regionals, :locals, :local_comarcal_relationships, :local_regional_relationships] + + desc "Generates a YAML file with the CSV with 'Comarcal' tag" + task comarcals: :environment do + csv_text = File.read("tmp/scopes_codes.csv") + csv = CSV.parse(csv_text, headers: true) + + csv = csv.select { |row| row["type"] == "FC" } + csv = csv.map(&:to_h) + + result = get_code_and_display_name(csv) + write_config_yaml!("comarcals", result) + end + + desc "Generates a YAML file with the CSV with 'Regional' tag" + task regionals: :environment do + csv_text = File.read("tmp/scopes_codes.csv") + csv = CSV.parse(csv_text, headers: true) + + csv = csv.select { |row| row["type"] == "FR" } + csv = csv.map(&:to_h) + + result = get_code_and_display_name(csv) + write_config_yaml!("regionals", result) + end + + desc "Generates a YAML file with the CSV with 'Locals' tag" + task locals: :environment do + csv_text = File.read("tmp/scopes_codes.csv") + csv = CSV.parse(csv_text, headers: true) + + csv = csv.select { |row| row["type"] == "SL" } + csv = csv.map(&:to_h) + + result = get_code_and_display_name(csv) + write_config_yaml!("locals", result) + end + + desc "Generates a YAML file with the relationship between CSV 'Local' and 'Comarcal'" + task local_comarcal_relationships: :environment do + local_ids = load_config_yaml("locals").keys + comarcal_ids = load_config_yaml("comarcals").keys + result = get_code_relationships(local_ids, comarcal_ids, "comarcal") + write_config_yaml!("local_comarcal_relationships", result) + end + + desc "Generates a YAML file with the relationship between CSV 'Local' and 'Regional'" + task local_regional_relationships: :environment do + local_ids = load_config_yaml("locals").keys + regional_ids = load_config_yaml("regionals").keys + result = get_code_relationships(local_ids, regional_ids, "regional") + write_config_yaml!("local_regional_relationships", result) + end + + def get_code_and_display_name(body) + body.map! { |element| { element["code"] => element["name"] } }.reduce({}, :merge) + end + + def get_code_relationships(local_ids, _filter_ids, scope_type) + local_ids.each_with_object({}) do |element, memo| + csv_text = File.read("tmp/scopes_hierarchy.csv") + csv = CSV.parse(csv_text, headers: true) + + csv = csv.select { |row| row["SL"] == element }.first.to_h + next if csv.blank? + + memo[csv["SL"]] = csv["FC"] if scope_type == "comarcal" + memo[csv["SL"]] = csv["FR"] if scope_type == "regional" + end + end + end +end