small refactorings

This commit is contained in:
Reinier Balt 2012-07-12 13:14:21 +02:00
parent 7d0c716366
commit 82bb67f518
6 changed files with 61 additions and 130 deletions

View file

@ -62,24 +62,26 @@ module IsTaggable
outgoing = tag_cast_to_string(outgoing)
tags.delete(*(tags.select{|tag| outgoing.include? tag.name}))
end
def get_tag_name_from_item(item)
case item
# removed next line as it prevents using numbers as tags
# when /^\d+$/, Fixnum then Tag.find(item).name # This will be slow if you use ids a lot.
when Tag
item.name
when String
item
else
raise "Invalid type"
end
end
def tag_cast_to_string obj
case obj
when Array
obj.map! do |item|
case item
# removed next line as it prevents using numbers as tags
# when /^\d+$/, Fixnum then Tag.find(item).name # This will be slow if you use ids a lot.
when Tag then item.name
when String then item
else
raise "Invalid type"
end
end
obj.map! { |item| get_tag_name_from_item(item) }
when String
obj = obj.split(Tag::DELIMITER).map do |tag_name|
tag_name.strip.squeeze(" ")
end
obj.split(Tag::DELIMITER).map { |tag_name| tag_name.strip.squeeze(" ") }
else
raise "Invalid object of class #{obj.class} as tagging method parameter"
end.flatten.compact.map(&:downcase).uniq