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:
parent
117b4e9398
commit
e5ca126c8a
@ -11,7 +11,7 @@ module Slugtastic
|
|||||||
# end
|
# end
|
||||||
def has_slug name, options = { :from => :title }
|
def has_slug name, options = { :from => :title }
|
||||||
before_validation do |record|
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -1,3 +1,3 @@
|
|||||||
module Slugtastic
|
module Slugtastic
|
||||||
VERSION = "1.2.0-alpha"
|
VERSION = "1.2.0.alpha"
|
||||||
end
|
end
|
||||||
|
|||||||
@ -4,8 +4,9 @@ require 'spec_helper'
|
|||||||
class Model < SuperModel::Base
|
class Model < SuperModel::Base
|
||||||
include ActiveModel::Validations::Callbacks
|
include ActiveModel::Validations::Callbacks
|
||||||
extend Slugtastic::ModelAdditions
|
extend Slugtastic::ModelAdditions
|
||||||
attr_accessor :slug, :title
|
attr_accessor :slug, :slug_2, :title
|
||||||
has_slug :slug, from: :title
|
has_slug :slug, from: :title
|
||||||
|
has_slug :slug_2, from: :title, delimiter: "_"
|
||||||
end
|
end
|
||||||
|
|
||||||
describe Slugtastic::ModelAdditions do
|
describe Slugtastic::ModelAdditions do
|
||||||
@ -13,6 +14,10 @@ describe Slugtastic::ModelAdditions do
|
|||||||
Model.create!(:title => "A Simple Title").slug.should eq "a-simple-title"
|
Model.create!(:title => "A Simple Title").slug.should eq "a-simple-title"
|
||||||
end
|
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
|
it "doesn't regenerate the slug if it already exists" do
|
||||||
model = Model.create!(:title => "A Simple Title")
|
model = Model.create!(:title => "A Simple Title")
|
||||||
model.slug.should eq "a-simple-title"
|
model.slug.should eq "a-simple-title"
|
||||||
@ -20,5 +25,6 @@ describe Slugtastic::ModelAdditions do
|
|||||||
model.title = "A new title"
|
model.title = "A new title"
|
||||||
model.save
|
model.save
|
||||||
model.slug.should eq "a-simple-title"
|
model.slug.should eq "a-simple-title"
|
||||||
|
model.slug_2.should eq "a_simple_title"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user