public
Rubygem
Description: FriendlyId is the “Swiss Army bulldozer” of slugging and permalink plugins for ActiveRecord. It allows you to create pretty URL’s and work with human-friendly strings as if they were numeric ids for ActiveRecord models.
Homepage: http://friendly-id.rubyforge.org
Clone URL: git://github.com/norman/friendly_id.git
Added support for namespaced models in Rakefile. (David Ramalho)
norman (author)
Mon Jul 14 14:12:26 -0700 2008
commit  ed5e32b916c7b7a1b3ac2ba2c7603e2908f1640c
tree    d5f076190d525a1280a8eac3d64dd468f8154bb1
parent  38f23b8d6507cdd5892fece9787953a5093868d6
...
 
 
 
 
1
2
3
...
1
2
3
4
5
6
7
0
@@ -1,3 +1,7 @@
0
+July 14 2008
0
+
0
+* Added support for namespaced models in Rakefile. (David Ramalho)
0
+
0
 June 23 2008
0
 
0
 * Cached most recent slug to improve performance (Emilio Tagua).
...
2
3
4
5
6
7
 
 
8
9
 
10
11
12
13
 
14
15
16
17
18
19
20
21
22
 
 
23
24
 
25
26
27
...
37
38
39
 
 
 
 
 
 
 
 
40
41
...
2
3
4
 
 
 
5
6
7
 
8
9
10
11
 
12
13
14
15
16
17
18
 
 
 
19
20
21
 
22
23
24
25
...
35
36
37
38
39
40
41
42
43
44
45
46
47
0
@@ -2,26 +2,24 @@ namespace :friendly_id do
0
   desc "Make slugs for a model."
0
   task :make_slugs => :environment do
0
     raise 'USAGE: rake friendly_id:make_slugs MODEL=MyModelName' if ENV["MODEL"].nil?
0
-    klass = Object.const_get(ENV["MODEL"])
0
-    if !klass.friendly_id_options[:use_slug]
0
-      raise "Class \"#{klass.to_s}\" doesn't appear to be using slugs"
0
+    if !sluggable_class.friendly_id_options[:use_slug]
0
+      raise "Class \"#{sluggable_class.to_s}\" doesn't appear to be using slugs"
0
     end
0
-    records = klass.find(:all, :include => :slugs, :conditions => "slugs.id IS NULL")
0
+    records = sluggable_class.find(:all, :include => :slugs, :conditions => "slugs.id IS NULL")
0
     records.each do |r|
0
       r.set_slug
0
       r.save!
0
-      puts "#{klass.to_s}(#{r.id}) friendly_id set to \"#{r.slug.name}\""
0
+      puts "#{sluggable_class.to_s}(#{r.id}) friendly_id set to \"#{r.slug.name}\""
0
     end
0
   end
0
 
0
   desc "Regenereate slugs for a model."
0
   task :redo_slugs => :environment do
0
     raise 'USAGE: rake friendly_id:redo_slugs MODEL=MyModelName' if ENV["MODEL"].nil?
0
-    klass = Object.const_get(ENV["MODEL"])
0
-    if !klass.friendly_id_options[:use_slug]
0
-      raise "Class \"#{klass.to_s}\" doesn't appear to be using slugs"
0
+    if !sluggable_class.friendly_id_options[:use_slug]
0
+      raise "Class \"#{sluggable_class.to_s}\" doesn't appear to be using slugs"
0
     end
0
-    Slug.destroy_all(["sluggable_type = ?", klass.to_s])
0
+    Slug.destroy_all(["sluggable_type = ?", sluggable_class.to_s])
0
     Rake::Task["friendly_id:make_slugs"].invoke
0
   end
0
   
0
@@ -37,4 +35,12 @@ namespace :friendly_id do
0
       s.destroy if !s.is_most_recent?
0
     end
0
   end
0
+end
0
+
0
+def sluggable_class
0
+  if (ENV["MODEL"].split('::').size > 1) 
0
+    ENV["MODEL"].split('::').inject(Kernel) {|scope, const_name| scope.const_get(const_name)} 
0
+  else 
0
+    Object.const_get(ENV["MODEL"])
0
+  end
0
 end
0
\ No newline at end of file

Comments