mongoid-tags-arent-hard
mongoid-tags-arent-hard copied to clipboard
Add #rename method
This method should simply rename a tag:
Foo.rename_tags('old tag name', 'new tag name')
And it should work with scoped queries too:
Foo.where(name: 'test').rename_tags('old tag name', 'new tag name')
Foo.where(name: 'test').rename_colors('old tag name', 'new tag name')
...just updated the issue description: The proposed syntax now better conforms to the naming convention for different tags.
Sounds good. I'm looking forward to the pull request. :)
The workaround is to find all records in a collection with the tag to be modified. Add the new tag name. Reject the old tag name. This workaround helps in a variety of situations including renaming, deleting, merging, and splitting tags.
Set these variables old_tag is the one to be modified new_tag is the modified version
Examples Rename: tagg => tag Split: twotags => two tags Merge: badtag => existingtag Delete: tag => ""
Collection.where(tags: old_tag).batch_size(100).each do |doc|
# Add the new tag to the tags array
doc.tags << params[:new_tag]
# Reject the old tag from the tags array
doc.tags.reject! { |tag| tag == old_tag }
# Save the change
doc.save!
end