Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
diff --git vendor/plugins/acts_as_taggable_on_steroids/lib/acts_as_taggable.rb vendor/plugins/acts_as_taggable_on_steroids/lib/acts_as_taggable.rb
index 7a96ef0..adaaeba 100644
--- vendor/plugins/acts_as_taggable_on_steroids/lib/acts_as_taggable.rb
+++ vendor/plugins/acts_as_taggable_on_steroids/lib/acts_as_taggable.rb
@@ -9,7 +9,8 @@ module ActiveRecord #:nodoc:
def acts_as_taggable
has_many :taggings, :as => :taggable, :dependent => :destroy, :include => :tag
has_many :tags, :through => :taggings
-
+
+ validate :validate_tags
before_save :save_cached_tag_list
after_create :save_tags
@@ -181,6 +182,21 @@ module ActiveRecord #:nodoc:
def tag_list=(value)
@tag_list = TagList.from(value)
end
+
+ def validate_tags
+ error_messages = []
+ new_tag_names = @tag_list - tags.map(&:name)
+
+ new_tag_names.each do |new_tag_name|
+ tag = Tag.find_or_initialize_with_like_by_name(new_tag_name)
+ error_messages << tag.errors.on(:name) unless tag.valid?
+ end
+ error_messages.flatten!
+
+ error_messages.uniq.each do |msg|
+ errors.add :tag_list, msg
+ end
+ end
def save_cached_tag_list
if self.class.caching_tag_list?
diff --git vendor/plugins/acts_as_taggable_on_steroids/lib/tag.rb vendor/plugins/acts_as_taggable_on_steroids/lib/tag.rb
index 4ac6a24..115f8a3 100644
--- vendor/plugins/acts_as_taggable_on_steroids/lib/tag.rb
+++ vendor/plugins/acts_as_taggable_on_steroids/lib/tag.rb
@@ -16,6 +16,10 @@ class Tag < ActiveRecord::Base
def self.find_or_create_with_like_by_name(name)
find(:first, :conditions => ["name LIKE ?", name]) || create(:name => name)
end
+
+ def self.find_or_initialize_with_like_by_name(name)
+ find(:first, :conditions => ["name LIKE ?", name]) || new(:name => name)
+ end
def ==(object)
super || (object.is_a?(Tag) && name == object.name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.