sluggerize 0.0.3 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gemtest +0 -0
- data/Gemfile +6 -1
- data/Rakefile +13 -0
- data/lib/sluggerize.rb +5 -47
- data/lib/sluggerize/class_methods.rb +38 -0
- data/lib/sluggerize/errors.rb +12 -0
- data/lib/sluggerize/instance_methods.rb +18 -0
- data/lib/sluggerize/version.rb +1 -1
- data/sluggerize.gemspec +1 -0
- data/test/sluggerize_test.rb +88 -0
- data/test/test_helper.rb +40 -0
- metadata +29 -8
data/.gemtest
ADDED
File without changes
|
data/Gemfile
CHANGED
data/Rakefile
CHANGED
@@ -1,2 +1,15 @@
|
|
1
1
|
require 'bundler'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the restful_authentication plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
2
15
|
Bundler::GemHelper.install_tasks
|
data/lib/sluggerize.rb
CHANGED
@@ -1,54 +1,12 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'sluggerize/errors'
|
3
|
+
require 'sluggerize/class_methods'
|
4
|
+
require 'sluggerize/instance_methods'
|
5
|
+
|
1
6
|
module Sluggerize
|
2
7
|
def self.included(base)
|
3
8
|
base.extend ClassMethods
|
4
9
|
end
|
5
|
-
module ClassMethods
|
6
|
-
def sluggerize(attr=nil,options={})
|
7
|
-
class_inheritable_accessor :options
|
8
|
-
attr ||= :title
|
9
|
-
options[:as_param] ||= true
|
10
|
-
options[:substitution_char] ||= '-'
|
11
|
-
self.options = options
|
12
|
-
|
13
|
-
raise ArgumentError, "#{self.name} is missing source column" if columns_hash[attr.to_s].nil?
|
14
|
-
raise ArgumentError, "#{self.name} is missing required slug column" if columns_hash['slug'].nil?
|
15
|
-
|
16
|
-
before_validation :create_slug, :on => :create
|
17
|
-
|
18
|
-
validates_presence_of :slug
|
19
|
-
validates_uniqueness_of :slug, :allow_nil => (options[:as_param] ? true : false)
|
20
|
-
|
21
|
-
send :define_method, :column_to_slug, lambda { self.send(attr) }
|
22
|
-
|
23
|
-
class << self
|
24
|
-
def find(*args)
|
25
|
-
if self.options[:as_param] && args.first.is_a?(String)
|
26
|
-
find_by_slug(args)
|
27
|
-
else
|
28
|
-
super(*args)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
include InstanceMethods
|
34
|
-
end
|
35
|
-
end
|
36
|
-
module InstanceMethods
|
37
|
-
|
38
|
-
def to_param
|
39
|
-
options[:as_param] ? self.slug : self.id
|
40
|
-
end
|
41
|
-
|
42
|
-
protected
|
43
|
-
|
44
|
-
def create_slug
|
45
|
-
self.slug ||= clean("#{column_to_slug}")
|
46
|
-
end
|
47
|
-
|
48
|
-
def clean(string)
|
49
|
-
string.downcase.gsub(/[^\w\s\d\_\-]/,'').gsub(/\s\s+/,' ').gsub(/[^\w\d]/, options[:substitution_char])
|
50
|
-
end
|
51
|
-
end
|
52
10
|
end
|
53
11
|
|
54
12
|
ActiveRecord::Base.send(:include, Sluggerize)
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Sluggerize
|
2
|
+
module ClassMethods
|
3
|
+
def sluggerize(attr=nil, options={})
|
4
|
+
class_attribute :options
|
5
|
+
|
6
|
+
attr ||= :title
|
7
|
+
options[:as_param] = true unless options[:as_param] == false
|
8
|
+
options[:substitution_char] ||= '-'
|
9
|
+
self.options = options
|
10
|
+
|
11
|
+
raise ArgumentError unless options[:substitution_char].is_a?(String)
|
12
|
+
|
13
|
+
if ActiveRecord::Base.connection.table_exists?(self.table_name)
|
14
|
+
raise NoSourceColumnError, "#{self.name} is missing source column" if columns_hash[attr.to_s].nil?
|
15
|
+
raise NoSlugColumnError, "#{self.name} is missing required slug column" if columns_hash['slug'].nil?
|
16
|
+
end
|
17
|
+
|
18
|
+
before_validation :create_slug, :on => :create
|
19
|
+
|
20
|
+
validates_presence_of :slug, :allow_nil => (options[:as_param] ? true : false)
|
21
|
+
validates_uniqueness_of :slug, :allow_nil => (options[:as_param] ? true : false)
|
22
|
+
|
23
|
+
send :define_method, :column_to_slug, lambda { self.send(attr) }
|
24
|
+
|
25
|
+
class << self
|
26
|
+
def find(*args)
|
27
|
+
if self.options[:as_param] && args.first.is_a?(String)
|
28
|
+
find_by_slug(args)
|
29
|
+
else
|
30
|
+
super(*args)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
include InstanceMethods
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Sluggerize
|
2
|
+
class NoSlugColumnError < StandardError
|
3
|
+
def to_s
|
4
|
+
'There must be a slug column defined in the database table for this model'
|
5
|
+
end
|
6
|
+
end
|
7
|
+
class NoSourceColumnError < StandardError
|
8
|
+
def to_s
|
9
|
+
'The column matching the attribute you want to sluggerize can not be found in the database table'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Sluggerize
|
2
|
+
module InstanceMethods
|
3
|
+
|
4
|
+
def to_param
|
5
|
+
options[:as_param] ? self.slug : self.id
|
6
|
+
end
|
7
|
+
|
8
|
+
protected
|
9
|
+
|
10
|
+
def create_slug
|
11
|
+
self.slug ||= clean("#{column_to_slug}")
|
12
|
+
end
|
13
|
+
|
14
|
+
def clean(string)
|
15
|
+
string.downcase.gsub(/[^\w\s\d\_\-]/,'').gsub(/\s\s+/,' ').gsub(/[^\w\d]/, options[:substitution_char])
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/sluggerize/version.rb
CHANGED
data/sluggerize.gemspec
CHANGED
@@ -12,6 +12,7 @@ Gem::Specification.new do |s|
|
|
12
12
|
s.summary = %q{Simple library for creating a slug column from another string column}
|
13
13
|
s.description = %q{Creates a slug from the specified column of any model.}
|
14
14
|
|
15
|
+
s.add_dependency "activerecord", "~> 3.0"
|
15
16
|
s.rubyforge_project = "sluggerize"
|
16
17
|
|
17
18
|
s.files = `git ls-files`.split("\n")
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'test/test_helper'
|
2
|
+
|
3
|
+
class SluggerizeBaseTest < ActiveSupport::TestCase
|
4
|
+
end
|
5
|
+
|
6
|
+
class SluggerizeTest < SluggerizeBaseTest
|
7
|
+
test "should generate slug on create" do
|
8
|
+
ObjectWithTitleAndSlug.sluggerize
|
9
|
+
|
10
|
+
assert_difference('ObjectWithTitleAndSlug.count') do
|
11
|
+
o = ObjectWithTitleAndSlug.create(:title => 'hello')
|
12
|
+
assert_not_nil o.slug
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
test "should be invalid with a blank slug" do
|
17
|
+
ObjectWithTitleAndSlug.sluggerize
|
18
|
+
|
19
|
+
assert_no_difference('ObjectWithTitleAndSlug.count') do
|
20
|
+
o = ObjectWithTitleAndSlug.create
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
test "should format the source for a slug" do
|
25
|
+
ObjectWithTitleAndSlug.sluggerize
|
26
|
+
|
27
|
+
o = ObjectWithTitleAndSlug.create(:title => 'Jeremy Was Here')
|
28
|
+
assert_equal 'jeremy-was-here', o.slug
|
29
|
+
end
|
30
|
+
|
31
|
+
test "should remove all non url friendly characters" do
|
32
|
+
ObjectWithTitleAndSlug.sluggerize
|
33
|
+
|
34
|
+
o = ObjectWithTitleAndSlug.create(:title => 'a!@#$%^&*()_+a')
|
35
|
+
assert_equal 'a_a', o.slug
|
36
|
+
end
|
37
|
+
|
38
|
+
test "should replace all spaces with the substitution char" do
|
39
|
+
ObjectWithTitleAndSlug.sluggerize
|
40
|
+
|
41
|
+
o = ObjectWithTitleAndSlug.create(:title => 'a a')
|
42
|
+
assert_equal 'a-a', o.slug
|
43
|
+
end
|
44
|
+
|
45
|
+
test "should set defaults if no options are passed in" do
|
46
|
+
ObjectWithTitleAndSlug.sluggerize
|
47
|
+
|
48
|
+
assert_not_nil ObjectWithTitleAndSlug.options[:substitution_char]
|
49
|
+
assert_not_nil ObjectWithTitleAndSlug.options[:as_param]
|
50
|
+
end
|
51
|
+
|
52
|
+
test "should default to as_params true" do
|
53
|
+
ObjectWithTitleAndSlug.sluggerize
|
54
|
+
|
55
|
+
assert ObjectWithTitleAndSlug.options[:as_param]
|
56
|
+
end
|
57
|
+
|
58
|
+
test "should set as_params to true if anything other than false is passed in to parameters" do
|
59
|
+
ObjectWithTitleAndSlug.sluggerize(nil, :as_param => 'test')
|
60
|
+
|
61
|
+
assert ObjectWithTitleAndSlug.options[:as_param]
|
62
|
+
end
|
63
|
+
|
64
|
+
test "should set as_params to false via parameters" do
|
65
|
+
ObjectWithTitleAndSlug.sluggerize(nil, :as_param => false)
|
66
|
+
|
67
|
+
assert !ObjectWithTitleAndSlug.options[:as_param]
|
68
|
+
end
|
69
|
+
|
70
|
+
test "should raise an error if the substitution char is not a string" do
|
71
|
+
assert_raise ArgumentError do
|
72
|
+
ObjectWithTitleAndSlug.sluggerize(nil, :substitution_char => Object)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
test "should raise an error if the source column does not exist" do
|
77
|
+
assert_raise Sluggerize::NoSourceColumnError do
|
78
|
+
ObjectWithNonTitleColumnAndSlug.sluggerize
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
test "should raise an error if the slug column does not exist" do
|
83
|
+
assert_raise Sluggerize::NoSlugColumnError do
|
84
|
+
ObjectWithoutSlug.sluggerize
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_record'
|
5
|
+
require 'active_model'
|
6
|
+
|
7
|
+
$:.unshift "#{File.dirname(__FILE__)}/../"
|
8
|
+
$:.unshift "#{File.dirname(__FILE__)}/../lib/"
|
9
|
+
|
10
|
+
require 'sluggerize'
|
11
|
+
|
12
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
13
|
+
|
14
|
+
ActiveRecord::Schema.define(:version => 1) do
|
15
|
+
create_table :objects_with_title_and_slug do |t|
|
16
|
+
t.string :title
|
17
|
+
t.string :slug
|
18
|
+
end
|
19
|
+
|
20
|
+
create_table :objects_with_non_title_column_and_slug do |t|
|
21
|
+
t.string :name
|
22
|
+
t.boolean :slug
|
23
|
+
end
|
24
|
+
|
25
|
+
create_table :objects_without_slug do |t|
|
26
|
+
t.string :title
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class ObjectWithTitleAndSlug < ActiveRecord::Base
|
31
|
+
self.table_name = 'objects_with_title_and_slug'
|
32
|
+
end
|
33
|
+
|
34
|
+
class ObjectWithNonTitleColumnAndSlug < ActiveRecord::Base
|
35
|
+
self.table_name = 'objects_with_non_title_column_and_slug'
|
36
|
+
end
|
37
|
+
|
38
|
+
class ObjectWithoutSlug < ActiveRecord::Base
|
39
|
+
self.table_name = 'objects_without_slug'
|
40
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sluggerize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.3
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jeremy Hubert
|
@@ -15,9 +15,23 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
19
|
-
dependencies:
|
20
|
-
|
18
|
+
date: 2012-02-04 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: activerecord
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 7
|
29
|
+
segments:
|
30
|
+
- 3
|
31
|
+
- 0
|
32
|
+
version: "3.0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
21
35
|
description: Creates a slug from the specified column of any model.
|
22
36
|
email:
|
23
37
|
- jhubert@gmail.com
|
@@ -28,13 +42,19 @@ extensions: []
|
|
28
42
|
extra_rdoc_files: []
|
29
43
|
|
30
44
|
files:
|
45
|
+
- .gemtest
|
31
46
|
- .gitignore
|
32
47
|
- Gemfile
|
33
48
|
- README.md
|
34
49
|
- Rakefile
|
35
50
|
- lib/sluggerize.rb
|
51
|
+
- lib/sluggerize/class_methods.rb
|
52
|
+
- lib/sluggerize/errors.rb
|
53
|
+
- lib/sluggerize/instance_methods.rb
|
36
54
|
- lib/sluggerize/version.rb
|
37
55
|
- sluggerize.gemspec
|
56
|
+
- test/sluggerize_test.rb
|
57
|
+
- test/test_helper.rb
|
38
58
|
homepage: ""
|
39
59
|
licenses: []
|
40
60
|
|
@@ -68,5 +88,6 @@ rubygems_version: 1.8.15
|
|
68
88
|
signing_key:
|
69
89
|
specification_version: 3
|
70
90
|
summary: Simple library for creating a slug column from another string column
|
71
|
-
test_files:
|
72
|
-
|
91
|
+
test_files:
|
92
|
+
- test/sluggerize_test.rb
|
93
|
+
- test/test_helper.rb
|