]> git.openstreetmap.org Git - rails.git/commitdiff
Replace deprecated finder methods
authorTom Hughes <tom@compton.nu>
Mon, 8 Jul 2013 22:02:26 +0000 (23:02 +0100)
committerTom Hughes <tom@compton.nu>
Sat, 21 Sep 2013 10:35:46 +0000 (11:35 +0100)
app/controllers/api_controller.rb
app/controllers/browse_controller.rb
app/controllers/notes_controller.rb
lib/classic_pagination/pagination.rb
test/functional/amf_controller_test.rb
test/functional/changeset_controller_test.rb
test/functional/way_controller_test.rb

index 5e75c731e9be85a1cbded6614815e4b688b54a3c..cc9e32266316263952a9eba2598904a67eed8b55 100644 (file)
@@ -151,9 +151,9 @@ class ApiController < ApplicationController
     # find which ways are needed
     ways = Array.new
     if node_ids.length > 0
     # 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] }
       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 }
 
       list_of_way_nodes = ways.collect { |way|
         way.way_nodes.collect { |way_node| way_node.node_id }
index 622bcadd5da137cfa648d91dee03ed9c9c5ccadd..47d902a30a10547252e3d91e2bf43cf1ca8dabaa 100644 (file)
@@ -27,7 +27,7 @@ class BrowseController < ApplicationController
   
   def way
     @type = "way"
   
   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
     @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"
   
   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
   rescue ActiveRecord::RecordNotFound
     render :action => "not_found", :status => :not_found
   end
index f7344c2116b3b8e7eb3e39fd1d89014c3639c5bf..a297748bdd69ae6a34d982669ef8c16fe4633088 100644 (file)
@@ -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
         @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] 
       else
         @title = t 'user.no_such_user.title' 
         @not_found_user = params[:display_name] 
index 6a3e1a97b0f661cb63937fc8bbcc6b0b6c533a38..fa1307beef86dc9edc1ab3b55953327cc2a5c294 100644 (file)
@@ -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)
     # 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)
     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,
     end
   
     protected :create_paginators_and_retrieve_collections,
index 757acf6aceaa808adb3b01cb2e86cce3598c180c..f3a4e7cd1b642ea69da87723ea2010eae669761d 100644 (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 result[4], current_node.version, "The version returned, is different to the one returned by the amf"
     # Now check the history table
     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"
     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
     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"
     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"
index 9ed9f295954ba3dc90382c4011933559c315f697..aace6e70ddef3c3f60c1ad2ec72f629097f90e40 100644 (file)
@@ -1728,7 +1728,7 @@ EOF
   ##
   # This should display the last 20 changesets closed.
   def test_list
   ##
   # 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
     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
   ##
   # 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
     assert changesets.size <= 20
     get :feed, {:format => "atom"}
     assert_response :success
index a69612350fbad26804b1ea25658bbe1e57122faa..332f92dc7898d3bc571fd1817fc4f4f8b8ed6df3 100644 (file)
@@ -54,7 +54,7 @@ class WayControllerTest < ActionController::TestCase
   ##
   # check the "full" mode
   def test_full
   ##
   # 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...
       get :full, :id => way.id
 
       # full call should say "gone" for non-visible ways...