markbates / mongoid-tags-arent-hard

A tagging gem for Mongoid 3 that doesn't actually suck.
MIT License
16 stars 12 forks source link

Add #rename method #6

Open cblock opened 11 years ago

cblock commented 11 years ago

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')
cblock commented 11 years ago

...just updated the issue description: The proposed syntax now better conforms to the naming convention for different tags.

markbates commented 11 years ago

Sounds good. I'm looking forward to the pull request. :)

davesouth commented 10 years ago

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