Permalink
Browse files

Merge pull request #26 from alphagov/tag_rake_task

Generalised rake task to change the parent of a tag
  • Loading branch information...
2 parents ff9956f + 30c27b8 commit 603a672c6a32315739049dad32a94bc94bbd0746 @jystewart jystewart committed Oct 12, 2012
Showing with 30 additions and 30 deletions.
  1. +30 −0 lib/tasks/change_tag_parent.rake
  2. +0 −30 lib/tasks/rename_slugs.rake
@@ -0,0 +1,30 @@
+namespace :migrate do
+ desc "Change the parent id for a given tag"
+ task :change_tag_parent, [:tag_id, :new_parent] => :environment do |t, args|
+ # justice/court-claims-debt-bankruptcy
+ puts "Migrating #{args[:tag_id]} to be a child of #{args[:new_parent]}"
+ tag = Tag.where(tag_id: "#{args[:tag_id]}").first
+ if tag.nil?
+ raise "Wa? No frakkin' tag with tag_id #{args[:tag_id]}"
+ end
+ parent = Tag.where(tag_id: "#{args[:new_parent]}").first
+ if parent.nil?
+ raise "Wa? No frakkin' parent with that tag_id #{args[:new_parent]}"
+ end
+ old_tag_id = tag.tag_id
+ tag.tag_id = "#{parent.tag_id}/#{tag.tag_id.split("/")[1]}"
+ tag.parent_id = parent.tag_id
+ tag.save!
+ new_tag_id = tag.tag_id
+
+ artefacts = Artefact.any_in(tag_ids: [old_tag_id])
+ puts "#{artefacts.count} of artefacts tagged with #{old_tag_id}"
+ artefacts.each do |a|
+ puts "Artefact: #{a.slug} with tag_ids #{a.tag_ids}"
+ index = a.tag_ids.index(old_tag_id)
+ a.tag_ids[index] = new_tag_id
+ a.save!
+ puts "new tag_ids #{a.tag_ids}"
+ end
+ end
+end
@@ -1,30 +0,0 @@
-# This is a one time run script to change the slugs of two categories
-# and update all artefacts that have been tagged with those sections
-# TODO: Once this has been run, this script can be deleted
-
-namespace :migrate do
- desc "Change slug for two categories"
- task :rename_two_category_slug => :environment do
- tag_mappings = [
- ["business/corporation-tax-capital-allowance", "business/business-tax"],
- ["driving/blue-badges-parking", "driving/blue-badge-parking"]
- ]
- tag_mappings.each do |old_tag_id, new_tag_id|
- t = Tag.where(tag_id: "#{old_tag_id}").first
- if t.nil?
- raise "Wa? No frakkin' tag with tag_id #{old_tag_id}"
- end
- t.tag_id = new_tag_id
- t.save!
- artefacts = Artefact.any_in(tag_ids: [old_tag_id])
- puts "#{artefacts.count} of artefacts tagged with #{old_tag_id}"
- artefacts.each do |a|
- puts "Artefact: #{a.slug} with tag_ids #{a.tag_ids}"
- index = a.tag_ids.index(old_tag_id)
- a.tag_ids[index] = new_tag_id
- a.save!
- puts "new tag_ids #{a.tag_ids}"
- end
- end
- end
-end

0 comments on commit 603a672

Please sign in to comment.