1
0
mirror of https://github.com/danbee/slugtastic synced 2025-03-04 08:49:05 +00:00
slugtastic/lib/slugtastic/model_additions.rb
2018-04-18 15:20:18 -04:00

26 lines
766 B
Ruby

module Slugtastic
module ModelAdditions
# To generate a slug from another value, call <tt>has_slug</tt> in any
# ActiveRecord model and pass in the name of the slug attribute.
# By default the slug will be generated from the <tt>title</tt> attribute,
# but you can specify by adding <tt>:from => {attribute}</tt>.
#
# class Article < ActiveRecord::Base
# has_slug :slug, :from => :title
# end
#
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
end
end