Skip to content

Commit

Permalink
Reorganize rake tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
laurajaime committed Jan 12, 2022
1 parent 4738f92 commit dbaa3bd
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 88 deletions.
17 changes: 17 additions & 0 deletions lib/file_manager.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

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
91 changes: 3 additions & 88 deletions lib/tasks/civi_crm.rake
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# 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
Expand Down Expand Up @@ -65,79 +67,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]

Expand Down Expand Up @@ -198,18 +127,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
85 changes: 85 additions & 0 deletions lib/tasks/erc_auth.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# frozen_string_literal: true

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?

case scope_type
when "comarcal"
memo[csv["SL"]] = csv["FC"]
when "regional"
memo[csv["SL"]] = csv["FR"]
end
end
end
end
end

0 comments on commit dbaa3bd

Please sign in to comment.