Skip to content

Commit

Permalink
updated for rails 3.*, added Gemfile and gemspec, & fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tiegz committed Mar 28, 2012
1 parent edd9849 commit b3769f0
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 38 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source :rubygems

gemspec
36 changes: 36 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
PATH
remote: .
specs:
acts_as_sluggable (1.0)
activerecord (>= 3.0.0)

GEM
remote: http://rubygems.org/
specs:
activemodel (3.2.2)
activesupport (= 3.2.2)
builder (~> 3.0.0)
activerecord (3.2.2)
activemodel (= 3.2.2)
activesupport (= 3.2.2)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
activesupport (3.2.2)
i18n (~> 0.6)
multi_json (~> 1.0)
arel (3.0.2)
builder (3.0.0)
i18n (0.6.0)
multi_json (1.2.0)
rake (0.8.7)
sqlite3 (1.3.5)
tzinfo (0.3.32)

PLATFORMS
ruby

DEPENDENCIES
acts_as_sluggable!
bundler (>= 1.0.0)
rake (= 0.8.7)
sqlite3
21 changes: 21 additions & 0 deletions acts_as_sluggable.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- encoding: utf-8 -*-
Gem::Specification.new do |s|
s.name = "acts_as_sluggable"
s.version = "1.0"
s.platform = Gem::Platform::RUBY
s.authors = ["[email protected]"]
s.email = ["[email protected]"]
s.homepage = "https://github.com/kickstarter/acts_as_sluggable"
s.summary = "acts_as_sluggable, forked and updated"
s.description = "acts_as_sluggable, forked and updated"

s.add_dependency "activerecord", ">= 3.0.0"

s.add_development_dependency "bundler", ">= 1.0.0"
s.add_development_dependency "sqlite3"
s.add_development_dependency "rake", "0.8.7"

s.files = `git ls-files`.split("\n")
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
s.require_path = 'lib'
end
40 changes: 7 additions & 33 deletions lib/acts_as_slugable.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,6 @@
require 'string'
require 'active_record'

class String
def to_slug(options = {})
options[:length] ||= 50

#normalize chars to ascii
self.mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n, '').to_s.downcase.
#strip out common punctuation
gsub(/[\'\"\#\$\,\.\!\?\%\@\(\)]+/, '').
#replace ampersand chars with 'and'
gsub(/&/, 'and').
#replace non-word chars with dashes
gsub(/[\W^-_]+/, '-').
#remove double dashes
gsub(/\-{2}/, '-').
#removing leading dashes
gsub(/^-/, '').
#truncate to a a decent length
slice(0...options[:length]).
#remove trailing dashes and whitespace
gsub(/[-\s]*$/, '')
end
end

module Multiup
module Acts #:nodoc:
module Slugable #:nodoc:
Expand Down Expand Up @@ -109,7 +87,7 @@ module InstanceMethods
# d. Check if the slug is unique and, if not, append a number until it is
# e. Save the URL slug
def create_slug
return if self.errors.length > 0
return if self.errors.size > 0
return if self[source_column].blank?

if self[slug_column].to_s.empty?
Expand All @@ -120,13 +98,11 @@ def create_slug
acts_as_slugable_class.transaction do
while existing != nil
# look for records with the same url slug and increment a counter until we find a unique slug
existing = acts_as_slugable_class.find(:first, :conditions => ["#{slug_column} = ? and #{slug_scope_condition}", proposed_slug + suffix])
existing = acts_as_slugable_class.
where(slug_column => proposed_slug + suffix).
where(slug_scope_condition).first
if existing
if suffix.empty?
suffix = "-0"
else
suffix.succ!
end
suffix = suffix.empty? ? "-0" : suffix.succ
end
end
end # end of transaction
Expand All @@ -138,6 +114,4 @@ def create_slug
end
end

ActiveRecord::Base.class_eval do
include Multiup::Acts::Slugable
end
::ActiveRecord::Base.send :include, Multiup::Acts::Slugable
22 changes: 22 additions & 0 deletions lib/string.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class String
def to_slug(options = {})
options[:length] ||= 50

#normalize chars to ascii
self.mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n, '').to_s.downcase.
#strip out common punctuation
gsub(/[\'\"\#\$\,\.\!\?\%\@\(\)]+/, '').
#replace ampersand chars with 'and'
gsub(/&/, 'and').
#replace non-word chars with dashes
gsub(/[\W^-_]+/, '-').
#remove double dashes
gsub(/\-{2}/, '-').
#removing leading dashes
gsub(/^-/, '').
#truncate to a a decent length
slice(0...options[:length]).
#remove trailing dashes and whitespace
gsub(/[-\s]*$/, '')
end
end
3 changes: 3 additions & 0 deletions rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'bundler'
Bundler::GemHelper.install_tasks


desc 'Default: clear the debug log and run unit tests.'
task :default => [:clean_log, :test]
Expand Down
10 changes: 6 additions & 4 deletions test/acts_as_slugable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
class ActsAsSlugableTest < ActiveSupport::TestCase
def test_hooks_presence
# after_validation callback hooks should exist
assert Page.after_validation.include?(:create_slug)
assert Page.after_validation.include?(:create_slug)
callbacks = Page._validation_callbacks.select { |c|
c.kind == :after and c.filter == :create_slug
}
assert_equal 1, callbacks.size
end

def test_create
Expand All @@ -25,12 +27,12 @@ def test_model_still_runs_validations
# create, with nil title
pg = Page.create(:title => nil)
assert !pg.valid?
assert pg.errors.on(:title)
assert pg.errors[:title]

# create, with blank title
pg = Page.create(:title => '')
assert !pg.valid?
assert pg.errors.on(:title)
assert pg.errors[:title]
end

def test_update
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/page.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Page < ActiveRecord::Base
validates_length_of :title, :minimum => 2
validates_presence_of :title
acts_as_slugable :source_column => :title, :target_column => :url_slug, :scope => :parent, :slug_length => 50
acts_as_slugable :source_column => :title, :target_column => :url_slug,
:scope => :parent, :slug_length => 50
end

0 comments on commit b3769f0

Please sign in to comment.