Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix README: add parse:true documentation #386

Merged
merged 2 commits into from
Jan 2, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 59 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,74 @@ If you want, add a `.ruby-version` file in the project root (and use rbenv or RV

## Usage

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")
@user.tag_list = "awesome, slick, hefty" # this should be familiar
@user.skill_list = "joking, clowning, boxing" # but you can do it for any context!
```

Add and remove a single tag

```ruby
@user.tag_list.add("awesomer") # add a single tag. alias for <<
@user.tag_list.remove("awesome") # remove a single tag
```

Add and remove multiple tags in an array

```ruby
@user.tag_list.add("awesomer", "slicker")
@user.tag_list.remove("awesome", "slick")
```

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.

Pay attention you need to add `parse: true` as option in this case.

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.

```ruby
@user.tag_list.add("awesomer, slicker", parse: true)
@user.tag_list.remove("awesome, slick", parse: true)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe?

alternatively, rather than passing in an array of tag arguments, you can pass in a string with a delimited list of tags to be parsed, with 'parse: true'

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe there should just be a general section on the options hash most of the tagging methods take, separate from the general examples?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice idea. I think the alternative section is cleaner, just like the line "To preserve the order in which tags are created use acts_as_ordered_taggable" and its later section. I'll do that.

```

You can also add and remove tags by direct assignment. Note this will
remove existing tags so use it with attention.

```ruby
@user.tag_list = "awesome, slick, hefty"
@user.tags
# => [<Tag name:"awesome">,<Tag name:"slick">,<Tag name:"hefty">]
```

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.tags # => [<Tag name:"awesome">,<Tag name:"slick">,<Tag name:"hefty">]
@user.skills # => [<Tag name:"joking">,<Tag name:"clowning">,<Tag name:"boxing">]
@user.skill_list # => ["joking","clowning","boxing"] as TagList
@user.skill_list.add("coding")

@user.tag_list.remove("awesome") # remove a single tag
@user.tag_list.remove("awesome, slick") # works with arrays too
@user.tag_list.add("awesomer") # add a single tag. alias for <<
@user.tag_list.add("awesomer, slicker") # also works with arrays
@user.skill_list
# => ["joking","clowning","boxing", "coding"]

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

To preserve the order in which tags are created use `acts_as_ordered_taggable`:
Expand Down