Replace deprecated finder methods

This commit is contained in:
Tom Hughes 2013-07-08 23:02:26 +01:00
parent d8399d3e18
commit 6c51b3cc0a
7 changed files with 32 additions and 19 deletions

View file

@ -151,9 +151,9 @@ class ApiController < ApplicationController
# find which ways are needed # find which ways are needed
ways = Array.new ways = Array.new
if node_ids.length > 0 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] } 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| list_of_way_nodes = ways.collect { |way|
way.way_nodes.collect { |way_node| way_node.node_id } way.way_nodes.collect { |way_node| way_node.node_id }

View file

@ -27,7 +27,7 @@ class BrowseController < ApplicationController
def way def way
@type = "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 @next = Way.visible.where("id > ?", @way.id).order("id ASC").first
@prev = Way.visible.where("id < ?", @way.id).order("id DESC").first @prev = Way.visible.where("id < ?", @way.id).order("id DESC").first
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
@ -36,7 +36,7 @@ class BrowseController < ApplicationController
def way_history def way_history
@type = "way" @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 rescue ActiveRecord::RecordNotFound
render :action => "not_found", :status => :not_found render :action => "not_found", :status => :not_found
end end

View file

@ -279,7 +279,7 @@ class NotesController < ApplicationController
@description = t 'note.mine.subheading', :user => render_to_string(:partial => "user", :object => @this_user) @description = t 'note.mine.subheading', :user => render_to_string(:partial => "user", :object => @this_user)
@page = (params[:page] || 1).to_i @page = (params[:page] || 1).to_i
@page_size = 10 @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 else
@title = t 'user.no_such_user.title' @title = t 'user.no_such_user.title'
@not_found_user = params[:display_name] @not_found_user = params[:display_name]

View file

@ -173,21 +173,34 @@ module ActionController
# the +model+ and given +conditions+. Override this method to implement a # the +model+ and given +conditions+. Override this method to implement a
# custom counter. # custom counter.
def count_collection_for_pagination(model, options) def count_collection_for_pagination(model, options)
model.count(:conditions => options[:conditions], collection = model.joins(options[:join] || options[:joins])
:joins => options[:join] || options[:joins], collection = collection.where(options[:conditions])
:include => options[:include], collection = collection.includes(options[:include])
:select => (options[:group] ? "DISTINCT #{options[:group]}" : options[:count]))
if options[:group]
collection = collection.select(options[:group]).distinct
elsif options[:count]
collection = collection.select(options[:count])
end
collection.count
end end
# Returns a collection of items for the given +model+ and +options[conditions]+, # Returns a collection of items for the given +model+ and +options[conditions]+,
# ordered by +options[order]+, for the current page in the given +paginator+. # ordered by +options[order]+, for the current page in the given +paginator+.
# Override this method to implement a custom finder. # Override this method to implement a custom finder.
def find_collection_for_pagination(model, options, paginator) def find_collection_for_pagination(model, options, paginator)
model.find(:all, :conditions => options[:conditions], collection = model.joins(options[:join] || options[:joins])
:order => options[:order_by] || options[:order], collection = collection.where(options[:conditions])
:joins => options[:join] || options[:joins], :include => options[:include], collection = collection.order(options[:order_by] || options[:order])
:select => options[:select], :limit => options[:per_page], collection = collection.includes(options[:include])
:group => options[:group], :offset => paginator.current.offset) collection = collection.group(options[:group])
if options[:select]
collection = collection.select(options[:select])
end
collection.offset(paginator.current.offset).limit(options[:per_page])
end end
protected :create_paginators_and_retrieve_collections, protected :create_paginators_and_retrieve_collections,

View file

@ -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 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" assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
# Now check the history table # 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" assert_equal 1, historic_nodes.size, "There should only be one historic node created"
first_historic_node = historic_nodes.first first_historic_node = historic_nodes.first
assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly" 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({ "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" assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
# Now check the history table # 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" assert_equal 1, historic_nodes.size, "There should only be one historic node created"
first_historic_node = historic_nodes.first first_historic_node = historic_nodes.first
assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly" assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"

View file

@ -1728,7 +1728,7 @@ EOF
## ##
# This should display the last 20 changesets closed. # This should display the last 20 changesets closed.
def test_list 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 assert changesets.size <= 20
get :list, {:format => "html"} get :list, {:format => "html"}
assert_response :success assert_response :success
@ -1762,7 +1762,7 @@ EOF
## ##
# This should display the last 20 changesets closed. # This should display the last 20 changesets closed.
def test_feed 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 assert changesets.size <= 20
get :feed, {:format => "atom"} get :feed, {:format => "atom"}
assert_response :success assert_response :success

View file

@ -54,7 +54,7 @@ class WayControllerTest < ActionController::TestCase
## ##
# check the "full" mode # check the "full" mode
def test_full def test_full
Way.find(:all).each do |way| Way.all.each do |way|
get :full, :id => way.id get :full, :id => way.id
# full call should say "gone" for non-visible ways... # full call should say "gone" for non-visible ways...