#833 ✓resolved
Tomoaki Hayasaka

``Hobo::Controller::Model.find_or_paginate`` overwrites ``finder.find``

Reported by Tomoaki Hayasaka | October 18th, 2010 @ 04:15 AM

Hobo::Controller::Model.find_or_paginate does finder.send(:scope, :find), which overwrites finder.find (e.g. User.find, you may find a warning about it in the log), and that causes many wierd results.

Reproducing:

  config.cache_classes = true

class UsersController < ApplicationController
  hobo_user_controller
  auto_actions :all
end

First time you go to /users, there's nothing special. But re-visiting /users gets "undefined method `guest?' for #<ActiveRecord::Relation:0xb2c3458>".

In rails3, ActiveRecord::Base.scope is still defined but its meaning is completely different.

Fix:

diff --git a/hobo/lib/hobo/controller/model.rb b/hobo/lib/hobo/controller/model.rb
index 2ef9c28..bb68d64 100644
--- a/hobo/lib/hobo/controller/model.rb
+++ b/hobo/lib/hobo/controller/model.rb
@@ -451,7 +451,7 @@ module Hobo
       do_pagination = options.delete(:paginate) && finder.respond_to?(:paginate)
       finder = Array.wrap(options.delete(:scope)).inject(finder) { |a, v| a.send(*Array.wrap(v).flatten) }
 
-      options[:order] = :default unless options[:order] || finder.send(:scope, :find)._?[:order]
+      options[:order] = finder.default_order unless options[:order] || finder.try.order_values.present?
 
       if do_pagination
         options.reverse_merge!(:page => params[:page] || 1)
@@ -734,7 +734,7 @@ module Hobo
 
     def hobo_completions(attribute, finder, options={})
       options = options.reverse_merge(:limit => 10, :param => :query, :query_scope => "#{attribute}_contains")
-      finder = finder.limit(options[:limit]) unless finder.send(:scope, :find, :limit)
+      finder = finder.limit(options[:limit]) unless finder.try.limit_value
 
       begin
         finder = finder.send(options[:query_scope], params[options[:param]])

Test:

  config.cache_classes = true

class User < ActiveRecord::Base
  hobo_user_model # Don't put anything above this
  fields do
    username      :string, :required, :unique
    email_address :email_address, :login => true
    timestamps
  end
  def name; sprintf("%02d - %s - %s", id, username, email_address); end
  def administrator?; true; end
  def create_permitted?; true; end
  def update_permitted?; true; end
  def destroy_permitted?; true; end
  def view_permitted?(field); true; end
end

class UsersController < ApplicationController
  hobo_user_controller

  def index
    ::User.send(:set_default_order, (params[:by] || :email_address))
    hobo_index
  end

  index_action :using_option do
    hobo_index ::User, :order => (params[:by] || :email_address)
    render :index
  end

  index_action :using_hobo_scope do
    hobo_index ::User.order(params[:by] || :email_address)
    render :index
  end

  index_action :using_ar_scope do
    hobo_index ::User.order_by(params[:by] || :email_address)
    render :index
  end
end

names = (0..35).map{|i| "A User #{i.to_s(36)}"}.shuffle
emails = (0..35).map{|i| "user_#{i.to_s(36)}@example.com"}.shuffle
(0..35).each { |i| User.create!(:username => names[i], :email_address => emails[i])}

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