Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/soulcutter/slug_fu
Yet another library for generating URL slugs for strings
https://github.com/soulcutter/slug_fu
slug
Last synced: 22 days ago
JSON representation
Yet another library for generating URL slugs for strings
- Host: GitHub
- URL: https://github.com/soulcutter/slug_fu
- Owner: soulcutter
- License: mit
- Created: 2017-03-30T13:14:58.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-06-24T00:35:46.000Z (over 4 years ago)
- Last Synced: 2024-10-06T03:17:14.684Z (about 1 month ago)
- Topics: slug
- Language: Ruby
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# SlugFu
Yet Another gem for generating slug strings suitable for URLs (or whatever your slug business may be).
**What makes this one different?** This gives you tools for ensuring the uniqueness of your slugs,
and if your slug is not unique it allows you to plug in different strategies for generating unique slugs.## Installation
Add this line to your application's Gemfile:
```ruby
gem 'slug_fu'
```And then execute:
$ bundle
Or install it yourself as:
$ gem install slug_fu
## Usage
```ruby
include SlugFuSlugFu(string) # doesn't need to check uniqueness, just make a slug string
SlugFu(string, context: %w(one two)) # generated slug will unique as far as `context.include?(slug)` is concerned
class NamingStrategy
def initialize(str)
@str = str
enddef next
@next = @next.nil? ? @str : @next + "-"
end
end
SlugFu("one", context: %w(one one-), naming_strategy: NamingStrategy) # Use a custom strategy for naming, calling #next until a unique name is found
```### Usage with Rails
SlugFu supplies `SlugFu::ModelContext` for ensuring uniqueness on Rails models.
```ruby
include SlugFuSlugFu(str, context: SlugFu::ModelContext.new(Book)) # slug will be unique for the `Book#slug` attribute
SlugFu(str, context: SlugFu::ModelContext.new(Book.where(language: "en")) # slug will be unique for the `Book#slug` attribute in the given scope
SlugFu(str, context: SlugFu::ModelContext.new(Book, :url_slug)) # slug will be unique for the `Book#url_slug` attribute
```## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/soulcutter/slug_fu
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).