Skip to content

Instantly share code, notes, and snippets.

@ledermann
Created August 5, 2010 16:10
Show Gist options
  • Save ledermann/509954 to your computer and use it in GitHub Desktop.
Save ledermann/509954 to your computer and use it in GitHub Desktop.
Monkey patch for the Rails plugin "acts_as_taggable_on" to handle dirty tracking
# Monkey patch for the Rails plugin "acts_as_taggable_on" to handle dirty tracking
# http://github.com/mbleigh/acts-as-taggable-on/issues#issue/1
module ActsAsTaggableOn::Taggable
module Core
module InstanceMethods
def set_tag_list_on_with_dirty_tracking(context,new_list)
value = new_list.to_s
attr = "#{context.to_s.singularize}_list"
if changed_attributes.include?(attr)
# The attribute already has an unsaved change.
old = changed_attributes[attr]
changed_attributes.delete(attr) if (old == value)
else
old = tag_list_on(context).to_s
changed_attributes[attr] = old if (old != value)
end
set_tag_list_on_without_dirty_tracking(context,new_list)
end
alias_method_chain :set_tag_list_on, :dirty_tracking
end
end
end
@dsmalko
Copy link

dsmalko commented Apr 14, 2011

There is a typo:

value = new_list.is_a?(Array) ? new_list.join(', ') : value

But correct is:

value = new_list.is_a?(Array) ? new_list.join(', ') : new_list

Thanks for sharing it!

@ledermann
Copy link
Author

Thanks @dsmalko, I have updated the gist and made it a little bit more simple

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment