Skip to content
Snippets Groups Projects
Commit 780738d7 authored by Mateus Rambo Strey's avatar Mateus Rambo Strey
Browse files

Merge branch 'fix-import-stages' into 'master'

Fix import stages

See merge request !326
parents 5e24eca3 6da03b76
No related branches found
No related tags found
No related merge requests found
EducationalStage.create(name: 'Educação Infatil')
EducationalStage.create(name: 'Educação Infantil')
EducationalStage.create(name: 'Ensino Fundamental')
EducationalStage.create(name: 'Ensino Médio')
EducationalStage.create(name: 'Ensino Superior')
EducationalStage.create(name: 'Outros')
require 'rainbow'
require 'logger'
require 'rainbow'
require 'logger'
namespace :import do
......@@ -7,27 +7,40 @@ namespace :import do
task :educational_stage, [:log] => [:environment] do |t, args|
logger = Logger.new(STDOUT)
educational_relations = []
educational_relations = {}
tags = []
high_stages = ['Edução Superior', 'Ensino Terciário', 'Ensino Superior']
EducationalStage.all.each do |educational|
logger.info("Creating relations of #{educational.name}")
EducationalStage.all.each do |educational|
logger.info("Creating relations of #{educational.name}")
if educational.name == 'Ensino Superior'
high_stages.select{ |stage| tag_exists?(stage) }.each do |stage|
tags << Tag.find_by_name(stage)
end
elsif educational.name == 'Outros'
tags = Tag.where.not(name: EducationalStage.all.select{|educational| educational.name != 'Outros'}.map(&:name) + high_stages)
else
if tag_exists?(educational.name)
tag = Tag.find_by_name(educational.name)
else
tag = Tag.create(name: educational.name)
tags << Tag.find_by_name(educational.name)
end
end
tags.each do |tag|
tag.taggings.each do |tagging|
educational_relation = StageRelation.new(educational_stage: educational, stageable_id: tagging.taggable_id, stageable_type: tagging.taggable_type)
educational_relations << educational_relation
next if stage_relation_exists?(educational, tagging.taggable_id, tagging.taggable_type)
educational_relation = StageRelation.new(educational_stage: educational, stageable_id: tagging.taggable_id, stageable_type: tagging.taggable_type)
educational_relations[[educational, tagging.taggable_id, tagging.taggable_type]] = educational_relation
end
end
StageRelation.import educational_relations
tags = []
end
StageRelation.import educational_relations.values
end
private
......@@ -35,4 +48,8 @@ namespace :import do
Tag.where(name: name).exists?
end
def stage_relation_exists? (educational, id, type)
StageRelation.where(educational_stage: educational, stageable_id: id, stageable_type: type).exists?
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment