module Slugtastic::ModelAdditions

Public Instance Methods

slug(name, options = {}) click to toggle source

To generate a slug from another value, call has_slug in any ActiveRecord model and pass in the name of the slug attribute. By default the slug will be generated from the title attribute, but you can specify by adding :from => {attribute}.

class Article < ActiveRecord::Base
  has_slug :slug, :from => :title
end
# File lib/slugtastic/model_additions.rb, line 12
def slug(name, options = {})
  options.reverse_merge!(from: :title)

  before_validation do
    if send(name).nil? || send(name).blank?
      send(
        "#{name}=",
        Slugtastic.generate_slug(send(options[:from]), options[:delimiter]),
      )
    end
  end
end