From 6c51b3cc0ad7e193c7dae3794502088faab37c7b Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Mon, 8 Jul 2013 23:02:26 +0100 Subject: [PATCH] Replace deprecated finder methods --- app/controllers/api_controller.rb | 4 +-- app/controllers/browse_controller.rb | 4 +-- app/controllers/notes_controller.rb | 2 +- lib/classic_pagination/pagination.rb | 31 ++++++++++++++------ test/functional/amf_controller_test.rb | 4 +-- test/functional/changeset_controller_test.rb | 4 +-- test/functional/way_controller_test.rb | 2 +- 7 files changed, 32 insertions(+), 19 deletions(-) diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb index 5e75c731e..cc9e32266 100644 --- a/app/controllers/api_controller.rb +++ b/app/controllers/api_controller.rb @@ -151,9 +151,9 @@ class ApiController < ApplicationController # find which ways are needed ways = Array.new if node_ids.length > 0 - way_nodes = WayNode.find_all_by_node_id(node_ids) + way_nodes = WayNode.where(:node_id => node_ids) way_ids = way_nodes.collect { |way_node| way_node.id[0] } - ways = Way.find(way_ids, :include => [:way_nodes, :way_tags]) + ways = Way.preload(:way_nodes, :way_tags).find(way_ids) list_of_way_nodes = ways.collect { |way| way.way_nodes.collect { |way_node| way_node.node_id } diff --git a/app/controllers/browse_controller.rb b/app/controllers/browse_controller.rb index 622bcadd5..47d902a30 100644 --- a/app/controllers/browse_controller.rb +++ b/app/controllers/browse_controller.rb @@ -27,7 +27,7 @@ class BrowseController < ApplicationController def way @type = "way" - @way = Way.find(params[:id], :include => [:way_tags, {:changeset => :user}, {:nodes => [:node_tags, {:ways => :way_tags}]}, :containing_relation_members]) + @way = Way.preload(:way_tags, :containing_relation_members, :changeset => :user, :nodes => [:node_tags, :ways => :way_tags]).find(params[:id]) @next = Way.visible.where("id > ?", @way.id).order("id ASC").first @prev = Way.visible.where("id < ?", @way.id).order("id DESC").first rescue ActiveRecord::RecordNotFound @@ -36,7 +36,7 @@ class BrowseController < ApplicationController def way_history @type = "way" - @way = Way.find(params[:id], :include => [:way_tags, {:old_ways => {:changeset => :user}}]) + @way = Way.preload(:way_tags, :old_ways => { :changeset => :user }).find(params[:id]) rescue ActiveRecord::RecordNotFound render :action => "not_found", :status => :not_found end diff --git a/app/controllers/notes_controller.rb b/app/controllers/notes_controller.rb index f7344c211..a297748bd 100644 --- a/app/controllers/notes_controller.rb +++ b/app/controllers/notes_controller.rb @@ -279,7 +279,7 @@ class NotesController < ApplicationController @description = t 'note.mine.subheading', :user => render_to_string(:partial => "user", :object => @this_user) @page = (params[:page] || 1).to_i @page_size = 10 - @notes = @this_user.notes.order("updated_at DESC, id").uniq.offset((@page - 1) * @page_size).limit(@page_size).preload(:comments => :author).all + @notes = @this_user.notes.order("updated_at DESC, id").uniq.offset((@page - 1) * @page_size).limit(@page_size).preload(:comments => :author).to_a else @title = t 'user.no_such_user.title' @not_found_user = params[:display_name] diff --git a/lib/classic_pagination/pagination.rb b/lib/classic_pagination/pagination.rb index 6a3e1a97b..fa1307bee 100644 --- a/lib/classic_pagination/pagination.rb +++ b/lib/classic_pagination/pagination.rb @@ -173,21 +173,34 @@ module ActionController # the +model+ and given +conditions+. Override this method to implement a # custom counter. def count_collection_for_pagination(model, options) - model.count(:conditions => options[:conditions], - :joins => options[:join] || options[:joins], - :include => options[:include], - :select => (options[:group] ? "DISTINCT #{options[:group]}" : options[:count])) + collection = model.joins(options[:join] || options[:joins]) + collection = collection.where(options[:conditions]) + collection = collection.includes(options[:include]) + + if options[:group] + collection = collection.select(options[:group]).distinct + elsif options[:count] + collection = collection.select(options[:count]) + end + + collection.count end # Returns a collection of items for the given +model+ and +options[conditions]+, # ordered by +options[order]+, for the current page in the given +paginator+. # Override this method to implement a custom finder. def find_collection_for_pagination(model, options, paginator) - model.find(:all, :conditions => options[:conditions], - :order => options[:order_by] || options[:order], - :joins => options[:join] || options[:joins], :include => options[:include], - :select => options[:select], :limit => options[:per_page], - :group => options[:group], :offset => paginator.current.offset) + collection = model.joins(options[:join] || options[:joins]) + collection = collection.where(options[:conditions]) + collection = collection.order(options[:order_by] || options[:order]) + collection = collection.includes(options[:include]) + collection = collection.group(options[:group]) + + if options[:select] + collection = collection.select(options[:select]) + end + + collection.offset(paginator.current.offset).limit(options[:per_page]) end protected :create_paginators_and_retrieve_collections, diff --git a/test/functional/amf_controller_test.rb b/test/functional/amf_controller_test.rb index 757acf6ac..f3a4e7cd1 100644 --- a/test/functional/amf_controller_test.rb +++ b/test/functional/amf_controller_test.rb @@ -416,7 +416,7 @@ class AmfControllerTest < ActionController::TestCase assert_equal 0, current_node.tags.size, "There seems to be a tag that has been added to the node" assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf" # Now check the history table - historic_nodes = Node.find(:all, :conditions => { :id => result[3] }) + historic_nodes = Node.where(:id => result[3]) assert_equal 1, historic_nodes.size, "There should only be one historic node created" first_historic_node = historic_nodes.first assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly" @@ -456,7 +456,7 @@ class AmfControllerTest < ActionController::TestCase assert_equal({ "key" => "value", "ping" => "pong" }, current_node.tags, "tags are different") assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf" # Now check the history table - historic_nodes = Node.find(:all, :conditions => { :id => result[3] }) + historic_nodes = Node.where(:id => result[3]) assert_equal 1, historic_nodes.size, "There should only be one historic node created" first_historic_node = historic_nodes.first assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly" diff --git a/test/functional/changeset_controller_test.rb b/test/functional/changeset_controller_test.rb index 9ed9f2959..aace6e70d 100644 --- a/test/functional/changeset_controller_test.rb +++ b/test/functional/changeset_controller_test.rb @@ -1728,7 +1728,7 @@ EOF ## # This should display the last 20 changesets closed. def test_list - changesets = Changeset.find(:all, :order => "created_at DESC", :conditions => ['num_changes > 0'], :limit=> 20) + changesets = Changeset.where("num_changes > 0").order(:created_at => :desc).limit(20) assert changesets.size <= 20 get :list, {:format => "html"} assert_response :success @@ -1762,7 +1762,7 @@ EOF ## # This should display the last 20 changesets closed. def test_feed - changesets = Changeset.find(:all, :order => "created_at DESC", :conditions => ['num_changes > 0'], :limit=> 20) + changesets = Changeset.where("num_changes > 0").order(:created_at => :desc).limit(20) assert changesets.size <= 20 get :feed, {:format => "atom"} assert_response :success diff --git a/test/functional/way_controller_test.rb b/test/functional/way_controller_test.rb index a69612350..332f92dc7 100644 --- a/test/functional/way_controller_test.rb +++ b/test/functional/way_controller_test.rb @@ -54,7 +54,7 @@ class WayControllerTest < ActionController::TestCase ## # check the "full" mode def test_full - Way.find(:all).each do |way| + Way.all.each do |way| get :full, :id => way.id # full call should say "gone" for non-visible ways... -- 2.43.2