Update some more queries to use AREL in place of deprecated methods

This commit is contained in:
Tom Hughes 2011-09-20 23:22:11 +01:00
parent 62e571a4aa
commit 8ae5d94b2f
16 changed files with 146 additions and 189 deletions

View file

@ -375,7 +375,7 @@ class AmfController < ApplicationController
# Ideally we would do ":include => :nodes" here but if we do that
# then rails only seems to return the first copy of a node when a
# way includes a node more than once
way = Way.find(:first, :conditions => { :id => wayid }, :include => { :nodes => :node_tags })
way = Way.where(:id => wayid).preload(:nodes => :node_tags).first
# check case where way has been deleted or doesn't exist
return [-4, 'way', wayid] if way.nil? or !way.visible
@ -413,13 +413,13 @@ class AmfController < ApplicationController
amf_handle_error_with_timeout("'getway_old' #{id}, #{timestamp}", 'way',id) do
if timestamp == ''
# undelete
old_way = OldWay.find(:first, :conditions => ['visible = ? AND id = ?', true, id], :order => 'version DESC')
old_way = OldWay.where(:visible => true, :id => id).order("version DESC").first
points = old_way.get_nodes_undelete unless old_way.nil?
else
begin
# revert
timestamp = DateTime.strptime(timestamp.to_s, "%d %b %Y, %H:%M:%S")
old_way = OldWay.find(:first, :conditions => ['id = ? AND timestamp <= ?', id, timestamp], :order => 'timestamp DESC')
old_way = OldWay.where("id = ? AND timestamp <= ?", id, timestamp).order("timestamp DESC").first
unless old_way.nil?
points = old_way.get_nodes_revert(timestamp)
if !old_way.visible
@ -515,16 +515,14 @@ class AmfController < ApplicationController
if !user then return -1,"You must be logged in to search for GPX traces." end
unless user.active_blocks.empty? then return -1,t('application.setup_user_auth.blocked') end
gpxs = []
if searchterm.to_i>0 then
gpx = Trace.find(searchterm.to_i, :conditions => ["visible=? AND (public=? OR user_id=?)",true,true,user.id] )
if gpx then
gpxs.push([gpx.id, gpx.name, gpx.description])
end
query = Trace.visible_to(user)
if searchterm.to_i > 0 then
query = query.where(:id => searchterm.to_i)
else
Trace.find(:all, :limit => 21, :conditions => ["visible=? AND (public=? OR user_id=?) AND MATCH(name) AGAINST (?)",true,true,user.id,searchterm] ).each do |gpx|
gpxs.push([gpx.id, gpx.name, gpx.description])
end
query = query.where("MATCH(name) AGAINST (?)", searchterm).limit(21)
end
gpxs = query.collect do |gpx|
[gpx.id, gpx.name, gpx.description]
end
[0,'',gpxs]
end
@ -541,7 +539,7 @@ class AmfController < ApplicationController
def getrelation(relid) #:doc:
amf_handle_error("'getrelation' #{relid}" ,'relation',relid) do
rel = Relation.find(:first, :conditions => { :id => relid })
rel = Relation.where(:id => relid).first
return [-4, 'relation', relid] if rel.nil? or !rel.visible
[0, '', relid, rel.tags, rel.members, rel.version]
@ -554,12 +552,12 @@ class AmfController < ApplicationController
def findrelations(searchterm)
rels = []
if searchterm.to_i>0 then
rel = Relation.find(searchterm.to_i)
rel = Relation.where(:id => searchterm.to_i).first
if rel and rel.visible then
rels.push([rel.id, rel.tags, rel.members, rel.version])
end
else
RelationTag.find(:all, :limit => 11, :conditions => ["v like ?", "%#{searchterm}%"] ).each do |t|
RelationTag.where("v like ?", "%#{searchterm}%").limit(11).each do |t|
if t.relation.visible then
rels.push([t.relation.id, t.relation.tags, t.relation.members, t.relation.version])
end
@ -838,7 +836,7 @@ class AmfController < ApplicationController
n = Node.find(id)
v = n.version
unless timestamp == ''
n = OldNode.find(:first, :conditions => ['id = ? AND timestamp <= ?', id, timestamp], :order => 'timestamp DESC')
n = OldNode.where("id = ? AND timestamp <= ?", id, timestamp).order("timestamp DESC").first
end
if n

View file

@ -12,8 +12,8 @@ class BrowseController < ApplicationController
def relation
@type = "relation"
@relation = Relation.find(params[:id])
@next = Relation.find(:first, :order => "id ASC", :conditions => [ "visible = true AND id > :id", { :id => @relation.id }] )
@prev = Relation.find(:first, :order => "id DESC", :conditions => [ "visible = true AND id < :id", { :id => @relation.id }] )
@next = Relation.visible.where("id > ?", @relation.id).order("id ASC").first
@prev = Relation.visible.where("id < ?", @relation.id).order("id DESC").first
rescue ActiveRecord::RecordNotFound
render :action => "not_found", :status => :not_found
end
@ -28,8 +28,8 @@ 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])
@next = Way.find(:first, :order => "id ASC", :conditions => [ "visible = true AND id > :id", { :id => @way.id }] )
@prev = Way.find(:first, :order => "id DESC", :conditions => [ "visible = true AND id < :id", { :id => @way.id }] )
@next = Way.visible.where("id > ?", @way.id).order("id ASC").first
@prev = Way.visible.where("id < ?", @way.id).order("id DESC").first
# Used for edit link, takes approx middle node of way
@midnode = @way.nodes[@way.nodes.length/2]
@ -47,8 +47,8 @@ class BrowseController < ApplicationController
def node
@type = "node"
@node = Node.find(params[:id])
@next = Node.find(:first, :order => "id ASC", :conditions => [ "visible = true AND id > :id", { :id => @node.id }] )
@prev = Node.find(:first, :order => "id DESC", :conditions => [ "visible = true AND id < :id", { :id => @node.id }] )
@next = Node.visible.where("id > ?", @node.id).order("id ASC").first
@prev = Node.visible.where("id < ?", @node.id).order("id DESC").first
rescue ActiveRecord::RecordNotFound
render :action => "not_found", :status => :not_found
end
@ -69,12 +69,12 @@ class BrowseController < ApplicationController
@relation_pages, @relations = paginate(:old_relations, :conditions => {:changeset_id => @changeset.id}, :per_page => 20, :parameter => 'relation_page')
@title = "#{I18n.t('browse.changeset.title')} | #{@changeset.id}"
@next = Changeset.find(:first, :order => "id ASC", :conditions => [ "id > :id", { :id => @changeset.id }] )
@prev = Changeset.find(:first, :order => "id DESC", :conditions => [ "id < :id", { :id => @changeset.id }] )
@next = Changeset.where("id > ?", @changeset.id).order("id ASC").first
@prev = Changeset.where("id < ?", @changeset.id).order("id DESC").first
if @changeset.user.data_public?
@next_by_user = Changeset.find(:first, :order => "id ASC", :conditions => [ "id > :id AND user_id = :user_id", { :id => @changeset.id, :user_id => @changeset.user_id }] )
@prev_by_user = Changeset.find(:first, :order => "id DESC", :conditions => [ "id < :id AND user_id = :user_id", { :id => @changeset.id, :user_id => @changeset.user_id }] )
@next_by_user = Changeset.where("user_id = ? AND id > ?", @changeset.user_id, @changeset.id).order("id ASC").first
@prev_by_user = Changeset.where("user_id = ? AND id < ?", @changeset.user_id, @changeset.id).order("id DESC").first
end
rescue ActiveRecord::RecordNotFound
render :action => "not_found", :status => :not_found

View file

@ -200,20 +200,18 @@ class ChangesetController < ApplicationController
def query
# create the conditions that the user asked for. some or all of
# these may be nil.
conditions = conditions_bbox(params['bbox'])
conditions = cond_merge conditions, conditions_user(params['user'], params['display_name'])
conditions = cond_merge conditions, conditions_time(params['time'])
conditions = cond_merge conditions, conditions_open(params['open'])
conditions = cond_merge conditions, conditions_closed(params['closed'])
changesets = Changeset.scoped
changesets = conditions_bbox(changesets, params['bbox'])
changesets = conditions_user(changesets, params['user'], params['display_name'])
changesets = conditions_time(changesets, params['time'])
changesets = conditions_open(changesets, params['open'])
changesets = conditions_closed(changesets, params['closed'])
# create the results document
results = OSM::API.new.get_xml_doc
# add all matching changesets to the XML results document
Changeset.find(:all,
:conditions => conditions,
:limit => 100,
:order => 'created_at desc').each do |cs|
changesets.order("created_at DESC").limit(100).each do |cs|
results.root << cs.to_xml_node
end
@ -253,16 +251,16 @@ class ChangesetController < ApplicationController
if request.format == :atom and params[:page]
redirect_to params.merge({ :page => nil }), :status => :moved_permanently
else
conditions = conditions_nonempty
changesets = conditions_nonempty(Changeset.scoped)
if params[:display_name]
user = User.find_by_display_name(params[:display_name], :conditions => { :status => ["active", "confirmed"] })
user = User.find_by_display_name(params[:display_name])
if user
if user and user.active?
if user.data_public? or user == @user
conditions = cond_merge conditions, ['user_id = ?', user.id]
changesets = changesets.where(:user_id => user.id)
else
conditions = cond_merge conditions, ['false']
changesets = changesets.where("false")
end
elsif request.format == :html
@title = t 'user.no_such_user.title'
@ -278,7 +276,7 @@ class ChangesetController < ApplicationController
end
if bbox
conditions = cond_merge conditions, conditions_bbox(bbox)
changesets = conditions_bbox(changesets, bbox)
bbox = BoundingBox.from_s(bbox)
bbox_link = render_to_string :partial => "bbox", :object => bbox
end
@ -310,12 +308,7 @@ class ChangesetController < ApplicationController
@bbox = bbox
@edits = Changeset.find(:all,
:include => [:user, :changeset_tags],
:conditions => conditions,
:order => "changesets.created_at DESC",
:offset => (@page - 1) * @page_size,
:limit => @page_size)
@edits = changesets.order("changesets.created_at DESC").offset((@page - 1) * @page_size).limit(@page_size).preload(:user, :changeset_tags)
end
end
@ -324,43 +317,29 @@ private
# utility functions below.
#------------------------------------------------------------
##
# merge two conditions
def cond_merge(a, b)
if a and b
a_str = a.shift
b_str = b.shift
return [ a_str + " AND " + b_str ] + a + b
elsif a
return a
else b
return b
end
end
##
# if a bounding box was specified then parse it and do some sanity
# checks. this is mostly the same as the map call, but without the
# area restriction.
def conditions_bbox(bbox)
def conditions_bbox(changesets, bbox)
unless bbox.nil?
raise OSM::APIBadUserInput.new("Bounding box should be min_lon,min_lat,max_lon,max_lat") unless bbox.count(',') == 3
bbox = sanitise_boundaries(bbox.split(/,/))
raise OSM::APIBadUserInput.new("Minimum longitude should be less than maximum.") unless bbox[0] <= bbox[2]
raise OSM::APIBadUserInput.new("Minimum latitude should be less than maximum.") unless bbox[1] <= bbox[3]
return ['min_lon < ? and max_lon > ? and min_lat < ? and max_lat > ?',
(bbox[2] * GeoRecord::SCALE).to_i,
(bbox[0] * GeoRecord::SCALE).to_i,
(bbox[3] * GeoRecord::SCALE).to_i,
(bbox[1] * GeoRecord::SCALE).to_i]
return changesets.where("min_lon < ? and max_lon > ? and min_lat < ? and max_lat > ?",
(bbox[2] * GeoRecord::SCALE).to_i,
(bbox[0] * GeoRecord::SCALE).to_i,
(bbox[3] * GeoRecord::SCALE).to_i,
(bbox[1] * GeoRecord::SCALE).to_i)
else
return nil
return changesets
end
end
##
# restrict changesets to those by a particular user
def conditions_user(user, name)
def conditions_user(changesets, user, name)
unless user.nil? and name.nil?
# shouldn't provide both name and UID
raise OSM::APIBadUserInput.new("provide either the user ID or display name, but not both") if user and name
@ -386,15 +365,15 @@ private
raise OSM::APINotFoundError if @user.nil? or @user.id != u.id
end
return ['user_id = ?', u.id]
return changesets.where(:user_id => u.id)
else
return nil
return changesets
end
end
##
# restrict changes to those closed during a particular time period
def conditions_time(time)
def conditions_time(changesets, time)
unless time.nil?
# if there is a range, i.e: comma separated, then the first is
# low, second is high - same as with bounding boxes.
@ -404,13 +383,13 @@ private
raise OSM::APIBadUserInput.new("bad time range") if times.size != 2
from, to = times.collect { |t| DateTime.parse(t) }
return ['closed_at >= ? and created_at <= ?', from, to]
return changesets.where("closed_at >= ? and created_at <= ?", from, to)
else
# if there is no comma, assume its a lower limit on time
return ['closed_at >= ?', DateTime.parse(time)]
return changesets.where("closed_at >= ?", DateTime.parse(time))
end
else
return nil
return changesets
end
# stupid DateTime seems to throw both of these for bad parsing, so
# we have to catch both and ensure the correct code path is taken.
@ -424,25 +403,33 @@ private
# return changesets which are open (haven't been closed yet)
# we do this by seeing if the 'closed at' time is in the future. Also if we've
# hit the maximum number of changes then it counts as no longer open.
# if parameter 'open' is nill then open and closed changsets are returned
def conditions_open(open)
return open.nil? ? nil : ['closed_at >= ? and num_changes <= ?',
Time.now.getutc, Changeset::MAX_ELEMENTS]
# if parameter 'open' is nill then open and closed changesets are returned
def conditions_open(changesets, open)
if open.nil?
return changesets
else
return changesets.where("closed_at >= ? and num_changes <= ?",
Time.now.getutc, Changeset::MAX_ELEMENTS)
end
end
##
# query changesets which are closed
# ('closed at' time has passed or changes limit is hit)
def conditions_closed(closed)
return closed.nil? ? nil : ['(closed_at < ? or num_changes > ?)',
Time.now.getutc, Changeset::MAX_ELEMENTS]
def conditions_closed(changesets, closed)
if closed.nil?
return changesets
else
return changesets.where("closed_at < ? or num_changes > ?",
Time.now.getutc, Changeset::MAX_ELEMENTS)
end
end
##
# eliminate empty changesets (where the bbox has not been set)
# this should be applied to all changeset list displays
def conditions_nonempty()
return ['num_changes > 0']
def conditions_nonempty(changesets)
return changesets.where("num_changes > 0")
end
end

View file

@ -7,7 +7,7 @@ class OauthClientsController < ApplicationController
def index
@client_applications = @user.client_applications
@tokens = @user.oauth_tokens.find :all, :conditions => 'oauth_tokens.invalidated_at is null and oauth_tokens.authorized_at is not null'
@tokens = @user.oauth_tokens.authorized
end
def new

View file

@ -41,69 +41,28 @@ class SearchController < ApplicationController
return false
end
way_ids = Array.new
ways = Array.new
nodes = Array.new
relations = Array.new
# Matching for node tags table
cond_node = Array.new
sql = '1=1'
if type
sql += ' AND current_node_tags.k=?'
cond_node += [type]
if do_nodes
nodes = Node.joins(:node_tags)
nodes = nodes.where(:current_node_tags => { :k => type }) if type
nodes = nodes.where(:current_node_tags => { :v => value }) if value
nodes = nodes.limit(100)
end
if value
sql += ' AND current_node_tags.v=?'
cond_node += [value]
end
cond_node = [sql] + cond_node
# Matching for way tags table
cond_way = Array.new
sql = '1=1'
if type
sql += ' AND current_way_tags.k=?'
cond_way += [type]
if do_ways
ways = Way.joins(:way_tags)
ways = ways.where(:current_way_tags => { :k => type }) if type
ways = ways.where(:current_way_tags => { :v => value }) if value
ways = ways.limit(100)
end
if value
sql += ' AND current_way_tags.v=?'
cond_way += [value]
end
cond_way = [sql] + cond_way
# Matching for relation tags table
cond_rel = Array.new
sql = '1=1'
if type
sql += ' AND current_relation_tags.k=?'
cond_rel += [type]
end
if value
sql += ' AND current_relation_tags.v=?'
cond_rel += [value]
end
cond_rel = [sql] + cond_rel
# First up, look for the relations we want
if do_relations
relations = Relation.find(:all,
:joins => "INNER JOIN current_relation_tags ON current_relation_tags.id = current_relations.id",
:conditions => cond_rel, :limit => 100)
end
# then ways
if do_ways
ways = Way.find(:all,
:joins => "INNER JOIN current_way_tags ON current_way_tags.id = current_ways.id",
:conditions => cond_way, :limit => 100)
end
# Now, nodes
if do_nodes
nodes = Node.find(:all,
:joins => "INNER JOIN current_node_tags ON current_node_tags.id = current_nodes.id",
:conditions => cond_node, :limit => 2000)
relations = Relation.joins(:relation_tags)
relations = relations.where(:current_relation_tags => { :k => type }) if type
relations = relations.where(:current_relation_tags => { :v => value }) if value
relations = relations.limit(2000)
end
# Fetch any node needed for our ways (only have matching nodes so far)
@ -121,11 +80,12 @@ class SearchController < ApplicationController
ways.each do |way|
doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
end
end
relations.each do |rel|
doc.root << rel.to_xml_node(nil, changeset_cache, user_display_name_cache)
end
end
render :text => doc.to_s, :content_type => "text/xml"
end
end

View file

@ -272,7 +272,7 @@ class TraceController < ApplicationController
end
def api_read
trace = Trace.find(params[:id], :conditions => { :visible => true })
trace = Trace.visible.find(params[:id])
if trace.public? or trace.user == @user
render :text => trace.to_xml.to_s, :content_type => "text/xml"
@ -282,7 +282,7 @@ class TraceController < ApplicationController
end
def api_update
trace = Trace.find(params[:id], :conditions => { :visible => true })
trace = Trace.visible.find(params[:id])
if trace.user == @user
new_trace = Trace.from_xml(request.raw_post)
@ -303,7 +303,7 @@ class TraceController < ApplicationController
end
def api_delete
trace = Trace.find(params[:id], :conditions => { :visible => true })
trace = Trace.visible.find(params[:id])
if trace.user == @user
trace.visible = false