#877 ✓resolved
Tomoaki Hayasaka

member_class is not preserved on paginate

Reported by Tomoaki Hayasaka | November 27th, 2010 @ 11:44 PM

1.3.0pre22

In "ActiveRecord::Relation#paginate", member_class is not preserved.
Moreover, "Array#paginate" doesn't propagate hobo metadata at all.

Because of this, if Relation or Array is passed to hobo_index, view may be corrupted.

Reproducing:

>> User.all.member_class.to_s
=> "User"
>> User.paginate(:page => 1).member_class.to_s
=> "User"
>> User.recent.paginate(:page => 1).member_class.to_s
=> "#<ActiveRecord::Relation:0xba0a838>"  # should be "User"
>> User.all.paginate(:page => 1).member_class.to_s
=> ""                                     # should be "User"

Fix:

index 1f8d1e2..29dfcbe 100644
--- a/hobo/lib/hobo/extensions/array.rb
+++ b/hobo/lib/hobo/extensions/array.rb
@@ -1,4 +1,7 @@
 # Add support for type metadata to arrays
+
+require 'will_paginate/array'
+
 class Array
 
   attr_accessor :member_class, :origin, :origin_attribute
@@ -12,4 +15,13 @@ class Array
     origin and origin_id = origin.try.typed_id and "#{origin_id}:#{origin_attribute}"
   end
 
+  def paginate_with_hobo_metadata(*args, &block)
+    collection = paginate_without_hobo_metadata(*args, &block)
+    collection.member_class     = member_class
+    collection.origin           = try.proxy_owner
+    collection.origin_attribute = try.proxy_reflection._?.name
+    collection
+  end
+  alias_method_chain :paginate, :hobo_metadata
+
 end
diff --git a/hobo/lib/hobo/model.rb b/hobo/lib/hobo/model.rb
index 08bebde..800e6b2 100644
--- a/hobo/lib/hobo/model.rb
+++ b/hobo/lib/hobo/model.rb
@@ -61,7 +61,7 @@ module Hobo
         WillPaginate::Finders::Base.class_eval do
           def paginate_with_hobo_metadata(*args, &block)
             collection = paginate_without_hobo_metadata(*args, &block)
-            collection.member_class     = self
+            collection.member_class     = self.is_a?(ActiveRecord::Relation) ? member_class : self
             collection.origin           = try.proxy_owner
             collection.origin_attribute = try.proxy_reflection._?.name
             collection
---

Similar patches are spread in files...

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

People watching this ticket

Tags

Referenced by

Pages