rdavila_friendly_id 2.2.6
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +189 -0
- data/LICENSE +19 -0
- data/README.rdoc +384 -0
- data/Rakefile +35 -0
- data/extras/README.txt +3 -0
- data/extras/template-gem.rb +26 -0
- data/extras/template-plugin.rb +28 -0
- data/generators/friendly_id/friendly_id_generator.rb +28 -0
- data/generators/friendly_id/templates/create_slugs.rb +18 -0
- data/generators/friendly_id_20_upgrade/friendly_id_20_upgrade_generator.rb +12 -0
- data/generators/friendly_id_20_upgrade/templates/upgrade_friendly_id_to_20.rb +19 -0
- data/init.rb +1 -0
- data/lib/friendly_id.rb +83 -0
- data/lib/friendly_id/helpers.rb +12 -0
- data/lib/friendly_id/non_sluggable_class_methods.rb +34 -0
- data/lib/friendly_id/non_sluggable_instance_methods.rb +45 -0
- data/lib/friendly_id/slug.rb +98 -0
- data/lib/friendly_id/sluggable_class_methods.rb +113 -0
- data/lib/friendly_id/sluggable_instance_methods.rb +161 -0
- data/lib/friendly_id/tasks.rb +92 -0
- data/lib/friendly_id/version.rb +8 -0
- data/lib/tasks/friendly_id.rake +40 -0
- data/lib/tasks/friendly_id.rb +1 -0
- data/test/cached_slug_test.rb +109 -0
- data/test/custom_slug_normalizer_test.rb +36 -0
- data/test/non_slugged_test.rb +99 -0
- data/test/scoped_model_test.rb +64 -0
- data/test/slug_test.rb +105 -0
- data/test/slugged_model_test.rb +348 -0
- data/test/sti_test.rb +49 -0
- data/test/support/database.yml.postgres +6 -0
- data/test/support/database.yml.sqlite3 +2 -0
- data/test/support/models.rb +45 -0
- data/test/tasks_test.rb +105 -0
- data/test/test_helper.rb +104 -0
- metadata +144 -0
data/test/sti_test.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class STIModelTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "A slugged model using single table inheritance" do
|
6
|
+
|
7
|
+
setup do
|
8
|
+
Novel.friendly_id_options = FriendlyId::DEFAULT_OPTIONS.merge(:method => :name, :use_slug => true)
|
9
|
+
@novel = Novel.new :name => "Test novel"
|
10
|
+
@novel.save!
|
11
|
+
end
|
12
|
+
|
13
|
+
teardown do
|
14
|
+
Novel.delete_all
|
15
|
+
Slug.delete_all
|
16
|
+
end
|
17
|
+
|
18
|
+
should "have a slug" do
|
19
|
+
assert_not_nil @novel.slug
|
20
|
+
end
|
21
|
+
|
22
|
+
context "found by its friendly id" do
|
23
|
+
|
24
|
+
setup do
|
25
|
+
@novel = Novel.find(@novel.friendly_id)
|
26
|
+
end
|
27
|
+
|
28
|
+
should "not indicate that it has a better id" do
|
29
|
+
assert !@novel.has_better_id?
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
context "found by its numeric id" do
|
36
|
+
|
37
|
+
setup do
|
38
|
+
@novel = Novel.find(@novel.id)
|
39
|
+
end
|
40
|
+
|
41
|
+
should "indicate that it has a better id" do
|
42
|
+
assert @novel.has_better_id?
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
class CreateSupportModels < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :books do |t|
|
4
|
+
t.string :name
|
5
|
+
t.string :type
|
6
|
+
end
|
7
|
+
create_table :cities do |t|
|
8
|
+
t.string :name
|
9
|
+
t.string :my_slug
|
10
|
+
t.integer :population
|
11
|
+
end
|
12
|
+
create_table :countries do |t|
|
13
|
+
t.string :name
|
14
|
+
end
|
15
|
+
create_table :districts do |t|
|
16
|
+
t.string :name
|
17
|
+
t.string :cached_slug
|
18
|
+
end
|
19
|
+
create_table :events do |t|
|
20
|
+
t.string :name
|
21
|
+
t.datetime :event_date
|
22
|
+
end
|
23
|
+
create_table :legacy_table do |t|
|
24
|
+
t.string :name
|
25
|
+
end
|
26
|
+
create_table :people do |t|
|
27
|
+
t.string :name
|
28
|
+
end
|
29
|
+
create_table :posts do |t|
|
30
|
+
t.string :name
|
31
|
+
t.boolean :published
|
32
|
+
end
|
33
|
+
create_table :residents do |t|
|
34
|
+
t.string :name
|
35
|
+
t.integer :country_id
|
36
|
+
end
|
37
|
+
create_table :users do |t|
|
38
|
+
t.string :name
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.down
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
data/test/tasks_test.rb
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
require "friendly_id/tasks"
|
3
|
+
|
4
|
+
class TasksTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
context "FriendlyId tasks" do
|
7
|
+
|
8
|
+
should "parse a top-level class name and return a class" do
|
9
|
+
assert_equal String, FriendlyId::Tasks.parse_class_name("String")
|
10
|
+
end
|
11
|
+
|
12
|
+
should "parse a namespaced class name and return a class" do
|
13
|
+
assert_equal Test::Unit, FriendlyId::Tasks.parse_class_name("Test::Unit")
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
context "The 'make slugs' task" do
|
19
|
+
|
20
|
+
setup do
|
21
|
+
City.create! :name => "Buenos Aires"
|
22
|
+
City.create! :name => "Rio de Janeiro"
|
23
|
+
City.create! :name => "Tokyo"
|
24
|
+
City.create! :name => "Nairobi"
|
25
|
+
Slug.delete_all
|
26
|
+
end
|
27
|
+
|
28
|
+
teardown do
|
29
|
+
City.delete_all
|
30
|
+
Slug.delete_all
|
31
|
+
end
|
32
|
+
|
33
|
+
should "make one slug per model" do
|
34
|
+
assert_equal 0, Slug.count
|
35
|
+
FriendlyId::Tasks.make_slugs("City")
|
36
|
+
assert_equal 4, Slug.count
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
context "The 'delete_slugs_for' task" do
|
42
|
+
|
43
|
+
setup do
|
44
|
+
@post = Post.create! :name => "Slugs Considered Harmful"
|
45
|
+
@city = City.create! :name => "Buenos Aires"
|
46
|
+
end
|
47
|
+
|
48
|
+
teardown do
|
49
|
+
Post.delete_all
|
50
|
+
City.delete_all
|
51
|
+
Slug.delete_all
|
52
|
+
end
|
53
|
+
|
54
|
+
should "Delete only slugs for the specified model" do
|
55
|
+
assert_equal 2, Slug.count
|
56
|
+
FriendlyId::Tasks.delete_slugs_for("City")
|
57
|
+
assert_equal 1, Slug.count
|
58
|
+
end
|
59
|
+
|
60
|
+
should "set the cached_slug column to NULL" do
|
61
|
+
District.create! :name => "Garment"
|
62
|
+
FriendlyId::Tasks.delete_slugs_for("District")
|
63
|
+
assert_nil District.first.cached_slug
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
context "The 'delete_old_slugs' task" do
|
69
|
+
|
70
|
+
setup do
|
71
|
+
@post = Post.create! :name => "Slugs Considered Harmful"
|
72
|
+
@city = City.create! :name => "Buenos Aires"
|
73
|
+
City.connection.execute "UPDATE slugs SET created_at = '%s' WHERE id = %d" % [
|
74
|
+
45.days.ago.strftime("%Y-%m-%d"), @city.slug.id]
|
75
|
+
@city.name = "Ciudad de Buenos Aires"
|
76
|
+
@city.save!
|
77
|
+
end
|
78
|
+
|
79
|
+
teardown do
|
80
|
+
Post.delete_all
|
81
|
+
City.delete_all
|
82
|
+
Slug.delete_all
|
83
|
+
end
|
84
|
+
|
85
|
+
should "delete slugs older than 45 days by default" do
|
86
|
+
assert_equal 3, Slug.count
|
87
|
+
FriendlyId::Tasks.delete_old_slugs
|
88
|
+
assert_equal 2, Slug.count
|
89
|
+
end
|
90
|
+
|
91
|
+
should "respect the days argument" do
|
92
|
+
assert_equal 3, Slug.count
|
93
|
+
FriendlyId::Tasks.delete_old_slugs(100)
|
94
|
+
assert_equal 3, Slug.count
|
95
|
+
end
|
96
|
+
|
97
|
+
should "respect the class argument" do
|
98
|
+
assert_equal 3, Slug.count
|
99
|
+
FriendlyId::Tasks.delete_old_slugs(1, "Post")
|
100
|
+
assert_equal 3, Slug.count
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
2
|
+
$:.unshift(File.dirname(__FILE__))
|
3
|
+
|
4
|
+
$KCODE = 'UTF8' if RUBY_VERSION < '1.9'
|
5
|
+
$VERBOSE = false
|
6
|
+
|
7
|
+
require 'rubygems'
|
8
|
+
require 'test/unit'
|
9
|
+
require 'contest'
|
10
|
+
require 'mocha'
|
11
|
+
|
12
|
+
# You can use "rake test AR_VERSION=2.2.3" to test against 2.2.3 for example.
|
13
|
+
# The default is to use the latest installed ActiveRecord.
|
14
|
+
if ENV["AR_VERSION"]
|
15
|
+
gem 'activerecord', "#{ENV["AR_VERSION"]}"
|
16
|
+
gem 'activesupport', "#{ENV["AR_VERSION"]}"
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'active_record'
|
20
|
+
require 'active_support'
|
21
|
+
require 'friendly_id'
|
22
|
+
require File.dirname(__FILE__) + '/../generators/friendly_id/templates/create_slugs'
|
23
|
+
require File.dirname(__FILE__) + '/support/models'
|
24
|
+
|
25
|
+
local_db_settings = File.dirname(__FILE__) + '/support/database.yml'
|
26
|
+
default_db_settings = File.dirname(__FILE__) + '/support/database.yml.sqlite3'
|
27
|
+
db_settings = File.exists?(local_db_settings) ? local_db_settings : default_db_settings
|
28
|
+
ActiveRecord::Base.establish_connection(YAML::load(File.open(db_settings)))
|
29
|
+
|
30
|
+
class ActiveRecord::Base
|
31
|
+
def log_protected_attribute_removal(*args) end
|
32
|
+
end
|
33
|
+
|
34
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
35
|
+
ActiveRecord::Base.connection.drop_table(table)
|
36
|
+
end
|
37
|
+
ActiveRecord::Migration.verbose = false
|
38
|
+
CreateSlugs.up
|
39
|
+
CreateSupportModels.up
|
40
|
+
|
41
|
+
# A model that uses the automagically configured "cached_slug" column
|
42
|
+
class District < ActiveRecord::Base
|
43
|
+
has_friendly_id :name, :use_slug => true
|
44
|
+
end
|
45
|
+
|
46
|
+
# A model that specifies a custom cached slug column
|
47
|
+
class City < ActiveRecord::Base
|
48
|
+
attr_accessible :name
|
49
|
+
has_friendly_id :name, :use_slug => true, :cache_column => 'my_slug'
|
50
|
+
end
|
51
|
+
|
52
|
+
# A model with a custom slug text normalizer
|
53
|
+
class Person < ActiveRecord::Base
|
54
|
+
has_friendly_id :name, :use_slug => true do |text|
|
55
|
+
text.upcase
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# A slugged model that uses a scope
|
60
|
+
class Resident < ActiveRecord::Base
|
61
|
+
belongs_to :country
|
62
|
+
has_friendly_id :name, :use_slug => true, :scope => :country
|
63
|
+
end
|
64
|
+
|
65
|
+
# A model used as a scope
|
66
|
+
class Country < ActiveRecord::Base
|
67
|
+
has_many :people
|
68
|
+
has_friendly_id :name, :use_slug => true
|
69
|
+
end
|
70
|
+
|
71
|
+
# A model that doesn't use slugs
|
72
|
+
class User < ActiveRecord::Base
|
73
|
+
has_friendly_id :name
|
74
|
+
end
|
75
|
+
|
76
|
+
# A model that uses default slug settings and has a named scope
|
77
|
+
class Post < ActiveRecord::Base
|
78
|
+
has_friendly_id :name, :use_slug => true
|
79
|
+
named_scope :published, :conditions => { :published => true }
|
80
|
+
end
|
81
|
+
|
82
|
+
# Model that uses a custom table name
|
83
|
+
class Place < ActiveRecord::Base
|
84
|
+
self.table_name = "legacy_table"
|
85
|
+
has_friendly_id :name, :use_slug => true
|
86
|
+
end
|
87
|
+
|
88
|
+
# A model that uses a datetime field for its friendly_id
|
89
|
+
class Event < ActiveRecord::Base
|
90
|
+
has_friendly_id :event_date, :use_slug => true
|
91
|
+
end
|
92
|
+
|
93
|
+
# A base model for single table inheritence
|
94
|
+
class Book < ActiveRecord::Base ; end
|
95
|
+
|
96
|
+
# A model that uses STI
|
97
|
+
class Novel < ::Book
|
98
|
+
has_friendly_id :name, :use_slug => true
|
99
|
+
end
|
100
|
+
|
101
|
+
# A model with no table
|
102
|
+
class Question < ActiveRecord::Base
|
103
|
+
has_friendly_id :name, :use_slug => true
|
104
|
+
end
|
metadata
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rdavila_friendly_id
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.2.6
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Norman Clarke
|
8
|
+
- Adrian Mugnolo
|
9
|
+
- Emilio Tagua
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
|
14
|
+
date: 2010-01-20 00:00:00 -05:00
|
15
|
+
default_executable:
|
16
|
+
dependencies:
|
17
|
+
- !ruby/object:Gem::Dependency
|
18
|
+
name: activerecord
|
19
|
+
type: :runtime
|
20
|
+
version_requirement:
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: 2.2.3
|
26
|
+
version:
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
type: :runtime
|
30
|
+
version_requirement:
|
31
|
+
version_requirements: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 2.2.3
|
36
|
+
version:
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: contest
|
39
|
+
type: :development
|
40
|
+
version_requirement:
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
description: " FriendlyId is the \"Swiss Army bulldozer\" of slugging and permalink\n plugins for Ruby on Rails. It allows you to create pretty URL\xE2\x80\x99s\n and work with human-friendly strings as if they were numeric ids for\n ActiveRecord models.\n"
|
48
|
+
email:
|
49
|
+
- norman@njclarke.com
|
50
|
+
- adrian@mugnolo.com
|
51
|
+
- miloops@gmail.com
|
52
|
+
executables: []
|
53
|
+
|
54
|
+
extensions: []
|
55
|
+
|
56
|
+
extra_rdoc_files:
|
57
|
+
- README.rdoc
|
58
|
+
- History.txt
|
59
|
+
files:
|
60
|
+
- lib/friendly_id/version.rb
|
61
|
+
- lib/friendly_id/slug.rb
|
62
|
+
- lib/friendly_id/tasks.rb
|
63
|
+
- lib/friendly_id/non_sluggable_class_methods.rb
|
64
|
+
- lib/friendly_id/sluggable_class_methods.rb
|
65
|
+
- lib/friendly_id/non_sluggable_instance_methods.rb
|
66
|
+
- lib/friendly_id/sluggable_instance_methods.rb
|
67
|
+
- lib/friendly_id/helpers.rb
|
68
|
+
- lib/tasks/friendly_id.rb
|
69
|
+
- lib/friendly_id.rb
|
70
|
+
- lib/tasks/friendly_id.rake
|
71
|
+
- init.rb
|
72
|
+
- README.rdoc
|
73
|
+
- History.txt
|
74
|
+
- LICENSE
|
75
|
+
- Rakefile
|
76
|
+
- generators/friendly_id/templates/create_slugs.rb
|
77
|
+
- generators/friendly_id/friendly_id_generator.rb
|
78
|
+
- generators/friendly_id_20_upgrade/templates/upgrade_friendly_id_to_20.rb
|
79
|
+
- generators/friendly_id_20_upgrade/friendly_id_20_upgrade_generator.rb
|
80
|
+
- test/tasks_test.rb
|
81
|
+
- test/support/database.yml.postgres
|
82
|
+
- test/support/database.yml.sqlite3
|
83
|
+
- test/support/models.rb
|
84
|
+
- test/custom_slug_normalizer_test.rb
|
85
|
+
- test/slug_test.rb
|
86
|
+
- test/slugged_model_test.rb
|
87
|
+
- test/scoped_model_test.rb
|
88
|
+
- test/non_slugged_test.rb
|
89
|
+
- test/sti_test.rb
|
90
|
+
- test/test_helper.rb
|
91
|
+
- test/cached_slug_test.rb
|
92
|
+
- extras/README.txt
|
93
|
+
- extras/template-plugin.rb
|
94
|
+
- extras/template-gem.rb
|
95
|
+
has_rdoc: true
|
96
|
+
homepage: http://rdoc.info/projects/norman/friendly_id
|
97
|
+
licenses: []
|
98
|
+
|
99
|
+
post_install_message: |+
|
100
|
+
|
101
|
+
***********************************************************
|
102
|
+
|
103
|
+
If you are upgrading friendly_id, please run
|
104
|
+
|
105
|
+
./script/generate friendly_id --skip-migration
|
106
|
+
|
107
|
+
in your Rails application to ensure that you have the
|
108
|
+
latest friendly_id Rake tasks.
|
109
|
+
|
110
|
+
***********************************************************
|
111
|
+
|
112
|
+
rdoc_options:
|
113
|
+
- --main
|
114
|
+
- README.rdoc
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: "0"
|
122
|
+
version:
|
123
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: "0"
|
128
|
+
version:
|
129
|
+
requirements: []
|
130
|
+
|
131
|
+
rubyforge_project: friendly-id
|
132
|
+
rubygems_version: 1.3.5
|
133
|
+
signing_key:
|
134
|
+
specification_version: 3
|
135
|
+
summary: A comprehensive slugging and pretty-URL plugin for ActiveRecord.
|
136
|
+
test_files:
|
137
|
+
- test/tasks_test.rb
|
138
|
+
- test/custom_slug_normalizer_test.rb
|
139
|
+
- test/slug_test.rb
|
140
|
+
- test/slugged_model_test.rb
|
141
|
+
- test/scoped_model_test.rb
|
142
|
+
- test/non_slugged_test.rb
|
143
|
+
- test/sti_test.rb
|
144
|
+
- test/cached_slug_test.rb
|