pretty_slugs 0.0.3 → 0.0.4
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/Gemfile +7 -0
- data/Guardfile +24 -0
- data/lib/pretty_slugs/version.rb +1 -1
- data/lib/pretty_slugs.rb +13 -3
- data/test/lib/pretty_slugs/test_pretty_slugs.rb +91 -0
- data/test/test_helper.rb +31 -0
- metadata +27 -41
data/Gemfile
CHANGED
@@ -6,3 +6,10 @@ gem 'activerecord', ">= 3.0.0", "~> 3.2"
|
|
6
6
|
gem 'activesupport', ">= 3.0.0", "~> 3.2"
|
7
7
|
gem 'railties', ">= 3.0.0", "~> 3.2"
|
8
8
|
gem 'rake'
|
9
|
+
group :development do
|
10
|
+
gem 'sqlite3'
|
11
|
+
gem 'minitest'
|
12
|
+
gem 'minitest-matchers'
|
13
|
+
gem 'guard-minitest'
|
14
|
+
gem 'pry'
|
15
|
+
end
|
data/Guardfile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard 'minitest' do
|
5
|
+
# with Minitest::Unit
|
6
|
+
watch(%r|^test/(.*)\/?test_(.*)\.rb|)
|
7
|
+
watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
|
8
|
+
watch(%r|^test/test_helper\.rb|) { "test" }
|
9
|
+
|
10
|
+
# with Minitest::Spec
|
11
|
+
# watch(%r|^spec/(.*)_spec\.rb|)
|
12
|
+
# watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
|
13
|
+
# watch(%r|^spec/spec_helper\.rb|) { "spec" }
|
14
|
+
|
15
|
+
# Rails 3.2
|
16
|
+
# watch(%r|^app/controllers/(.*)\.rb|) { |m| "test/controllers/#{m[1]}_test.rb" }
|
17
|
+
# watch(%r|^app/helpers/(.*)\.rb|) { |m| "test/helpers/#{m[1]}_test.rb" }
|
18
|
+
# watch(%r|^app/models/(.*)\.rb|) { |m| "test/unit/#{m[1]}_test.rb" }
|
19
|
+
|
20
|
+
# Rails
|
21
|
+
# watch(%r|^app/controllers/(.*)\.rb|) { |m| "test/functional/#{m[1]}_test.rb" }
|
22
|
+
# watch(%r|^app/helpers/(.*)\.rb|) { |m| "test/helpers/#{m[1]}_test.rb" }
|
23
|
+
# watch(%r|^app/models/(.*)\.rb|) { |m| "test/unit/#{m[1]}_test.rb" }
|
24
|
+
end
|
data/lib/pretty_slugs/version.rb
CHANGED
data/lib/pretty_slugs.rb
CHANGED
@@ -1,15 +1,21 @@
|
|
1
|
-
require "pretty_slugs/version"
|
1
|
+
# require "pretty_slugs/version"
|
2
|
+
require 'active_support/concern'
|
3
|
+
require 'active_record'
|
2
4
|
|
3
5
|
module PrettySlugs
|
4
6
|
extend ActiveSupport::Concern
|
5
7
|
included do
|
6
|
-
|
8
|
+
attr_accessible :slug
|
7
9
|
|
8
10
|
after_commit lambda {
|
9
11
|
action = transaction_include_action?(:create) ? "create" : transaction_include_action?(:destroy) ? "destroy" : "save"
|
10
12
|
case action
|
11
13
|
when "destroy"
|
12
14
|
remove_slug
|
15
|
+
when "create"
|
16
|
+
check_slug_existence
|
17
|
+
when "save"
|
18
|
+
check_slug_existence
|
13
19
|
end
|
14
20
|
}
|
15
21
|
|
@@ -46,6 +52,9 @@ module PrettySlugs
|
|
46
52
|
end
|
47
53
|
|
48
54
|
def slug=(val)
|
55
|
+
# the function of to_slug(val) means that if I try to store a
|
56
|
+
# custom slug, it will make sure it is slug-worthy, lowercase, underscored, etc...
|
57
|
+
@slugstorage = to_slug(val) and return if self.new_record?
|
49
58
|
check_slug_existence
|
50
59
|
if val != nil && val != ""
|
51
60
|
slug = val
|
@@ -65,7 +74,7 @@ module PrettySlugs
|
|
65
74
|
def generate_slug
|
66
75
|
inc = 1
|
67
76
|
slug = self.to_slug
|
68
|
-
while !Slug.
|
77
|
+
while !Slug.find_by_slug_and_sluggable_class(slug, self.class.to_s).nil?
|
69
78
|
slug += "-#{inc}"
|
70
79
|
inc += 1
|
71
80
|
end
|
@@ -95,6 +104,7 @@ module PrettySlugs
|
|
95
104
|
end
|
96
105
|
|
97
106
|
def to_slug(val=nil)
|
107
|
+
return @slugstorage if @slugstorage
|
98
108
|
word = self[self.respond_to?("name") ? "name" : "title"] rescue self["id"]
|
99
109
|
word = self["id"].to_s if word.nil?
|
100
110
|
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require_relative '../../test_helper'
|
2
|
+
require 'active_record'
|
3
|
+
|
4
|
+
class Page < ActiveRecord::Base
|
5
|
+
include PrettySlugs
|
6
|
+
attr_accessible :name, :title, :body
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
describe PrettySlugs do
|
11
|
+
|
12
|
+
before do
|
13
|
+
Page.destroy_all
|
14
|
+
PrettySlugs::Slug.destroy_all
|
15
|
+
@tim = Page.new(:name => "Timothy Johnson", :title => "Rails Peanut", :body => "Tim is a poser")
|
16
|
+
@timothy = Page.new(:name => "Timothy Johnson", :title => "Poop", :body => "sdfsf sd")
|
17
|
+
@joe = Page.new(:name => "Joe Ruoto", :slug => "joey_r", :title => "the man")
|
18
|
+
end
|
19
|
+
|
20
|
+
it "must be defined" do
|
21
|
+
PrettySlugs.wont_be_nil
|
22
|
+
end
|
23
|
+
|
24
|
+
it "must have a Page" do
|
25
|
+
Page.wont_be_nil
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
it "Page and Active Record save and retrieval work correctly" do
|
30
|
+
@tim.save
|
31
|
+
Page.first.name.must_equal "Timothy Johnson"
|
32
|
+
end
|
33
|
+
|
34
|
+
it "must create a slug on saving the parent model" do
|
35
|
+
@tim.save
|
36
|
+
Page.find("timothy_johnson").must_equal @tim
|
37
|
+
end
|
38
|
+
|
39
|
+
it "must create a slug on a saved Page" do
|
40
|
+
@tim.save
|
41
|
+
@tim.slug.must_equal "timothy_johnson"
|
42
|
+
Page.find("timothy_johnson").must_equal @tim
|
43
|
+
end
|
44
|
+
|
45
|
+
it "must not create a slug on non-saved Page" do
|
46
|
+
@tim.slug.must_equal ''
|
47
|
+
end
|
48
|
+
|
49
|
+
it "must create a new slug when a previous one is similar" do
|
50
|
+
@tim.save
|
51
|
+
@timothy.save
|
52
|
+
@tim.slug.must_equal 'timothy_johnson'
|
53
|
+
@timothy.slug.must_equal 'timothy_johnson-1'
|
54
|
+
Page.find("timothy_johnson").must_equal @tim
|
55
|
+
Page.find("timothy_johnson-1").must_equal @timothy
|
56
|
+
end
|
57
|
+
|
58
|
+
it "must assign a manual slug correctly" do
|
59
|
+
@joe.save
|
60
|
+
@joe.slug.must_equal "joey_r"
|
61
|
+
Page.find("joey_r").must_equal @joe
|
62
|
+
end
|
63
|
+
|
64
|
+
it "must assign a manual slug after saving correctly" do
|
65
|
+
@joe.save
|
66
|
+
@joe.slug = "jimminy"
|
67
|
+
@joe.save
|
68
|
+
@joe.slug.must_equal "jimminy"
|
69
|
+
Page.find("jimminy").must_equal @joe
|
70
|
+
end
|
71
|
+
|
72
|
+
it "must automatically create a slug, and allow overriding" do
|
73
|
+
@tim.save
|
74
|
+
Page.find("timothy_johnson").must_equal @tim
|
75
|
+
@tim.slug = "timmy"
|
76
|
+
@tim.save
|
77
|
+
Page.find("timothy_johnson").must_be_nil
|
78
|
+
Page.find("timmy").must_equal @tim
|
79
|
+
end
|
80
|
+
|
81
|
+
it "must handle case-sensitive slugs just like matching slugs" do
|
82
|
+
@jon = Page.new(:name => "Jon Teimann", :slug => "jon")
|
83
|
+
@jont = Page.new(:name => "Jon Teimann", :slug => "Jon")
|
84
|
+
@jon.save
|
85
|
+
@jont.save
|
86
|
+
Page.find("jon").must_equal @jon
|
87
|
+
Page.find("jon-1").must_equal @jont
|
88
|
+
@jont.slug.must_equal "jon-1"
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
require "minitest/matchers"
|
4
|
+
require 'minitest/autorun'
|
5
|
+
require 'minitest/pride'
|
6
|
+
require File.expand_path('../../lib/pretty_slugs.rb', __FILE__)
|
7
|
+
|
8
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3",
|
9
|
+
:database => ':memory:')
|
10
|
+
|
11
|
+
ActiveRecord::Schema.define do
|
12
|
+
self.verbose = false
|
13
|
+
|
14
|
+
create_table :pages, :force => true do |t|
|
15
|
+
t.string :name
|
16
|
+
t.string :title
|
17
|
+
t.string :body
|
18
|
+
end
|
19
|
+
|
20
|
+
create_table :pretty_slugs, :force => true do |t|
|
21
|
+
t.string :slug, null: false
|
22
|
+
t.integer :sluggable_id, null: false
|
23
|
+
t.string :sluggable_class, null: false
|
24
|
+
t.timestamps
|
25
|
+
end
|
26
|
+
|
27
|
+
add_index :pretty_slugs, :sluggable_id
|
28
|
+
add_index :pretty_slugs, [:slug, :sluggable_class]
|
29
|
+
add_index :pretty_slugs, :sluggable_class
|
30
|
+
|
31
|
+
end
|
metadata
CHANGED
@@ -1,37 +1,28 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: pretty_slugs
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 3
|
10
|
-
version: 0.0.3
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Kevin Hopkins
|
14
9
|
- Timothy Johnson
|
15
10
|
autorequire:
|
16
11
|
bindir: bin
|
17
12
|
cert_chain: []
|
18
|
-
|
19
|
-
date: 2013-03-01 00:00:00 Z
|
13
|
+
date: 2013-03-04 00:00:00.000000000 Z
|
20
14
|
dependencies: []
|
21
|
-
|
22
15
|
description: Pretty Slugs for Rails models
|
23
|
-
email:
|
16
|
+
email:
|
24
17
|
- kevin@h-pk-ns.com
|
25
18
|
- timothy@wearefound.com
|
26
19
|
executables: []
|
27
|
-
|
28
20
|
extensions: []
|
29
|
-
|
30
21
|
extra_rdoc_files: []
|
31
|
-
|
32
|
-
files:
|
22
|
+
files:
|
33
23
|
- .gitignore
|
34
24
|
- Gemfile
|
25
|
+
- Guardfile
|
35
26
|
- LICENSE
|
36
27
|
- README.md
|
37
28
|
- Rakefile
|
@@ -40,38 +31,33 @@ files:
|
|
40
31
|
- lib/pretty_slugs/migration.rb
|
41
32
|
- lib/pretty_slugs/version.rb
|
42
33
|
- pretty_slugs.gemspec
|
43
|
-
|
34
|
+
- test/lib/pretty_slugs/test_pretty_slugs.rb
|
35
|
+
- test/test_helper.rb
|
36
|
+
homepage: ''
|
44
37
|
licenses: []
|
45
|
-
|
46
38
|
post_install_message:
|
47
39
|
rdoc_options: []
|
48
|
-
|
49
|
-
require_paths:
|
40
|
+
require_paths:
|
50
41
|
- lib
|
51
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
43
|
none: false
|
53
|
-
requirements:
|
54
|
-
- -
|
55
|
-
- !ruby/object:Gem::Version
|
56
|
-
|
57
|
-
|
58
|
-
- 0
|
59
|
-
version: "0"
|
60
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
49
|
none: false
|
62
|
-
requirements:
|
63
|
-
- -
|
64
|
-
- !ruby/object:Gem::Version
|
65
|
-
|
66
|
-
segments:
|
67
|
-
- 0
|
68
|
-
version: "0"
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
69
54
|
requirements: []
|
70
|
-
|
71
55
|
rubyforge_project:
|
72
56
|
rubygems_version: 1.8.24
|
73
57
|
signing_key:
|
74
58
|
specification_version: 3
|
75
|
-
summary: Pretty Slugs stores sluggified strings for rails models in an extensible
|
76
|
-
|
77
|
-
|
59
|
+
summary: Pretty Slugs stores sluggified strings for rails models in an extensible
|
60
|
+
way
|
61
|
+
test_files:
|
62
|
+
- test/lib/pretty_slugs/test_pretty_slugs.rb
|
63
|
+
- test/test_helper.rb
|