Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/oleander/to_param-rb

A one-liner solution for ActiveRecord::Base#to_param
https://github.com/oleander/to_param-rb

Last synced: about 2 months ago
JSON representation

A one-liner solution for ActiveRecord::Base#to_param

Awesome Lists containing this project

README

        

# to_param

A one-liner solution for `ActiveRecord::Base#to_param`.

## Usage

### Default format

``` ruby
class User < ActiveRecord::Base
# ...
to_param :name
end

User.create(name: "Linus").to_param # => "1-linus"
```

### Custom format

``` ruby
class User < ActiveRecord::Base
# ...
to_param "Feel-lucky-:name"
end

User.create(name: "punk").to_param # => "feel-lucky-punk"
```

### Static value

``` ruby
class User < ActiveRecord::Base
# ...
to_param "static"
end

User.create(name: "Linus").to_param # => "static"
```

## Note

`ActiveRecord::Base.to_param` uses [#parameterize](http://apidock.com/rails/ActiveSupport/Inflector/parameterize) under the hood, so you don't have to worry about escaping.

## How do install

[sudo] gem install to_param

## Requirements

*to_param* is tested in *OS X 10.8.2* using Ruby *1.9.2* and Rails *3.2*.

## License

*to_param* is released under the *MIT license*.