Created
April 20, 2011 15:49
-
-
Save danchoi/931727 to your computer and use it in GitHub Desktop.
Monkeypatch to preserve tagging order of acts-as-taggable-on
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ActsAsTaggableOn::Taggable | |
module Ownership | |
module InstanceMethods | |
def save_owned_tags | |
tagging_contexts.each do |context| | |
cached_owned_tag_list_on(context).each do |owner, tag_list| | |
orig_tag_list = tag_list | |
tag_list = ActsAsTaggableOn::Tag.find_or_create_all_with_like_by_name(tag_list.uniq) | |
owned_tags = owner_tags_on(owner, context) | |
# Find all taggings that belong to the taggable (self), are owned by the owner, | |
# have the correct context, and are removed from the list. | |
old_taggings = ActsAsTaggableOn::Tagging.where(:taggable_id => id, :taggable_type => self.class.base_class.to_s, | |
:tagger_type => owner.class.to_s, :tagger_id => owner.id, | |
:tag_id => owned_tags, :context => context).all | |
if old_taggings.present? | |
# Destroy old taggings: | |
ActsAsTaggableOn::Tagging.destroy_all(:id => old_taggings.map(&:id)) | |
end | |
orig_tag_list.each do |tag_name| | |
tag = tag_list.detect {|t| t.name == tag_name} | |
taggings.create!(:tag_id => tag.id, :context => context.to_s, :tagger => owner, :taggable => self) | |
end | |
end | |
end | |
true | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment