mil_friendly_id 4.0.9.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/.gemtest +0 -0
  3. data/.gitignore +12 -0
  4. data/.travis.yml +20 -0
  5. data/.yardopts +4 -0
  6. data/Changelog.md +86 -0
  7. data/Gemfile +15 -0
  8. data/Guide.rdoc +553 -0
  9. data/MIT-LICENSE +19 -0
  10. data/README.md +150 -0
  11. data/Rakefile +108 -0
  12. data/WhatsNew.md +95 -0
  13. data/bench.rb +63 -0
  14. data/friendly_id.gemspec +43 -0
  15. data/gemfiles/Gemfile.rails-3.0.rb +21 -0
  16. data/gemfiles/Gemfile.rails-3.1.rb +22 -0
  17. data/gemfiles/Gemfile.rails-3.2.rb +22 -0
  18. data/geothird_friendly_id.gemspec +45 -0
  19. data/lib/friendly_id.rb +114 -0
  20. data/lib/friendly_id/base.rb +291 -0
  21. data/lib/friendly_id/configuration.rb +80 -0
  22. data/lib/friendly_id/finder_methods.rb +35 -0
  23. data/lib/friendly_id/globalize.rb +115 -0
  24. data/lib/friendly_id/history.rb +134 -0
  25. data/lib/friendly_id/migration.rb +19 -0
  26. data/lib/friendly_id/object_utils.rb +50 -0
  27. data/lib/friendly_id/reserved.rb +68 -0
  28. data/lib/friendly_id/scoped.rb +149 -0
  29. data/lib/friendly_id/simple_i18n.rb +95 -0
  30. data/lib/friendly_id/slug.rb +14 -0
  31. data/lib/friendly_id/slug_generator.rb +80 -0
  32. data/lib/friendly_id/slugged.rb +329 -0
  33. data/lib/generators/friendly_id_generator.rb +17 -0
  34. data/mil_friendly_id.gemspec +45 -0
  35. data/test/base_test.rb +72 -0
  36. data/test/compatibility/ancestry/Gemfile +8 -0
  37. data/test/compatibility/ancestry/ancestry_test.rb +34 -0
  38. data/test/compatibility/threading/Gemfile +8 -0
  39. data/test/compatibility/threading/threading.rb +45 -0
  40. data/test/configuration_test.rb +48 -0
  41. data/test/core_test.rb +48 -0
  42. data/test/databases.yml +19 -0
  43. data/test/generator_test.rb +20 -0
  44. data/test/globalize_test.rb +57 -0
  45. data/test/helper.rb +87 -0
  46. data/test/history_test.rb +149 -0
  47. data/test/object_utils_test.rb +28 -0
  48. data/test/reserved_test.rb +40 -0
  49. data/test/schema.rb +79 -0
  50. data/test/scoped_test.rb +83 -0
  51. data/test/shared.rb +156 -0
  52. data/test/simple_i18n_test.rb +133 -0
  53. data/test/slugged_test.rb +280 -0
  54. data/test/sti_test.rb +77 -0
  55. metadata +262 -0
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2008-2010 Norman Clarke, Adrian Mugnolo and Emilio Tagua.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
@@ -0,0 +1,150 @@
1
+ # FriendlyId
2
+
3
+ [![Build Status](http://travis-ci.org/norman/friendly_id.png)](http://travis-ci.org/norman/friendly_id)
4
+
5
+ FriendlyId is the "Swiss Army bulldozer" of slugging and permalink plugins for
6
+ Ruby on Rails. It allows you to create pretty URLs and work with human-friendly
7
+ strings as if they were numeric ids for Active Record models.
8
+
9
+ Using FriendlyId, it's easy to make your application use URLs like:
10
+
11
+ http://example.com/states/washington
12
+
13
+ instead of:
14
+
15
+ http://example.com/states/4323454
16
+
17
+
18
+ ## FriendlyId Features
19
+
20
+ FriendlyId offers many advanced features, including: slug history and
21
+ versioning, i18n, Globalize support, scoped slugs, reserved words, and custom
22
+ slug generators.
23
+
24
+ FriendlyId is compatible with Active Record **3.0** and higher.
25
+
26
+ ## Version 4.x
27
+
28
+ FriendlyId 4.x introduces many changes incompatible with 3.x. If you're
29
+ upgrading, please [read the
30
+ docs](http://rubydoc.info/github/norman/friendly_id/master/file/WhatsNew.md) to see what's
31
+ new.
32
+
33
+ ## Docs
34
+
35
+ The current docs can always be found
36
+ [here](http://rubydoc.info/github/norman/friendly_id/master/frames).
37
+
38
+ The best place to start is with the
39
+ [Guide](http://rubydoc.info/github/norman/friendly_id/master/file/Guide.rdoc),
40
+ which compiles the top-level RDocs into one outlined document.
41
+
42
+ You might also want to watch Ryan Bates's [Railscast on FriendlyId](http://railscasts.com/episodes/314-pretty-urls-with-friendlyid).
43
+
44
+ ## Rails Quickstart
45
+
46
+ gem install friendly_id
47
+
48
+ rails new my_app
49
+
50
+ cd my_app
51
+
52
+ gem "friendly_id", "~> 4.0.1"
53
+
54
+ rails generate scaffold user name:string slug:string
55
+
56
+ # edit db/migrate/*_create_users.rb
57
+ add_index :users, :slug, unique: true
58
+
59
+ rake db:migrate
60
+
61
+ # edit app/models/user.rb
62
+ class User < ActiveRecord::Base
63
+ extend FriendlyId
64
+ friendly_id :name, use: :slugged
65
+ end
66
+
67
+ User.create! name: "Joe Schmoe"
68
+
69
+ rails server
70
+
71
+ GET http://localhost:3000/users/joe-schmoe
72
+
73
+ # If you're adding FriendlyId to an existing app and need
74
+ # to generate slugs for existing users, do this from the
75
+ # console, runner, or add a Rake task:
76
+ User.find_each(&:save)
77
+
78
+
79
+ ## Benchmarks
80
+
81
+ The latest benchmarks for FriendlyId are maintained
82
+ [here](http://bit.ly/friendly-id-benchmarks).
83
+
84
+
85
+ ## Bugs
86
+
87
+ Please report them on the [Github issue
88
+ tracker](http://github.com/norman/friendly_id/issues) for this project.
89
+
90
+ If you have a bug to report, please include the following information:
91
+
92
+ * **Version information for FriendlyId, Rails and Ruby.**
93
+ * Full stack trace and error message (if you have them).
94
+ * Any snippets of relevant model, view or controller code that shows how you
95
+ are using FriendlyId.
96
+
97
+ If you are able to, it helps even more if you can fork FriendlyId on Github,
98
+ and add a test that reproduces the error you are experiencing.
99
+
100
+ For more info on how to report bugs, please see [this
101
+ article](http://yourbugreportneedsmore.info/).
102
+
103
+ ## Thanks and Credits
104
+
105
+ FriendlyId was originally created by Norman Clarke and Adrian Mugnolo, with
106
+ significant help early in its life by Emilio Tagua. I'm deeply gratful for the
107
+ generous contributions over the years from [many
108
+ volunteers](https://github.com/norman/friendly_id/contributors).
109
+
110
+ Part of the inspiration to rework FriendlyId came from Darcy Laycock's library
111
+ [Slugged](https://github.com/Sutto/slugged), which he was inspired to create
112
+ because of frustrations he experienced while using FriendlyId 3.x. Seeing a
113
+ smart programmer become frustrated with my code was enough of a kick in the
114
+ butt to make me want to significantly improve this library.
115
+
116
+ Many thanks to him for providing valid, real criticism while still being a cool
117
+ about it. I definitely recommend you check out his library if for some reason
118
+ FriendlyId doesn't do it for you.
119
+
120
+ Thanks also to Loren Segal and Nick Plante for YARD and the
121
+ [rubydoc.info](http://rubydoc.info/) website which FriendlyId uses for
122
+ documentation.
123
+
124
+ Lastly, FriendlyId uses [Travis](http://travis-ci.org/) for continuous
125
+ integration. It's an excellent, free service created by a whole bunch of [good
126
+ people](https://github.com/travis-ci) - if you're not already using it, you
127
+ should be!
128
+
129
+ ## License
130
+
131
+ Copyright (c) 2008-2012 Norman Clarke and contributors, released under the MIT
132
+ license.
133
+
134
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
135
+ this software and associated documentation files (the "Software"), to deal in
136
+ the Software without restriction, including without limitation the rights to
137
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
138
+ of the Software, and to permit persons to whom the Software is furnished to do
139
+ so, subject to the following conditions:
140
+
141
+ The above copyright notice and this permission notice shall be included in all
142
+ copies or substantial portions of the Software.
143
+
144
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
145
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
146
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
147
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
148
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
149
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
150
+ SOFTWARE.
@@ -0,0 +1,108 @@
1
+ require "rubygems"
2
+ require "rake/testtask"
3
+
4
+ task :default => :test
5
+
6
+ task :load_path do
7
+ %w(lib test).each do |path|
8
+ $LOAD_PATH.unshift(File.expand_path("../#{path}", __FILE__))
9
+ end
10
+ end
11
+
12
+ Rake::TestTask.new do |t|
13
+ t.libs << "test"
14
+ t.test_files = FileList['test/*_test.rb']
15
+ t.verbose = true
16
+ end
17
+
18
+ task :clean do
19
+ %x{rm -rf *.gem doc pkg coverage}
20
+ %x{rm -f `find . -name '*.rbc'`}
21
+ end
22
+
23
+ task :gem do
24
+ %x{gem build mil_friendly_id.gemspec}
25
+ end
26
+
27
+ task :yard => :guide do
28
+ puts %x{bundle exec yard}
29
+ end
30
+
31
+ task :bench => :load_path do
32
+ require File.expand_path("../bench", __FILE__)
33
+ end
34
+
35
+ task :guide do
36
+ def read_comments(path)
37
+ path = File.expand_path("../#{path}", __FILE__)
38
+ match = File.read(path).match(/\n=begin(.*)\n=end/m)[1].to_s
39
+ match.split("\n").reject {|x| x =~ /^@/}.join("\n")
40
+ end
41
+
42
+ buffer = []
43
+
44
+ buffer << read_comments("lib/friendly_id.rb")
45
+ buffer << read_comments("lib/friendly_id/base.rb")
46
+ buffer << read_comments("lib/friendly_id/slugged.rb")
47
+ buffer << read_comments("lib/friendly_id/history.rb")
48
+ buffer << read_comments("lib/friendly_id/scoped.rb")
49
+ buffer << read_comments("lib/friendly_id/simple_i18n.rb")
50
+ buffer << read_comments("lib/friendly_id/globalize.rb")
51
+ buffer << read_comments("lib/friendly_id/reserved.rb")
52
+
53
+ File.open("Guide.rdoc", "w") do |file|
54
+ file.write("#encoding: utf-8\n")
55
+ file.write(buffer.join("\n"))
56
+ end
57
+ end
58
+
59
+ namespace :test do
60
+
61
+ desc "Run each test class in a separate process"
62
+ task :isolated do
63
+ dir = File.expand_path("../test", __FILE__)
64
+ Dir["#{dir}/*_test.rb"].each do |test|
65
+ puts "Running #{test}:"
66
+ puts %x{ruby -Ilib -Itest #{test}}
67
+ end
68
+ end
69
+ end
70
+
71
+ namespace :db do
72
+
73
+ desc "Create the database"
74
+ task :create => :load_path do
75
+ require "helper"
76
+ driver = FriendlyId::Test::Database.driver
77
+ config = FriendlyId::Test::Database.config[driver]
78
+ commands = {
79
+ "mysql" => "mysql -u #{config['username']} -e 'create database #{config["database"]};' >/dev/null",
80
+ "postgres" => "psql -c 'create database #{config['database']};' -U #{config['username']} >/dev/null"
81
+ }
82
+ %x{#{commands[driver] || true}}
83
+ end
84
+
85
+ desc "Create the database"
86
+ task :drop => :load_path do
87
+ require "helper"
88
+ driver = FriendlyId::Test::Database.driver
89
+ config = FriendlyId::Test::Database.config[driver]
90
+ commands = {
91
+ "mysql" => "mysql -u #{config['username']} -e 'drop database #{config["database"]};' >/dev/null",
92
+ "postgres" => "psql -c 'drop database #{config['database']};' -U #{config['username']} >/dev/null"
93
+ }
94
+ %x{#{commands[driver] || true}}
95
+ end
96
+
97
+ desc "Set up the database schema"
98
+ task :up => :load_path do
99
+ require "helper"
100
+ FriendlyId::Test::Schema.up
101
+ end
102
+
103
+ desc "Drop and recreate the database schema"
104
+ task :reset => [:drop, :create]
105
+
106
+ end
107
+
108
+ task :doc => :yard
@@ -0,0 +1,95 @@
1
+ # What's New in FriendlyId 4?
2
+
3
+ ## Back to basics
4
+
5
+ FriendlyId is mostly a different codebase from FriendlyId 3. However, this isn't
6
+ the "big rewrite," it's the "small rewrite:"
7
+
8
+ Adding new features with each release is not sustainable. This release *removes*
9
+ features, but makes it possible to add them back as addons. We can also remove
10
+ some complexity by relying on the better default functionality provided by newer
11
+ versions of Active Support and Active Record.
12
+
13
+ Here's what's changed:
14
+
15
+ ## New configuration and setup
16
+
17
+ FriendlyId is no longer added to Active Record by default, you must explicitly
18
+ add it to each model you want to use it in. The method and options have also
19
+ changed:
20
+
21
+ # FriendlyId 3
22
+ class Post < ActiveRecord::Base
23
+ has_friendly_id :title, :use_slugs => true
24
+ end
25
+
26
+ # FriendlyId 4
27
+ class Post < ActiveRecord::Base
28
+ extend FriendlyId
29
+ friendly_id :title, :use => :slugged
30
+ end
31
+
32
+ It also adds a new "defaults" method for configuring all models:
33
+
34
+ FriendlyId.defaults do |config|
35
+ config.use :slugged, :reserved
36
+ config.base = :name
37
+ end
38
+
39
+ ## Active Record 3+ only
40
+
41
+ For 2.3 support, you can use FriendlyId 3.x, which will continue to be
42
+ maintained until people don't want it any more.
43
+
44
+ ## In-table slugs
45
+
46
+ FriendlyId no longer creates a separate slugs table - it just stores the
47
+ generated slug value in the model table, which is simpler, faster and what most
48
+ want by default. Keeping slug history in a separate table is an
49
+ {FriendlyId::History optional add-on} for FriendlyId 4.
50
+
51
+ ## No more multiple finds
52
+
53
+ Person.find "joe-schmoe" # Supported
54
+ Person.find ["joe-schmoe", "john-doe"] # No longer supported
55
+
56
+ If you want find by more than one friendly id, build your own query:
57
+
58
+ Person.where(:slug => ["joe-schmoe", "john-doe"])
59
+
60
+ This lets us do *far* less monkeypatching in Active Record. How much less?
61
+ FriendlyId overrides the base find with a mere 2 lines of code, and otherwise
62
+ changes nothing else. This means more stability and less breakage between Rails
63
+ updates.
64
+
65
+ ## No more finder status
66
+
67
+ FriendlyId 3 offered finder statuses to help you determine when an outdated
68
+ or non-friendly id was used to find the record, so that you could decide whether
69
+ to permanently redirect to the canonical URL. However, there's a simpler way to
70
+ do that, so this feature has been removed:
71
+
72
+ if request.path != person_path(@person)
73
+ return redirect_to @person, :status => :moved_permanently
74
+ end
75
+
76
+ ## Bye-bye Babosa
77
+
78
+ [Babosa](http://github.com/norman/babosa) is FriendlyId 3's slugging library.
79
+
80
+ FriendlyId 4 doesn't use it by default because the most important pieces of it
81
+ were already accepted into Active Support 3.
82
+
83
+ However, Babosa is still useful, for example, for idiomatically transliterating
84
+ Cyrillic ([or other
85
+ language](https://github.com/norman/babosa/tree/master/lib/babosa/transliterator))
86
+ strings to ASCII. It's very easy to include - just override
87
+ `#normalize_friendly_id` in your model:
88
+
89
+ class MyModel < ActiveRecord::Base
90
+ ...
91
+
92
+ def normalize_friendly_id(text)
93
+ text.to_slug.normalize! :transliterations => :russian
94
+ end
95
+ end
@@ -0,0 +1,63 @@
1
+ require File.expand_path("../test/helper", __FILE__)
2
+ require "ffaker"
3
+
4
+ N = 1000
5
+
6
+ def transaction
7
+ ActiveRecord::Base.transaction { yield ; raise ActiveRecord::Rollback }
8
+ end
9
+
10
+ class Array
11
+ def rand
12
+ self[Kernel.rand(length)]
13
+ end
14
+ end
15
+
16
+ Book = Class.new ActiveRecord::Base
17
+
18
+ class Journalist < ActiveRecord::Base
19
+ extend FriendlyId
20
+ friendly_id :name, :use => :slugged
21
+ end
22
+
23
+ class Manual < ActiveRecord::Base
24
+ extend FriendlyId
25
+ friendly_id :name, :use => :history
26
+ end
27
+
28
+ BOOKS = []
29
+ JOURNALISTS = []
30
+ MANUALS = []
31
+
32
+ 100.times do
33
+ name = Faker::Name.name
34
+ BOOKS << (Book.create! :name => name).id
35
+ JOURNALISTS << (Journalist.create! :name => name).friendly_id
36
+ MANUALS << (Manual.create! :name => name).friendly_id
37
+ end
38
+
39
+ ActiveRecord::Base.connection.execute "UPDATE manuals SET slug = NULL"
40
+
41
+ Benchmark.bmbm do |x|
42
+ x.report 'find (without FriendlyId)' do
43
+ N.times {Book.find BOOKS.rand}
44
+ end
45
+ x.report 'find (in-table slug)' do
46
+ N.times {Journalist.find JOURNALISTS.rand}
47
+ end
48
+ x.report 'find (external slug)' do
49
+ N.times {Manual.find MANUALS.rand}
50
+ end
51
+
52
+ x.report 'insert (without FriendlyId)' do
53
+ N.times {transaction {Book.create :name => Faker::Name.name}}
54
+ end
55
+
56
+ x.report 'insert (in-table-slug)' do
57
+ N.times {transaction {Journalist.create :name => Faker::Name.name}}
58
+ end
59
+
60
+ x.report 'insert (external slug)' do
61
+ N.times {transaction {Manual.create :name => Faker::Name.name}}
62
+ end
63
+ end
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ require "friendly_id"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "friendly_id"
8
+ s.version = FriendlyId::VERSION
9
+ s.authors = ["Norman Clarke", "Philip Arndt"]
10
+ s.email = ["norman@njclarke.com", "parndt@gmail.com"]
11
+ s.homepage = "http://github.com/norman/friendly_id"
12
+ s.summary = "A comprehensive slugging and pretty-URL plugin."
13
+ s.rubyforge_project = "friendly_id"
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test}/*`.split("\n")
16
+ s.require_paths = ["lib"]
17
+
18
+ s.add_development_dependency "railties", "~> 3.2.0"
19
+ s.add_development_dependency "activerecord", "~> 3.2.0"
20
+ s.add_development_dependency "minitest", "3.2.0"
21
+ s.add_development_dependency "mocha"
22
+ s.add_development_dependency "maruku"
23
+ s.add_development_dependency "yard"
24
+ s.add_development_dependency "i18n"
25
+ s.add_development_dependency "ffaker"
26
+ s.add_development_dependency "simplecov"
27
+ s.add_development_dependency "globalize3"
28
+
29
+ s.description = <<-EOM
30
+ FriendlyId is the "Swiss Army bulldozer" of slugging and permalink plugins for
31
+ Ruby on Rails. It allows you to create pretty URLs and work with human-friendly
32
+ strings as if they were numeric ids for Active Record models.
33
+ EOM
34
+
35
+ s.post_install_message = <<-EOM
36
+ NOTE: FriendlyId 4.x breaks compatibility with 3.x. If you're upgrading
37
+ from 3.x, please see this document:
38
+
39
+ http://rubydoc.info/github/norman/friendly_id/master/file/WhatsNew.md
40
+
41
+ EOM
42
+
43
+ end