add rake tasks for cleaning up mixed-case hashtags,

fix querying tagged models, in case multiple tags are found
----
the first rake task will attach all posts tagged with mixed-
case hashtags to their lower-case variant

    $ bundle exec rake migrations:rewire_uppercase_hashtags

the other rake task will remove the - now unused - mixed-case
hashtags from the db

    $ bundle exec rake migrations:remove_uppercase_hashtags

as always, perform a backup first! ;)
This commit is contained in:
Florian Staudacher 2012-05-22 18:20:56 +02:00
parent 5c534521cc
commit 472340e540
2 changed files with 37 additions and 2 deletions

View file

@ -57,6 +57,6 @@ class Stream::Tag < Stream::Base
else else
posts = posts.all_public posts = posts.all_public
end end
posts.tagged_with(tag_name) posts.tagged_with(tag_name, :any => true)
end end
end end

View file

@ -74,4 +74,39 @@ namespace :migrations do
} }
end end
# removes hashtags with uppercase letters and re-attaches
# the posts to the lowercase version
task :rewire_uppercase_hashtags => :environment do
evil_tags = ActsAsTaggableOn::Tag.where("lower(name) != name")
puts "found #{evil_tags.count} tags to convert..."
evil_tags.each_with_index do |tag, i|
good_tag = ActsAsTaggableOn::Tag.find_or_create_by_name(tag.name.downcase)
puts "++ '#{tag.name}' has #{tag.taggings.count} records attached"
deleteme = []
tag.taggings.each do |tagging|
deleteme << tagging
end
deleteme.each do |tagging|
#tag.taggings.delete(tagging)
good_tag.taggings << tagging
end
puts "-- converted '#{tag.name}' to '#{good_tag.name}' with #{deleteme.count} records"
puts "\n## #{i} tags processed\n\n" if (i % 50 == 0)
end
end
task :remove_uppercase_hashtags => :environment do
evil_tags = ActsAsTaggableOn::Tag.where("lower(name) != name")
evil_tags.each do |tag|
next if tag.taggings.count > 0 # non-ascii tags
puts "removing '#{tag.name}'..."
tag.destroy
end
end
end end