Skip to content

Instantly share code, notes, and snippets.

@Ad1n
Last active February 12, 2019 12:40
Show Gist options
  • Save Ad1n/6cb6e4896701ada160e80407118dd74d to your computer and use it in GitHub Desktop.
Save Ad1n/6cb6e4896701ada160e80407118dd74d to your computer and use it in GitHub Desktop.
Need for ref 2
require "yaml"
require "git"
class UpdateClassifier
GIT_REPO = "[email protected]:it-hq/it-classifications.git".freeze
TMP_PATH = "#{Rails.root}/tmp".freeze
LOCAL_CLASSIFIERS_PATH = "#{TMP_PATH}/categories".freeze
REG_PARAM = "#{LOCAL_CLASSIFIERS_PATH}/**/*.yml".freeze
attr_reader :classifiers_paths
def initialize
FileUtils.rm_rf(LOCAL_CLASSIFIERS_PATH)
Git.clone(GIT_REPO, "categories", path: TMP_PATH)
@classifiers_paths = Dir[REG_PARAM]
end
def update_tables
@classifiers_paths.each do |classifier_path|
add_classifier(classifier_path)
end
end
private
def add_classifier(classifier_path)
parsed_yml = YAML.load_file(classifier_path)
category_name = File.basename(File.dirname(classifier_path))
taxon_name = File.basename(classifier_path, ".yml")
category = Category.find_or_create_by!(title: category_name)
taxon = category.taxons.find_or_create_by!(name: taxon_name)
sections = parsed_yml.keys
sections.each do |s|
section = taxon.sections.find_or_create_by!(name: s)
parsed_yml.dig(s).is_a?(Hash) ? skills = parsed_yml.dig(s).keys : skills = parsed_yml.dig(s)
skills.each do |skill|
classifications_skill = Skill.find_or_create_by!(name: skill)
if parsed_yml.dig(s).is_a?(Hash)
parsed_yml.dig(s, skill)["versions"].each{ |version| classifications_skill.versions.find_or_create_by!(number: version) } unless parsed_yml.dig(s, skill).nil?
end
section.classifications.find_or_create_by!(skill: classifications_skill)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment