1
0
mirror of https://github.com/danbee/slugtastic synced 2025-03-04 08:49:05 +00:00

Add model additions for slug delimiter substitution.

This commit is contained in:
Dan Barber 2013-02-12 17:36:34 +00:00
parent 117b4e9398
commit e5ca126c8a
3 changed files with 9 additions and 3 deletions

View File

@ -11,7 +11,7 @@ module Slugtastic
# end
def has_slug name, options = { :from => :title }
before_validation do |record|
send("#{name}=", Slugtastic.generate_slug(send(options[:from]))) if send(name).nil? or send(name).blank?
send("#{name}=", Slugtastic.generate_slug(send(options[:from]), options[:delimiter])) if send(name).nil? or send(name).blank?
end
end

View File

@ -1,3 +1,3 @@
module Slugtastic
VERSION = "1.2.0-alpha"
VERSION = "1.2.0.alpha"
end

View File

@ -4,8 +4,9 @@ require 'spec_helper'
class Model < SuperModel::Base
include ActiveModel::Validations::Callbacks
extend Slugtastic::ModelAdditions
attr_accessor :slug, :title
attr_accessor :slug, :slug_2, :title
has_slug :slug, from: :title
has_slug :slug_2, from: :title, delimiter: "_"
end
describe Slugtastic::ModelAdditions do
@ -13,6 +14,10 @@ describe Slugtastic::ModelAdditions do
Model.create!(:title => "A Simple Title").slug.should eq "a-simple-title"
end
it "generates a slug from the title with delimiter substitutions" do
Model.create!(:title => "A Simple Title").slug_2.should eq "a_simple_title"
end
it "doesn't regenerate the slug if it already exists" do
model = Model.create!(:title => "A Simple Title")
model.slug.should eq "a-simple-title"
@ -20,5 +25,6 @@ describe Slugtastic::ModelAdditions do
model.title = "A new title"
model.save
model.slug.should eq "a-simple-title"
model.slug_2.should eq "a_simple_title"
end
end