public
Fork of henrik/permalink_fu
Description: ActiveRecord plugin for automatically converting fields to permalinks.
Clone URL: git://github.com/dstrelau/permalink_fu.git
Allow additional options to find_by_permalink (and ! variant).

We simply delegate to the find_by_* of whatever your attribute is actually 
called ('permalink' by default or your :as => value)
dstrelau (author)
Mon Jul 21 19:29:35 -0700 2008
commit  b4a01c4e35a747bab59795eb5542e4d04cdf6b10
tree    e3a23bee05aa27b64fe2fef2a59cb3fc639b1428
parent  589de2f3f492080ef6baa379c942649e6bd50cf9
...
65
66
67
68
69
 
 
 
 
 
 
 
 
70
71
72
73
 
 
74
75
76
...
65
66
67
 
 
68
69
70
71
72
73
74
75
76
77
 
 
78
79
80
81
82
0
@@ -65,12 +65,18 @@ module PermalinkFu
0
   end
0
   
0
   module PermalinkFinders
0
- def find_by_permalink(value)
0
- find(:first, :conditions => { permalink_field => value })
0
+ def self.extended(base)
0
+ unless base.permalink_field == 'permalink'
0
+ (class << base; self; end).instance_eval do
0
+ define_method(:find_by_permalink) do |value, *args|
0
+ send(:"find_by_#{base.permalink_field}", value, *args)
0
+ end
0
+ end
0
+ end
0
     end
0
     
0
- def find_by_permalink!(value)
0
- find_by_permalink(value) ||
0
+ def find_by_permalink!(value, *args)
0
+ find_by_permalink(value, *args) ||
0
       raise(ActiveRecord::RecordNotFound, "Couldn't find #{name} with permalink #{value.inspect}")
0
     end
0
   end
...
65
66
67
 
68
69
70
...
114
115
116
 
117
118
119
...
249
250
251
 
 
 
 
252
253
254
 
 
 
 
255
256
257
...
65
66
67
68
69
70
71
...
115
116
117
118
119
120
121
...
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
0
@@ -65,6 +65,7 @@ class BaseModel
0
   end
0
   
0
   def self.find(*args)
0
+ return true if args.last.has_key?(:include)
0
     nil
0
   end
0
 
0
@@ -114,6 +115,7 @@ end
0
 class AsModel < BaseModel
0
   attr_reader :slug
0
   has_permalink :title, :as => :slug
0
+ def self.find_by_slug(value, *args); find(:conditions => {:slug => value}, *args); end
0
 end
0
 
0
 class OverrideParamModel < BaseModel
0
@@ -249,9 +251,17 @@ class PermalinkFuTest < Test::Unit::TestCase
0
     assert_nil AsModel.find_by_permalink("permalink")
0
   end
0
   
0
+ def test_should_merge_options_to_find_by_permalink
0
+ assert AsModel.find_by_permalink('permalink', :include => :result)
0
+ end
0
+
0
   def test_find_by_permalink!
0
     assert_raises(ActiveRecord::RecordNotFound) { AsModel.find_by_permalink!("permalink") }
0
   end
0
+
0
+ def test_should_merge_options_to_find_by_permalink!
0
+ assert AsModel.find_by_permalink!('permalink', :include => :result)
0
+ end
0
   
0
   def test_should_abide_by_if_proc_condition
0
     @m = IfProcConditionModel.new

Comments

    No one has commented yet.