Skip to content
Snippets Groups Projects
Commit c7468ae9 authored by Giovanne Marcelo's avatar Giovanne Marcelo
Browse files

Fixing bug

parent d04f50e7
No related branches found
No related tags found
No related merge requests found
class Topic < ActiveRecord::Base
has_and_belongs_to_many :learning_objects
has_many :topic_relationships, foreign_key: "parent_id", dependent: :destroy, autosave: true
has_many :subtopics, through: :topic_relationships, source: :child, autosave: true
has_many :topic_relationships, foreign_key: "parent_id", dependent: :destroy
has_many :subtopics, through: :topic_relationships, source: :child
has_many :topic_highlights, dependent: :destroy
......
......@@ -4,38 +4,63 @@ class TopicsImporter
def initialize items
@items = items
@@topics = {}
@@topics_relations = []
end
def import
topics = []
relations = []
@items.each do |learning_object|
lo_topics = parse_topics ( learning_object.get_metadata_values_of "dc.subject.category" )
lo_topics.each_with_index do |topic_name, i|
next if topic_exists? topic_name
topic = Topic.where(name: topic_name).new
topic.learning_objects << learning_object
create_relations(@@topics[lo_topics[i - 1]], topic) unless i == 0
create_relations(lo_topics[i - 1], topic_name) unless i == 0
@@topics[topic_name] = topic
end
end
separate_relations()
end
Topic.import @@topics.values, recursive: true
Topic.import @@topics.values
import_topics_relations()
end
private
def create_relations parent_topic, topic
parent_topic.subtopics << topic
def create_relations parent_name, child_name
@@topics_relations.push(parent_name)
@@topics_relations.push(child_name)
end
def separate_relations
@@topics_relations.push nil
end
def import_topics_relations
relations = []
@@topics_relations.each_with_index do |relation, i|
unless relation.nil? || @@topics_relations[i - 1].nil?
parent_id = Topic.find_by_name(@@topics_relations[i - 1]).id
child_id = Topic.find_by_name(relation).id
unless TopicRelationship.exists?(parent_id: parent_id, child_id: child_id) || i == 0
relations.push TopicRelationship.where(parent_id: parent_id, child_id: child_id ).new
end
end
end
TopicRelationship.import relations
end
def parse_topics values
......@@ -44,12 +69,11 @@ class TopicsImporter
values.each do |value|
topics = value.split('::')
end
topics
topics.map{|topic| topic.strip}
end
def topic_exists? (topic_name)
Topic.exists? name: topic_name || !@@topics[topic_name].nil?
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