Skip to content
This repository has been archived by the owner on Mar 19, 2021. It is now read-only.

Commit

Permalink
seperate usage to several blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
billychan committed Dec 31, 2013
1 parent f27f945 commit 12f9a71
Showing 1 changed file with 46 additions and 26 deletions.
72 changes: 46 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,51 +65,71 @@ If you want, add a `.ruby-version` file in the project root (and use rbenv or RV

## Usage

*Note: The following examples with delimiters inside string argument all
depend on `ActsAsTaggableOn.delimiter` being set to ',' which is the
default. See [configuration](#configuration) for more about delimiter.*
Setup

```ruby
class User < ActiveRecord::Base
# Alias for acts_as_taggable_on :tags
acts_as_taggable
acts_as_taggable # Alias for acts_as_taggable_on :tags
acts_as_taggable_on :skills, :interests
end

@user = User.new(:name => "Bobby")
```

# this should be familiar
@user.tag_list = "awesome, slick, hefty"
Add and remove a single tag

# but you can do it for any context!
@user.skill_list = "joking, clowning, boxing"
```ruby
@user.tag_list.add("awesomer") # add a single tag. alias for <<
@user.tag_list.remove("awesome") # remove a single tag
```

@user.tags
# => [<Tag name:"awesome">,<Tag name:"slick">,<Tag name:"hefty">]
Add and remove multiple tags in an array

@user.skills
# => [<Tag name:"joking">,<Tag name:"clowning">,<Tag name:"boxing">]
```ruby
@user.tag_list.add("awesomer", "slicker")
@user.tag_list.remove("awesome", "slick")
```

@user.skill_list
# => ["joking","clowning","boxing"] as TagList
You can also add and remove tags in format of String. This would
be convenient in some cases such as handling tag input param in a String.

# remove a single tag
@user.tag_list.remove("awesome")
Pay attention you need to add `parse: true` as option in this case.

# "remove" works with arrays too
@user.tag_list.remove("awesome", "slick")
You may also want to take a look at delimiter in the string. The default
is comma `,` so you don't need to do anything here. However, if you made
a change on delimiter setting, make sure the string will match. See
[configuration](#configuration) for more about delimiter.

# "remove" works with string too, needing "parse: true" option.
```ruby
@user.tag_list.add("awesomer, slicker", parse: true)
@user.tag_list.remove("awesome, slick", parse: true)
```

# add a single tag. alias for <<
@user.tag_list.add("awesomer")
You can also add and remove tags by direct assignment. Note this will
remove existing tags so use it with attention.

# "add" also works with arrays
@user.tag_list.add("awesomer", "slicker")
```ruby
@user.tag_list = "awesome, slick, hefty"
@user.tags
# => [<Tag name:"awesome">,<Tag name:"slick">,<Tag name:"hefty">]
```

# "add" works with string too, needing "parse: true" option.
@user.tag_list.add("awesomer, slicker", parse: true)
With the defined context in model, you have multiple new methods at disposal
to manage and view the tags in the context. For example, with `:skill` context
these methods are added to the model: `skill_list`(and `skill_list.add`, `skill_list.remove`
`skill_list=`), `skills`(plural), skill_counts


```ruby
@user.skill_list = "joking, clowning, boxing"

@user.skills
# => [<Tag name:"joking">,<Tag name:"clowning">,<Tag name:"boxing">]

@user.skill_list.add("coding")

@user.skill_list
# => ["joking","clowning","boxing", "coding"]

User.skill_counts
# => [<Tag name="joking" count=2>,<Tag name="clowning" count=1>...]
Expand Down

0 comments on commit 12f9a71

Please sign in to comment.