Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the default delimiter with acts-as-taggable-on

The default delimiter in the acts-as-taggable-on gem is a comma. I'd like to change this to a space throughout my Rails 3 application. For example, tag_list should be assigned like this:

object.tag_list = "tagone tagtwo tagthree"

rather than like this:

object.tag_list = "tagone, tagtwo, tagthree"

What is the best way to go about changing the delimiter?

like image 758
Chris Alley Avatar asked Jan 04 '11 08:01

Chris Alley


2 Answers

You need define the delimiter class variable in ActsAsTaggableOn::TagList class

In an initializer add that :

ActsAsTaggableOn::TagList.delimiter = ' '
like image 80
shingara Avatar answered Oct 30 '22 07:10

shingara


I wouldn't go hacking around inside acts-as-taggable-on, just create another method on the class that implements it:

class MyClass < ActiveRecord::Base
  acts_as_taggable

  def human_tag_list
    self.tag_list.gsub(', ', ' ')
  end

  def human_tag_list= list_of_tags
    self.tag_list = list_of_tags.gsub(' ', ',')
  end
end

MyClass.get(1).tag_list # => "tagone, tagtwo, tagthree"
MyClass.get(1).human_tag_list # => "tagone and tagtwo and tagthree"
MyClass.get(1).human_tag_list = "tagone tagtwo tagthree"
like image 23
stef Avatar answered Oct 30 '22 06:10

stef