Skip to content

Commit

Permalink
Merge pull request #106 from zhishi-project/reasign_tags
Browse files Browse the repository at this point in the history
add rake task for remaping tags
  • Loading branch information
Oreoluwa authored Jun 10, 2016
2 parents 4526e3f + 1fa056d commit 8114531
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Tag < ActiveRecord::Base
has_many :similar_tags, class_name: 'Tag', foreign_key: 'representative_id'
belongs_to :representative, class_name: 'Tag'

before_save :downcase!, :strip!
before_save :downcase!, :strip!, :ensure_naming_convention
after_create :push_representative_assignment_to_sidekiq
after_update :push_subscription_update_to_sidekiq, if: :representative_id_changed?

Expand Down Expand Up @@ -99,6 +99,10 @@ def representative
super || self
end

def ensure_naming_convention
self.name = self.name.split.join("-")
end

private
def search_resolution
Tag.resolution_for(name)
Expand Down
72 changes: 72 additions & 0 deletions lib/tasks/remap_tags.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
namespace :remap_tags do

desc "this task remaps and deletes tags. This is a one time task!"
task execute: :environment do
map_tags
rename_and_delete_tags
implement_new_tag_naming_convention
puts "Completed!!!"
end

def map_tags
tags = {
"food" => ["meals"],
"fellows-lagos" => ["fellows lagos", "lagos"],
"php" => ["pdo", "laravel", "dbal"],
"training" => ["cousera", "simulations"],
"sports" =>["football"],
"healthcare" => ["kbl", "bupa", "health"],
"javascript" => ["dom", "superagent"],
"react" => ["react router"],
"ruby" => ["rspec", "serializers"],
"testing" => ["faker", "mock", "stub", "unit-testing"],
"operations" => ["leave", "leave-request", "annual-leave"],
"mac" => ["osx"],
"nairobi-fellows" => ["brown-bag", "kenya"],
"databases" => ["sql", "database relationships"],
"api" => ["rest"],
"css" => ["flexbox"]
}

tags.each do |key, value|
parent_tag = Tag.where(name: key)
Tag.where(name: value).update_all(representative_id: parent_tag)
end
puts "Sucessfuly Mapped Tags!"
end

def rename_and_delete_tags
old_tags = {
"module" => "ruby",
"survey" => "fellows",
"staff" => "operations",
"queries" => "databases",
"deployment" => "laravel"
}

old_tags.each do |key, value|
new_tag = Tag.where(name: value)
ResourceTag.joins(:tag).where(tags: {name: key}).update_all(tag_id: new_tag)
end

tags_to_delete = ["computer science", "operation", "forms", "networking", "pdo-connection",
"paid-courses", "programming", "associations", "facility", "ci", "mixin",
"discussion", "promotions", "beautiful soup"] + old_tags.keys

Tag.where(name: "orders").update_all(name: "ny-office")
Tag.where(name: "history").update_all(name: "andela-history")
Tag.where(name: "databases").update_all(name: "database")

Tag.where(name: tags_to_delete).destroy_all

puts "Sucessfuly Renamed And Deleted Unwanted Tags!!"
end

def implement_new_tag_naming_convention
Tag.find_each do |tag|
name = tag.name.split(' ').join('-')
tag.update(name: name)
end
puts "Naming convention successfully implemented"
end
end

0 comments on commit 8114531

Please sign in to comment.