Coder Social home page Coder Social logo

slugworth's Introduction

Slugworth

Gem Version Build Status

Simple slug functionality for your ActiveRecord objects

Installation

Add this line to your application's Gemfile:

  gem 'slugworth'

and then execute

  bundle

or

install it yourself with

  gem install slugworth

Usage

Getting started is easy! Just ensure that your model has a database column of type String called slug

To use, just add two declarations to your ActiveRecord class and away you go!

class User < ActiveRecord::Base
  include Slugworth
  slugged_with :name
end

After you include the Slugworth module, all you need to do is declare what method will be used to create the slug. The method passed to slugged_with will then be parameterized.

This provides most of the default slug functionality you would need.

  • Finders .find_by_slug & .find_by_slug! are provided. This will, of course, do what you expect and find a single record by the slug attribute provided.
  • #to_param has been defined as a parameterized version of the attribute declared to slugged_with.
  • Validations stating that slug is present and unique in the database.

Scoping uniqueness

By default the slug is unique to the entire table, but you can specify the scope of the uniqueness as the following:

class Product < ActiveRecord::Base
  include Slugworth
  belongs_to :user
  slugged_with :name, scope: :user_id
end

Updating slugs

Sometimes you want to update the slug if the attribute is changed.

class User < ActiveRecord::Base
  include Slugworth
  slugged_with :name, updatable: true
end

user = User.create(name: 'Jack')
=> User(id: 1, name: 'Jack', slug: 'jack')

user.update_attribute(:name, 'John')
=> User(id: 1, name: 'John', slug: 'john')

Incremental slugs

If another record already exists with the same slug it will generate an uniqueness validation error, but if incremental slugs is enabled, then it will append an incremented number to the slug:

class User < ActiveRecord::Base
  include Slugworth
  slugged_with :name, incremental: true
end

User.create(name: 'Jack')
=> User(id: 1, name: 'Jack', slug: 'jack')

User.create(name: 'Jack')
=> User(id: 2, name: 'Jack', slug: 'jack-1')

Test Helper

To aid in testing your models that implement the Slugworth functionality, I've added a shared example group that can be added to your test suite. Add this to your spec_helper.rb

include 'slugworth_shared_examples'

And then in your specs just add:

it_behaves_like :has_slug_functionality

This will add the same specs to your model that get run on Slugworth itself!

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Thanks

About

Hashrocket logo

Slugworth is supported by the team at Hashrocket, a multidisciplinary design and development consultancy. If you'd like to work with us or join our team, don't hesitate to get in touch.

slugworth's People

Contributors

greis avatar hashrocketeer avatar jwworth avatar mattpolito avatar rwz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

slugworth's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.