rakutenusa-friendly_id 2.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,108 @@
1
+ == 2.0.4 2009-02-12
2
+
3
+ * 1 major enhancment:
4
+ * You can now pass in your own custom slug generation blocks while setting up friendly_id.
5
+
6
+ == 2.0.3 2009-02-11
7
+
8
+ * 1 minor enhancment:
9
+ * Fixed to_param returning an empty string for non-slugged models with a null friendly_id.
10
+
11
+ == 2.0.2 2009-02-09
12
+
13
+ * 2 major enhancements:
14
+ * Made FriendlyId depend only on ActiveRecord. It should now be possible to
15
+ use FriendlyId with Camping or any other codebase that uses AR.
16
+ * Overhauled creaky testing setup and switched to Shoulda.
17
+
18
+ * 1 minor enhancment:
19
+ * Made reserved words work for non-slugged models.
20
+
21
+ == 2.0.1 2009-01-19
22
+
23
+ * 1 minor enhancements:
24
+ * Fix infinite redirect bug when using .has_better_id? in your controllers (Sean Abrahams)
25
+
26
+
27
+ == 2.0.0 2009-01-03
28
+
29
+ * 5 major enhancements:
30
+ * Support for scoped slugs (Norman Clarke)
31
+ * Support for UTF-8 friendly_ids (Norman Clarke)
32
+ * Can now be installed via Ruby Gems, or as a Rails plugin (Norman Clarke)
33
+ * Improved handling of non-unique slugs (Norman Clarke and Adrian Mugnolo)
34
+ * 2 minor enhancements:
35
+ * Shoulda macro (Josh Nichols)
36
+ * Various small bugfixes, cleanups and refactorings
37
+
38
+ == 2008-12-01
39
+
40
+ * Fixed bug that may return invalid records having similar id/names and using MySQL. (Emilio Tagua)
41
+ * Fixed slug generation to increment only numeric extension without modifying the name on duplicated slugs. (Emilio Tagua)
42
+
43
+ == 2008-10-31
44
+
45
+ * Fixed compatibility with Rails 2.0.x. (Norman Clarke)
46
+ * friendly_id::make_slugs update records in chunks of 1000 to avoid running out of memory with large datasets. (Tim Kadom)
47
+ * Fixed logic error with slug name collisions. Thanks to Tim Kadom for reporting this bug.
48
+
49
+ == 2008-10-22
50
+
51
+ * Reverted use of UTF8Handler - was causing errors for some people (Bence Nagy)
52
+ * Corrected find in case if a friendly_id begins with number (Bence Nagy)
53
+ * Added ability to reserve words from slugs (Adam Cigánek)
54
+
55
+ == 2008-10-09
56
+
57
+ * Moved "require"" for iconv to init.rb (Florian Aßmann)
58
+ * Removed "require" for Unicode, use Rails' handler instead (Florian Aßmann)
59
+ * Replaced some magic numbers with constants (Florian Aßmann)
60
+ * Don't overwrite find, alias_method_chain find_one and find_some instead (Florian Aßmann)
61
+ * Slugs behave more like ids now (Florian Aßmann)
62
+ * Can find by mixture of ids and slugs (Florian Aßmann)
63
+ * Reformatted code and comments (Florian Aßmann)
64
+ * Added support for Edge Rails' Inflector::parameterize (Norman Clarke)
65
+
66
+ == 2008-08-25
67
+
68
+ * Moved strip_diacritics into Slug for easier reuse/better organization.
69
+ * Put class methods inside class << self block. (Norman Clarke)
70
+
71
+ * Small change to allow friendly_id to work better with STI. (David Ramalho)
72
+
73
+ == 2008-07-14
74
+
75
+ * Improved slug generation for friendly id's with apostrophes. (alistairholt)
76
+ * Added support for namespaced models in Rakefile. (David Ramalho)
77
+
78
+ == 2008-06-23
79
+
80
+ * Cached most recent slug to improve performance (Emilio Tagua).
81
+
82
+ == 2008-06-10
83
+
84
+ * Added ability to find friendly_ids by array (Emilio Tagua)
85
+
86
+ == 2008-05-15
87
+
88
+ * Made friendly_id raise an error if slug method returns a blank value.
89
+
90
+ == 2008-05-12
91
+
92
+ * Added experimental Github gemspec.
93
+
94
+ == 2008-04-18
95
+
96
+ * Improved slug name collision avoidance.
97
+
98
+ == 2008-03-13
99
+
100
+ * Added :dependent => :destroy to slug relation, as suggested by Emilio Tagua.
101
+ * Fixed error when renaming a slugged item back to a previously used name.
102
+ * Incorporated documentation changes suggested by Jesse Crouch and Chris Nolan.
103
+
104
+ == 2008-02-07
105
+
106
+ * Applied patches from blog commenter "suntzu" to fix problem with model values were being overwritten.
107
+ * Applied patch from Dan Blue to make friendly_id no longer ignore options on ActiveRecordBase#find.
108
+ * Added call to options.assert_valid_keys in has_friendly_id. Thanks to W. Andrew Loe III for pointing out that this was missing.
data/Manifest.txt ADDED
@@ -0,0 +1,37 @@
1
+ History.txt
2
+ MIT-LICENSE
3
+ Manifest.txt
4
+ README.rdoc
5
+ Rakefile
6
+ config/website.yml
7
+ friendly_id.gemspec
8
+ generators/friendly_id/friendly_id_generator.rb
9
+ generators/friendly_id/templates/create_slugs.rb
10
+ generators/friendly_id_20_upgrade/friendly_id_20_upgrade_generator.rb
11
+ generators/friendly_id_20_upgrade/templates/upgrade_friendly_id_to_20.rb
12
+ init.rb
13
+ lib/friendly_id.rb
14
+ lib/friendly_id/helpers.rb
15
+ lib/friendly_id/non_sluggable_class_methods.rb
16
+ lib/friendly_id/non_sluggable_instance_methods.rb
17
+ lib/friendly_id/slug.rb
18
+ lib/friendly_id/sluggable_class_methods.rb
19
+ lib/friendly_id/sluggable_instance_methods.rb
20
+ lib/friendly_id/version.rb
21
+ lib/tasks/friendly_id.rake
22
+ lib/tasks/friendly_id.rb
23
+ test/custom_slug_normalizer_test.rb
24
+ test/models/book.rb
25
+ test/models/country.rb
26
+ test/models/novel.rb
27
+ test/models/person.rb
28
+ test/models/post.rb
29
+ test/models/thing.rb
30
+ test/models/user.rb
31
+ test/non_slugged_test.rb
32
+ test/schema.rb
33
+ test/scoped_model_test.rb
34
+ test/slug_test.rb
35
+ test/slugged_model_test.rb
36
+ test/sti_test.rb
37
+ test/test_helper.rb
data/README.rdoc ADDED
@@ -0,0 +1,347 @@
1
+ = FriendlyId
2
+
3
+ FriendlyId is the "Swiss Army bulldozer" of slugging and permalink plugins for
4
+ Ruby on Rails. It allows you to create pretty URL's and work with
5
+ human-friendly strings as if they were numeric ids for ActiveRecord models.
6
+
7
+ Using FriendlyId, it's easy to make your application use URL's like:
8
+
9
+ http://example.com/states/washington
10
+
11
+ instead of:
12
+
13
+ http://example.com/states/4323454
14
+
15
+ Want to find out more? Read on. The {most recent version of the FriendlyId
16
+ RDocs}[http://friendly-id.rubyforge.org] can always be found on
17
+ Rubyforge[http://www.rubyforge.org].
18
+
19
+ === Why?
20
+
21
+ * Text-based id's look better
22
+ * They make URL's easier to remember.
23
+ * They give no hint about the number of records in your database.
24
+ * They are better for search engine optimization.
25
+
26
+ === But...
27
+
28
+ * They can change, breaking your URL's and your SEO.
29
+ * It can be tricky to ensure they're always unique.
30
+ * They can become a pain to manage in large Rails applications.
31
+ * They can conflict with your application's namespace.
32
+
33
+ FriendlyId tries to offer you the all the advantages, and avoid or soften the
34
+ potential impact of the disadvantages.
35
+
36
+ == Typical Uses
37
+
38
+ === User names ("non-slugged" models)
39
+
40
+ Usually users have unique user names stored in a column with a unique
41
+ constraint or index. In this case, all you need to do is add this to your
42
+ model:
43
+
44
+ has_friendly_id :login
45
+
46
+ and you can then write code like this:
47
+
48
+ @member = Member.find("joe") # the old Member.find(1) still works, too.
49
+ @member.to_param # returns "joe"
50
+ redirect_to @member # The URL would be /members/joe
51
+
52
+ === Blog posts ("slugged" models)
53
+
54
+ Blog posts generally have titles which are distinctive but not necessarily
55
+ unique. In this and similar cases, FriendlyId provides a Slug model separate
56
+ from your Post model. The Slug model handles duplicate friendly_ids, as well
57
+ as versioning.
58
+
59
+ Your model code would look something like this:
60
+
61
+ has_friendly_id :title, :use_slug => true
62
+
63
+ and you can then write code like this:
64
+
65
+ @post = Post.find("new-version-released") # Post.find(1) still works, too
66
+ @post.to_param # returns "new-version-released"
67
+ redirect_to @post # The URL would be /posts/new-version-released
68
+
69
+ Now in your controllers, if you want to prevent people from accessing your
70
+ models by numeric id, you can detect whether they were found by the
71
+ friendly_id:
72
+
73
+ @post = Post.find(params[:id])
74
+ raise "some error" if !@post.found_using_friendly_id?
75
+
76
+ or, you can 301 redirect if the model was found by the numeric id if you don't
77
+ care about numeric access, but want the SEO value of the friendly_id:
78
+
79
+ @post = Post.find(params[:id])
80
+ redirect_to @post, :status => 301 if @post.has_better_id?
81
+
82
+ The "has_better_id?" method returns true if the model was found with the
83
+ numeric id, or with an outdated slug.
84
+
85
+ == Extra Features
86
+
87
+ === Slug Versioning
88
+
89
+ FriendlyId will record changes to slugs so that you can tell when the model is
90
+ found with an older slug, or by the numeric id. This can be useful if you want
91
+ to do a 301 redirect to your updated URL.
92
+
93
+ class PostsController < ApplicationController
94
+
95
+ before_filter ensure_current_post_url, :only => :show
96
+
97
+ ...
98
+
99
+ def ensure_current_post_url
100
+ redirect_to @post, :status => :moved_permanently if @post.has_better_id?
101
+ end
102
+
103
+ end
104
+
105
+ This is particularly useful when implementing FrindlyId on an existing
106
+ website that already has many URL's with the old numeric id listed on search
107
+ engines. When the search engine spiders crawl your site, they will
108
+ eventually pick up the new, more SEO-friendly URL's.
109
+
110
+ === Non-unique Slug Names
111
+
112
+ FriendlyId will append a arbitrary number to the end of the id to keep it
113
+ unique if necessary:
114
+
115
+ /posts/new-version-released
116
+ /posts/new-version-released--2
117
+ /posts/new-version-released--3
118
+ ...
119
+ etc.
120
+
121
+ Note that the number is preceeded by two dashes to distinguish it from the
122
+ rest of the slug. This is important to enable having slugs like:
123
+
124
+ /cars/peugeot-206
125
+ /cars/peugeot-206--2
126
+
127
+ === Reserved Names
128
+
129
+ You can mark off some strings as reserved so that, for example, you don't end
130
+ up with this problem:
131
+
132
+ /users/joe-schmoe # A user chose "joe schmoe" as his user name - no worries.
133
+ /users/new # A user chose "new" as his user name, and now no one can sign up.
134
+
135
+ Here's how to do it:
136
+
137
+ class Restaurant < ActiveRecord::Base
138
+ belongs_to :city
139
+ has_friendly_id :name, :use_slug => true, :reserved => ["my", "values"]
140
+ end
141
+
142
+ As of FriendlyId version 2.0.2, "new" and "index" are reseved by default. When
143
+ you attempt to store a reserved value, FriendlyId raises a
144
+ FriendlyId::SlugGenerationError.
145
+
146
+
147
+ === Scoped Slugs
148
+
149
+ FriendlyId can generate unique slugs within a given scope. For example:
150
+
151
+ class Restaurant < ActiveRecord::Base
152
+ belongs_to :city
153
+ has_friendly_id :name, :use_slug => true, :scope => :city
154
+ end
155
+
156
+ class City < ActiveRecord::Base
157
+ has_many :restaurants
158
+ has_friendly_id :name, :use_slug => true
159
+ end
160
+
161
+ http://example.org/cities/seattle/restaurants/joes-diner
162
+ http://example.org/cities/chicago/restaurants/joes-diner
163
+
164
+ Restaurant.find("joes-diner", :scope => "seattle") # returns 1 record
165
+ Restaurant.find("joes-diner", :scope => "chicago") # returns 1 record
166
+ Restaurant.find("joes-diner") # returns both records
167
+
168
+
169
+ The value for the :scope key in your model can be a custom method you define,
170
+ or the name of a relation. If it's the name of a relation, then the scope's
171
+ text value will be the result of calling <code>to_param</code> on the related
172
+ model record. In the example above, the city model also uses FriendlyId and so
173
+ its <code>to_param</code> method returns its friendly_id: chicago or seattle.
174
+
175
+ This feature is new in FriendlyId 2 and should be considered of experimental
176
+ quality. Please don't use this for code that needs to run on the Space
177
+ Shuttle.
178
+
179
+ === Text Normalization
180
+
181
+ FriendlyId's slugging can strip diacritics from Western European characters,
182
+ so that you can have ASCII-only URL's; for example, conveting "ñøîéçü" to
183
+ "noiecu."
184
+
185
+ has_friendly_id :title, :use_slug => true, :strip_diacritics => true
186
+
187
+ If you are not using slugs, you'll have to do this manually for whatever value
188
+ you're using as the friendly_id.
189
+
190
+ === Diacritic-sensitive normalization
191
+
192
+ FriendlyId can also normalize slug text while preserving accented characters, if
193
+ you prefer to leave them in your URL's:
194
+
195
+ has_friendly_id :title, :use_slug => true
196
+ ...
197
+ @post = Post.create(:title => "¡Feliz Año!")
198
+ @post.friendly_id # "feliz-año"
199
+
200
+ === Unicode URL's
201
+
202
+ FriendlyId can generate slugs in any language that can be written with
203
+ Unicode. It does its best to strip away punctuation regardless of the language
204
+ being used. Since the authors only speak English, Spanish, Portuguese and
205
+ German, this has not been extensively tested with anything like Chinese,
206
+ Russian, Greek, etc, but it "should work." If you're a speaker of a language
207
+ that uses a non-Roman writing system, your feedback would be most welcome.
208
+
209
+ has_friendly_id :title, :use_slug => true
210
+ ...
211
+ @post = Post.create(:title => "友好编号在中国")
212
+ @post.friendly_id # "友好编号在中国"
213
+ @post2 = Post.create(:title => "友好编号在中国")
214
+ @post2.friendly_id # "友好编号在中国--2"
215
+
216
+ === Custom Slug Generation
217
+
218
+ While FriendlyId's slug generation options work for most people, you may need
219
+ something else. As of version 2.0.4 you can pass in your own custom slug
220
+ generation block:
221
+
222
+ require 'stringex'
223
+ class Post < ActiveRecord::Base
224
+ has_friendly_id :title, :use_slug => true do |text|
225
+ # User stringex to generate the friendly_id rather than the baked-in methods
226
+ text.to_url
227
+ end
228
+ end
229
+
230
+ ...
231
+
232
+ @post = Post.create(:title => "tell your readers 你好")
233
+ @post.friendly_id # "tell-your-readers-ni-hao"
234
+
235
+ FriendlyId will still respect your settings for max length and reserved words,
236
+ but will use your block rather than the baked-in methods to normalize the
237
+ friendly_id text.
238
+
239
+ (As an aside, the stringex[http://github.com/rsl/stringex/tree/master] library
240
+ provides some very cool slugging functionality and is a great option for
241
+ apps using FriendlyId in either English or Chinese. Definitely check it out.)
242
+
243
+ == Getting it
244
+
245
+ FriendlyId is installed as a Ruby Gem:
246
+
247
+ gem install friendly_id
248
+
249
+ Alternatively, you can install it as a Rails plugin, though this is
250
+ discouraged:
251
+
252
+ ./script/plugin install git://github.com/norman/friendly_id.git
253
+
254
+ If you are installing as a plugin, make sure you have installed the unicode gem,
255
+ which FriendlyId depends on:
256
+
257
+ gem install unicode
258
+
259
+
260
+ == Setting it up
261
+
262
+ FriendlyId currently works with Rails 2.0.0 - 2.3.0. Here's how to set it up.
263
+
264
+ 1) Install the Gem:
265
+
266
+ sudo gem install friendly_id
267
+ cd my_app
268
+ script/generate friendly_id
269
+ rake db:migrate
270
+
271
+ 2) Load FriendlyId in your app:
272
+
273
+ # Rails 2.1 and higher; add this to the gem section of environment.rb:
274
+ config.gem "friendly_id"
275
+
276
+ # Rails 2.0; this goes at the bottom of environment.rb
277
+ require 'friendly_id'
278
+
279
+ 3) Add some code to your models:
280
+
281
+ class Post < ActiveRecord::Base
282
+ has_friendly_id :title, :use_slug => true
283
+ end
284
+
285
+ 4) If you are using slugs, you can use a Rake task to generate slugs for your
286
+ existing records:
287
+
288
+ friendly_id:make_slugs MODEL=MyModelName
289
+
290
+ If you eventually want to expire old slugs every so often, or perhaps every
291
+ day via cron, you can do:
292
+
293
+ rake:friendly_id:remove_old_slugs
294
+
295
+ The default is to remove dead slugs older than 45 days, but is configurable:
296
+
297
+ rake:friendly_id:remove_old_slugs MODEL=MyModelName DAYS=60
298
+
299
+ == Upgrading from an older version
300
+
301
+ If you installed an older version of FriendlyId and want to upgrade to 2.0.x,
302
+ follow these steps:
303
+
304
+ ==== Install the friendly_id Gem:
305
+
306
+ sudo gem install friendly_id
307
+
308
+ ==== Add FriendlyId to environment.rb:
309
+
310
+ ===== For Rails 2.1 and higher:
311
+
312
+ config.gem "friendly_id"
313
+
314
+ ===== For Rails 2.0:
315
+
316
+ Add this to the bottom of environment.rb:
317
+
318
+ require 'friendly_id'
319
+
320
+ ==== Remove the older version of FriendlyId:
321
+
322
+ git rm -rf vendor/plugins/friendly_id
323
+ svn delete vendor/plugins/friendly_id
324
+ # or whatever
325
+
326
+ ==== Generate the upgrade migration and run it
327
+
328
+ ./script generate friendly_id_20_upgrade
329
+ rake db:migrate
330
+
331
+ == Hacking FriendlyId:
332
+
333
+ FriendlyId is {hosted on Github}[git://github.com/norman/friendly_id.git], and
334
+ we love pull requests. :-)
335
+
336
+ == Bugs:
337
+
338
+ Please report them on Lighthouse[http://randomba.lighthouseapp.com/projects/14675-friendly_id].
339
+
340
+
341
+ == Credits:
342
+
343
+ FriendlyId was created by {Norman Clarke}[mailto:norman@randomba.org],
344
+ {Adrian Mugnolo}[mailto:adrian@randomba.org], and {Emilio Tagua}[mailto:miloops@gmail.com].
345
+
346
+ Copyright (c) 2008 Norman Clarke, Adrian Mugnolo and Emilio Tagua, released
347
+ under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1,47 @@
1
+ require 'lib/friendly_id/version'
2
+
3
+ desc "Run RCov"
4
+ task :rcov do
5
+ run_coverage Dir["test/**/*_test.rb"]
6
+ end
7
+
8
+ def run_coverage(files)
9
+ rm_f "coverage"
10
+ rm_f "coverage.data"
11
+ if files.length == 0
12
+ puts "No files were specified for testing"
13
+ return
14
+ end
15
+ files = files.join(" ")
16
+ if PLATFORM =~ /darwin/
17
+ exclude = '--exclude "gems/"'
18
+ else
19
+ exclude = '--exclude "rubygems"'
20
+ end
21
+ rcov = "rcov -Ilib:test --sort coverage --text-report #{exclude} --no-validator-links"
22
+ cmd = "#{rcov} #{files}"
23
+ puts cmd
24
+ sh cmd
25
+ end
26
+
27
+ desc 'Publish RDoc to RubyForge.'
28
+ task :publish_docs => [:clean, :docs] do
29
+ host = "compay@rubyforge.org"
30
+ remote_dir = "/var/www/gforge-projects/friendly-id"
31
+ local_dir = 'doc'
32
+ sh %{rsync -av --delete #{local_dir}/ #{host}:#{remote_dir}}
33
+ end
34
+
35
+ begin
36
+ require 'jeweler'
37
+ Jeweler::Tasks.new do |gemspec|
38
+ gemspec.name = "friendly_id"
39
+ gemspec.summary = "TODO"
40
+ gemspec.email = "dev@rakutenusa.com"
41
+ gemspec.homepage = "http://github.com/rakutenusa/friendly_id"
42
+ gemspec.description = "TODO"
43
+ gemspec.authors = ["RakutenUSA"]
44
+ end
45
+ rescue LoadError
46
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
47
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 0
3
+ :patch: 6
4
+ :major: 2
@@ -0,0 +1,12 @@
1
+ class FriendlyIdGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ unless options[:skip_migration]
5
+ m.migration_template(
6
+ 'create_slugs.rb', 'db/migrate', :migration_file_name => 'create_slugs'
7
+ )
8
+ m.file "/../../../lib/tasks/friendly_id.rake", "lib/tasks/friendly_id.rake"
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,18 @@
1
+ class CreateSlugs < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :slugs do |t|
4
+ t.string :name
5
+ t.integer :sluggable_id
6
+ t.integer :sequence, :null => false, :default => 1
7
+ t.string :sluggable_type, :limit => 40
8
+ t.string :scope, :limit => 40
9
+ t.datetime :created_at
10
+ end
11
+ add_index :slugs, [:name, :sluggable_type, :scope, :sequence], :unique => true
12
+ add_index :slugs, :sluggable_id
13
+ end
14
+
15
+ def self.down
16
+ drop_table :slugs
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ class FriendlyId20UpgradeGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ unless options[:skip_migration]
5
+ m.migration_template(
6
+ 'upgrade_friendly_id_to_20.rb', 'db/migrate', :migration_file_name => 'upgrade_friendly_id_to_20'
7
+ )
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ class UpgradeFriendlyIdTo20 < ActiveRecord::Migration
2
+
3
+ def self.up
4
+ remove_column :slugs, :updated_at
5
+ remove_index :slugs, :column => [:name, :sluggable_type]
6
+ add_column :slugs, :sequence, :integer, :null => false, :default => 1
7
+ add_column :slugs, :scope, :string, :limit => 40
8
+ add_index :slugs, [:name, :sluggable_type, :scope, :sequence], :unique => true, :name => "index_slugs_on_n_s_s_and_s"
9
+ end
10
+
11
+ def self.down
12
+ remove_index :slugs, :name => "index_slugs_on_n_s_s_and_s"
13
+ remove_column :slugs, :scope
14
+ remove_column :slugs, :sequence
15
+ add_column :slugs, :updated_at, :datetime
16
+ add_index :slugs, [:name, :sluggable_type], :unique => true
17
+ end
18
+
19
+ end
@@ -0,0 +1,13 @@
1
+ module FriendlyId
2
+
3
+ module Helpers
4
+ # Calculate expected result size for find_some_with_friendly (taken from
5
+ # active_record/base.rb)
6
+ def expected_size(ids_and_names, options) #:nodoc:#
7
+ size = options[:offset] ? ids_and_names.size - options[:offset] : ids_and_names.size
8
+ size = options[:limit] if options[:limit] && size > options[:limit]
9
+ size
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,40 @@
1
+ module FriendlyId::NonSluggableClassMethods
2
+
3
+ include FriendlyId::Helpers
4
+
5
+ def self.extended(base) #:nodoc:#
6
+ class << base
7
+ alias_method_chain :find_one, :friendly
8
+ alias_method_chain :find_some, :friendly
9
+ end
10
+ end
11
+
12
+ protected
13
+
14
+ def find_one_with_friendly(id, options) #:nodoc:#
15
+ if id.is_a?(String) && result = send("find_by_#{ friendly_id_options[:column] }", id, options)
16
+ result.send(:found_using_friendly_id=, true)
17
+ else
18
+ result = find_one_without_friendly id, options
19
+ end
20
+ result
21
+ end
22
+
23
+ def find_some_with_friendly(ids_and_names, options) #:nodoc:#
24
+
25
+ results = with_scope :find => options do
26
+ find :all, :conditions => ["#{quoted_table_name}.#{primary_key} IN (?) OR #{friendly_id_options[:column].to_s} IN (?)",
27
+ ids_and_names, ids_and_names]
28
+ end
29
+
30
+ expected = expected_size(ids_and_names, options)
31
+ if results.size != expected
32
+ raise ActiveRecord::RecordNotFound, "Couldn't find all #{ name.pluralize } with IDs (#{ ids_and_names * ', ' }) AND #{ sanitize_sql options[:conditions] } (found #{ results.size } results, but was looking for #{ expected })"
33
+ end
34
+
35
+ results.each {|r| r.send(:found_using_friendly_id=, true) if ids_and_names.include?(r.friendly_id)}
36
+
37
+ results
38
+
39
+ end
40
+ end