Update ActiveRecord queries to use arel

This commit is contained in:
Tom Hughes 2010-09-09 10:56:19 +01:00
parent a543e731c0
commit 226c41be69
20 changed files with 110 additions and 185 deletions

View file

@ -314,7 +314,7 @@ class AmfController < ApplicationController
relations = sql_find_relations_in_area_and_ways(xmin, ymin, xmax, ymax, ways.collect {|x| x[0]}) relations = sql_find_relations_in_area_and_ways(xmin, ymin, xmax, ymax, ways.collect {|x| x[0]})
else else
# find the way ids in an area # find the way ids in an area
nodes_in_area = Node.find_by_area(ymin, xmin, ymax, xmax, :conditions => ["current_nodes.visible = ?", true], :include => :ways) nodes_in_area = Node.bbox(ymin, xmin, ymax, xmax).visible.includes(:ways)
ways = nodes_in_area.inject([]) { |sum, node| ways = nodes_in_area.inject([]) { |sum, node|
visible_ways = node.ways.select { |w| w.visible? } visible_ways = node.ways.select { |w| w.visible? }
sum + visible_ways.collect { |w| [w.id,w.version] } sum + visible_ways.collect { |w| [w.id,w.version] }
@ -326,8 +326,8 @@ class AmfController < ApplicationController
points = nodes_not_used_in_area.collect { |n| [n.id, n.lon, n.lat, n.tags, n.version] }.uniq points = nodes_not_used_in_area.collect { |n| [n.id, n.lon, n.lat, n.tags, n.version] }.uniq
# find the relations used by those nodes and ways # find the relations used by those nodes and ways
relations = Relation.find_for_nodes(nodes_in_area.collect { |n| n.id }, :conditions => {:visible => true}) + relations = Relation.nodes(nodes_in_area.collect { |n| n.id }).visible +
Relation.find_for_ways(ways.collect { |w| w[0] }, :conditions => {:visible => true}) Relation.ways(ways.collect { |w| w[0] }).visible
relations = relations.collect { |relation| [relation.id,relation.version] }.uniq relations = relations.collect { |relation| [relation.id,relation.version] }.uniq
end end
@ -348,8 +348,8 @@ class AmfController < ApplicationController
# see /config/application.yml # see /config/application.yml
check_boundaries(xmin, ymin, xmax, ymax) check_boundaries(xmin, ymin, xmax, ymax)
nodes_in_area = Node.find_by_area(ymin, xmin, ymax, xmax, :conditions => ["current_ways.visible = ?", false], :include => :ways_via_history) nodes_in_area = Node.bbox(ymin, xmin, ymax, xmax).joins(:ways_via_history).where(:current_ways => { :visible => false })
way_ids = nodes_in_area.collect { |node| node.ways_via_history_ids }.flatten.uniq way_ids = nodes_in_area.collect { |node| node.ways_via_history.invisible.collect { |way| way.id } }.flatten.uniq
[0,'',way_ids] [0,'',way_ids]
end end

View file

@ -39,7 +39,7 @@ class ApiController < ApplicationController
end end
# get all the points # get all the points
points = Tracepoint.find_by_area(min_lat, min_lon, max_lat, max_lon, :offset => offset, :limit => TRACEPOINTS_PER_PAGE, :order => "gpx_id DESC, trackid ASC, timestamp ASC" ) points = Tracepoint.bbox(min_lat, min_lon, max_lat, max_lon).offset(offset).limit(TRACEPOINTS_PER_PAGE).order("gpx_id DESC, trackid ASC, timestamp ASC")
doc = XML::Document.new doc = XML::Document.new
doc.encoding = XML::Encoding::UTF_8 doc.encoding = XML::Encoding::UTF_8
@ -145,7 +145,7 @@ class ApiController < ApplicationController
end end
# FIXME um why is this area using a different order for the lat/lon from above??? # FIXME um why is this area using a different order for the lat/lon from above???
@nodes = Node.find_by_area(min_lat, min_lon, max_lat, max_lon, :conditions => {:visible => true}, :include => :node_tags, :limit => MAX_NUMBER_OF_NODES+1) @nodes = Node.bbox(min_lat, min_lon, max_lat, max_lon).where(:visible => true).includes(:node_tags).limit(MAX_NUMBER_OF_NODES+1)
# get all the nodes, by tag not yet working, waiting for change from NickB # get all the nodes, by tag not yet working, waiting for change from NickB
# need to be @nodes (instance var) so tests in /spec can be performed # need to be @nodes (instance var) so tests in /spec can be performed
#@nodes = Node.search(bbox, params[:tag]) #@nodes = Node.search(bbox, params[:tag])
@ -191,7 +191,7 @@ class ApiController < ApplicationController
nodes_to_fetch = (list_of_way_nodes.uniq - node_ids) - [0] nodes_to_fetch = (list_of_way_nodes.uniq - node_ids) - [0]
if nodes_to_fetch.length > 0 if nodes_to_fetch.length > 0
@nodes += Node.find(nodes_to_fetch, :include => :node_tags) @nodes += Node.includes(:node_tags).find(nodes_to_fetch)
end end
visible_nodes = {} visible_nodes = {}
@ -213,15 +213,15 @@ class ApiController < ApplicationController
end end
end end
relations = Relation.find_for_nodes(visible_nodes.keys, :conditions => {:visible => true}) + relations = Relation.nodes(visible_nodes.keys).visible +
Relation.find_for_ways(way_ids, :conditions => {:visible => true}) Relation.ways(way_ids).visible
# we do not normally return the "other" partners referenced by an relation, # we do not normally return the "other" partners referenced by an relation,
# e.g. if we return a way A that is referenced by relation X, and there's # e.g. if we return a way A that is referenced by relation X, and there's
# another way B also referenced, that is not returned. But we do make # another way B also referenced, that is not returned. But we do make
# an exception for cases where an relation references another *relation*; # an exception for cases where an relation references another *relation*;
# in that case we return that as well (but we don't go recursive here) # in that case we return that as well (but we don't go recursive here)
relations += Relation.find_for_relations(relations.collect { |r| r.id }, :conditions => {:visible => true}) relations += Relation.relations(relations.collect { |r| r.id }).visible
# this "uniq" may be slightly inefficient; it may be better to first collect and output # this "uniq" may be slightly inefficient; it may be better to first collect and output
# all node-related relations, then find the *not yet covered* way-related ones etc. # all node-related relations, then find the *not yet covered* way-related ones etc.
@ -252,8 +252,7 @@ class ApiController < ApplicationController
endtime > starttime and endtime - starttime <= 24.hours endtime > starttime and endtime - starttime <= 24.hours
mask = (1 << zoom) - 1 mask = (1 << zoom) - 1
tiles = Node.count(:conditions => ["timestamp BETWEEN ? AND ?", starttime, endtime], tiles = Node.where(:timestamp => starttime .. endtime).group("maptile_for_point(latitude, longitude, #{zoom})").count
:group => "maptile_for_point(latitude, longitude, #{zoom})")
doc = OSM::API.new.get_xml_doc doc = OSM::API.new.get_xml_doc
changes = XML::Node.new 'changes' changes = XML::Node.new 'changes'

View file

@ -11,7 +11,7 @@ class ApplicationController < ActionController::Base
def authorize_web def authorize_web
if session[:user] if session[:user]
@user = User.find(session[:user], :conditions => {:status => ["active", "confirmed", "suspended"]}) @user = User.where(:status => ["active", "confirmed", "suspended"]).find(session[:user])
if @user.status == "suspended" if @user.status == "suspended"
session.delete(:user) session.delete(:user)

View file

@ -177,9 +177,7 @@ class ChangesetController < ApplicationController
created << elt.to_xml_node created << elt.to_xml_node
else else
# get the previous version from the element history # get the previous version from the element history
prev_elt = elt.class.find(:first, :conditions => prev_elt = elt.class.where(:id => elt.id, :version => elt.version).first
['id = ? and version = ?',
elt.id, elt.version])
unless elt.visible unless elt.visible
# if the element isn't visible then it must have been deleted, so # if the element isn't visible then it must have been deleted, so
# output the *previous* XML # output the *previous* XML

View file

@ -20,7 +20,7 @@ class DiaryEntryController < ApplicationController
@diary_entry.user = @user @diary_entry.user = @user
if @diary_entry.save if @diary_entry.save
default_lang = @user.preferences.find(:first, :conditions => {:k => "diary.default_language"}) default_lang = @user.preferences.where(:k => "diary.default_language").first
if default_lang if default_lang
default_lang.v = @diary_entry.language_code default_lang.v = @diary_entry.language_code
default_lang.save! default_lang.save!
@ -32,7 +32,7 @@ class DiaryEntryController < ApplicationController
render :action => 'edit' render :action => 'edit'
end end
else else
default_lang = @user.preferences.find(:first, :conditions => {:k => "diary.default_language"}) default_lang = @user.preferences.where(:k => "diary.default_language").first
lang_code = default_lang ? default_lang.v : @user.preferred_language lang_code = default_lang ? default_lang.v : @user.preferred_language
@diary_entry = DiaryEntry.new(:language_code => lang_code) @diary_entry = DiaryEntry.new(:language_code => lang_code)
render :action => 'edit' render :action => 'edit'
@ -71,7 +71,7 @@ class DiaryEntryController < ApplicationController
def list def list
if params[:display_name] if params[:display_name]
@this_user = User.find_by_display_name(params[:display_name], :conditions => { :status => ["active", "confirmed"] }) @this_user = User.where(:status => ["active", "confirmed"]).find_by_display_name(params[:display_name])
if @this_user if @this_user
@title = t 'diary_entry.list.user_title', :user => @this_user.display_name @title = t 'diary_entry.list.user_title', :user => @this_user.display_name
@ -111,17 +111,13 @@ class DiaryEntryController < ApplicationController
end end
def rss def rss
@entries = DiaryEntry.includes(:user).order("created_at DESC").limit(20)
if params[:display_name] if params[:display_name]
user = User.find_by_display_name(params[:display_name], :conditions => { :status => ["active", "confirmed"] }) user = User.where(:status => ["active", "confirmed"]).find_by_display_name(params[:display_name])
if user if user
@entries = DiaryEntry.find(:all, @entries = @entries.where(:user_id => user.id, :visible => true )
:conditions => {
:user_id => user.id,
:visible => true
},
:order => 'created_at DESC',
:limit => 20)
@title = I18n.t('diary_entry.feed.user.title', :user => user.display_name) @title = I18n.t('diary_entry.feed.user.title', :user => user.display_name)
@description = I18n.t('diary_entry.feed.user.description', :user => user.display_name) @description = I18n.t('diary_entry.feed.user.description', :user => user.display_name)
@link = "http://#{SERVER_URL}/user/#{user.display_name}/diary" @link = "http://#{SERVER_URL}/user/#{user.display_name}/diary"
@ -129,25 +125,15 @@ class DiaryEntryController < ApplicationController
render :nothing => true, :status => :not_found render :nothing => true, :status => :not_found
end end
elsif params[:language] elsif params[:language]
@entries = DiaryEntry.find(:all, :include => :user, @entries = @entries.where(:users => { :status => ["active", "confirmed"] },
:conditions => { :visible => true,
:users => { :status => ["active", "confirmed"] }, :language_code => params[:language])
:visible => true,
:language_code => params[:language]
},
:order => 'created_at DESC',
:limit => 20)
@title = I18n.t('diary_entry.feed.language.title', :language_name => Language.find(params[:language]).english_name) @title = I18n.t('diary_entry.feed.language.title', :language_name => Language.find(params[:language]).english_name)
@description = I18n.t('diary_entry.feed.language.description', :language_name => Language.find(params[:language]).english_name) @description = I18n.t('diary_entry.feed.language.description', :language_name => Language.find(params[:language]).english_name)
@link = "http://#{SERVER_URL}/diary/#{params[:language]}" @link = "http://#{SERVER_URL}/diary/#{params[:language]}"
else else
@entries = DiaryEntry.find(:all, :include => :user, @entries = @entries.where(:users => { :status => ["active", "confirmed"] },
:conditions => { :visible => true)
:users => { :status => ["active", "confirmed"] },
:visible => true
},
:order => 'created_at DESC',
:limit => 20)
@title = I18n.t('diary_entry.feed.all.title') @title = I18n.t('diary_entry.feed.all.title')
@description = I18n.t('diary_entry.feed.all.description') @description = I18n.t('diary_entry.feed.all.description')
@link = "http://#{SERVER_URL}/diary" @link = "http://#{SERVER_URL}/diary"
@ -155,14 +141,12 @@ class DiaryEntryController < ApplicationController
end end
def view def view
user = User.find_by_display_name(params[:display_name], :conditions => { :status => ["active", "confirmed"] }) user = User.where(:status => ["active", "confirmed"]).find_by_display_name(params[:display_name])
if user if user
@entry = DiaryEntry.find(:first, :conditions => { @entry = DiaryEntry.where(:id => params[:id],
:id => params[:id], :user_id => user.id,
:user_id => user.id, :visible => true).first
:visible => true
})
if @entry if @entry
@title = t 'diary_entry.view.title', :user => params[:display_name], :title => @entry.title @title = t 'diary_entry.view.title', :user => params[:display_name], :title => @entry.title
else else

View file

@ -15,7 +15,7 @@ class MessageController < ApplicationController
@to_user = User.find_by_display_name(params[:display_name]) @to_user = User.find_by_display_name(params[:display_name])
if @to_user if @to_user
if params[:message] if params[:message]
if @user.sent_messages.count(:conditions => ["sent_on >= ?", Time.now.getutc - 1.hour]) >= MAX_MESSAGES_PER_HOUR if @user.sent_messages.where("sent_on >= ?", Time.now.getutc - 1.hour).count >= MAX_MESSAGES_PER_HOUR
flash[:error] = t 'message.new.limit_exceeded' flash[:error] = t 'message.new.limit_exceeded'
else else
@message = Message.new(params[:message]) @message = Message.new(params[:message])
@ -103,7 +103,7 @@ class MessageController < ApplicationController
def mark def mark
if params[:message_id] if params[:message_id]
id = params[:message_id] id = params[:message_id]
message = Message.find_by_id(id, :conditions => ["to_user_id = ? or from_user_id = ?", @user.id, @user.id]) message = Message.where(:id => id).where("to_user_id = ? OR from_user_id = ?", @user.id, @user.id).first
if params[:mark] == 'unread' if params[:mark] == 'unread'
message_read = false message_read = false
notice = t 'message.mark.as_unread' notice = t 'message.mark.as_unread'
@ -134,7 +134,7 @@ class MessageController < ApplicationController
def delete def delete
if params[:message_id] if params[:message_id]
id = params[:message_id] id = params[:message_id]
message = Message.find_by_id(id, :conditions => ["to_user_id = ? or from_user_id = ?", @user.id, @user.id]) message = Message.where(:id => id).where("to_user_id = ? OR from_user_id = ?", @user.id, @user.id).first
message.from_user_visible = false if message.sender == @user message.from_user_visible = false if message.sender == @user
message.to_user_visible = false if message.recipient == @user message.to_user_visible = false if message.recipient == @user
if message.save if message.save

View file

@ -18,18 +18,15 @@ class OldNodeController < ApplicationController
end end
def version def version
old_node = OldNode.find(:first, :conditions => {:id => params[:id], :version => params[:version]} ) if old_node = OldNode.where(:id => params[:id], :version => params[:version]).first
if old_node.nil? response.headers['Last-Modified'] = old_node.timestamp.rfc822
# (RecordNotFound is not raised with find :first...)
render :nothing => true, :status => :not_found
return
end
response.headers['Last-Modified'] = old_node.timestamp.rfc822
doc = OSM::API.new.get_xml_doc
doc.root << old_node.to_xml_node
render :text => doc.to_s, :content_type => "text/xml" doc = OSM::API.new.get_xml_doc
doc.root << old_node.to_xml_node
render :text => doc.to_s, :content_type => "text/xml"
else
render :nothing => true, :status => :not_found
end
end end
end end

View file

@ -17,18 +17,15 @@ class OldRelationController < ApplicationController
end end
def version def version
old_relation = OldRelation.find(:first, :conditions => {:id => params[:id], :version => params[:version]} ) if old_relation = OldRelation.where(:id => params[:id], :version => params[:version]).first
if old_relation.nil? response.headers['Last-Modified'] = old_relation.timestamp.rfc822
# (RecordNotFound is not raised with find :first...)
doc = OSM::API.new.get_xml_doc
doc.root << old_relation.to_xml_node
render :text => doc.to_s, :content_type => "text/xml"
else
render :nothing => true, :status => :not_found render :nothing => true, :status => :not_found
return
end end
response.headers['Last-Modified'] = old_relation.timestamp.rfc822
doc = OSM::API.new.get_xml_doc
doc.root << old_relation.to_xml_node
render :text => doc.to_s, :content_type => "text/xml"
end end
end end

View file

@ -18,18 +18,15 @@ class OldWayController < ApplicationController
end end
def version def version
old_way = OldWay.find(:first, :conditions => {:id => params[:id], :version => params[:version]} ) if old_way = OldWay.where(:id => params[:id], :version => params[:version]).first
if old_way.nil? response.headers['Last-Modified'] = old_way.timestamp.rfc822
# (RecordNotFound is not raised with find :first...)
doc = OSM::API.new.get_xml_doc
doc.root << old_way.to_xml_node
render :text => doc.to_s, :content_type => "text/xml"
else
render :nothing => true, :status => :not_found render :nothing => true, :status => :not_found
return
end end
response.headers['Last-Modified'] = old_way.timestamp.rfc822
doc = OSM::API.new.get_xml_doc
doc.root << old_way.to_xml_node
render :text => doc.to_s, :content_type => "text/xml"
end end
end end

View file

@ -81,8 +81,8 @@ class RelationController < ApplicationController
# next load the relations and the ways. # next load the relations and the ways.
relations = Relation.find(relation_ids, :include => [:relation_tags]) relations = Relation.where(:id => relation_ids).includes(:relation_tags)
ways = Way.find(way_ids, :include => [:way_nodes, :way_tags]) ways = Way.where(:id => way_ids).includes(:way_nodes, :way_tags)
# now additionally collect nodes referenced by ways. Note how we # now additionally collect nodes referenced by ways. Note how we
# recursively evaluate ways but NOT relations. # recursively evaluate ways but NOT relations.
@ -91,7 +91,7 @@ class RelationController < ApplicationController
way.way_nodes.collect { |way_node| way_node.node_id } way.way_nodes.collect { |way_node| way_node.node_id }
} }
node_ids += way_node_ids.flatten node_ids += way_node_ids.flatten
nodes = Node.find(node_ids.uniq, :include => :node_tags) nodes = Node.where(:id => node_ids.uniq).includes(:node_tags)
# create XML. # create XML.
doc = OSM::API.new.get_xml_doc doc = OSM::API.new.get_xml_doc
@ -157,7 +157,7 @@ class RelationController < ApplicationController
end end
def relations_for_object(objtype) def relations_for_object(objtype)
relationids = RelationMember.find(:all, :conditions => ['member_type=? and member_id=?', objtype, params[:id]]).collect { |ws| ws.id[0] }.uniq relationids = RelationMember.where(:member_type => objtype, :member_id => params[:id]).collect { |ws| ws.id[0] }.uniq
doc = OSM::API.new.get_xml_doc doc = OSM::API.new.get_xml_doc

View file

@ -27,7 +27,7 @@ class TraceController < ApplicationController
# from display name, pick up user id if one user's traces only # from display name, pick up user id if one user's traces only
display_name = params[:display_name] display_name = params[:display_name]
if !display_name.blank? if !display_name.blank?
target_user = User.find(:first, :conditions => { :status => ["active", "confirmed"], :display_name => display_name }) target_user = User.where(:status => ["active", "confirmed"], :display_name => display_name).first
if target_user.nil? if target_user.nil?
@title = t'trace.no_such_user.title' @title = t'trace.no_such_user.title'
@not_found_user = display_name @not_found_user = display_name
@ -54,51 +54,43 @@ class TraceController < ApplicationController
# 4 - user's traces, not logged in as that user = all user's public traces # 4 - user's traces, not logged in as that user = all user's public traces
if target_user.nil? # all traces if target_user.nil? # all traces
if @user if @user
conditions = ["(gpx_files.visibility in ('public', 'identifiable') OR gpx_files.user_id = ?)", @user.id] #1 @traces = Trace.where("visibility IN ('public', 'identifiable') OR user_id = ?", @user.id) #1
else else
conditions = ["gpx_files.visibility in ('public', 'identifiable')"] #2 @traces = Trace.where("visibility IN ('public', 'identifiable')") #2
end end
else else
if @user and @user == target_user if @user and @user == target_user
conditions = ["gpx_files.user_id = ?", @user.id] #3 (check vs user id, so no join + can't pick up non-public traces by changing name) @traces = Trace.where(:user_id => @user.id) #3 (check vs user id, so no join + can't pick up non-public traces by changing name)
else else
conditions = ["gpx_files.visibility in ('public', 'identifiable') AND gpx_files.user_id = ?", target_user.id] #4 @traces = Trace.where("visibility IN ('public', 'identifiable') AND user_id = ?", target_user.id) #4
end end
end end
if params[:tag] if params[:tag]
@tag = params[:tag] @tag = params[:tag]
files = Tracetag.find_all_by_tag(params[:tag]).collect { |tt| tt.gpx_id } files = Tracetag.where(:tag => params[:tag]).select(:gpx_id).all
if files.length > 0 if files.length > 0
conditions[0] += " AND gpx_files.id IN (#{files.join(',')})" @traces = @traces.where(:id => files.collect { |tt| tt.gpx_id })
else
conditions[0] += " AND 0 = 1"
end end
end end
conditions[0] += " AND gpx_files.visible = ?"
conditions << true
@page = (params[:page] || 1).to_i @page = (params[:page] || 1).to_i
@page_size = 20 @page_size = 20
@traces = Trace.find(:all, @traces = @traces.where(:visible => true)
:include => [:user, :tags], @traces = @traces.order("timestamp DESC")
:conditions => conditions, @traces = @traces.offset((@page - 1) * @page_size)
:order => "gpx_files.timestamp DESC", @traces = @traces.limit(@page_size)
:offset => (@page - 1) * @page_size, @traces = @traces.includes(:user, :tags)
:limit => @page_size)
# put together SET of tags across traces, for related links # put together SET of tags across traces, for related links
tagset = Hash.new tagset = Hash.new
if @traces @traces.each do |trace|
@traces.each do |trace| trace.tags.reload if params[:tag] # if searched by tag, ActiveRecord won't bring back other tags, so do explicitly here
trace.tags.reload if params[:tag] # if searched by tag, ActiveRecord won't bring back other tags, so do explicitly here trace.tags.each do |tag|
trace.tags.each do |tag| tagset[tag.tag] = tag.tag
tagset[tag.tag] = tag.tag
end
end end
end end
@ -222,20 +214,19 @@ class TraceController < ApplicationController
end end
def georss def georss
conditions = ["gpx_files.visibility in ('public', 'identifiable')"] traces = Trace.where(:visibility => [:public, :identifiable])
if params[:display_name] if params[:display_name]
conditions[0] += " AND users.display_name = ?" traces = traces.where(:users => {:display_name => params[:display_name]})
conditions << params[:display_name]
end end
if params[:tag] if params[:tag]
conditions[0] += " AND EXISTS (SELECT * FROM gpx_file_tags AS gft WHERE gft.gpx_id = gpx_files.id AND gft.tag = ?)" traces = traces.where("EXISTS (SELECT * FROM gpx_file_tags AS gft WHERE gft.gpx_id = gpx_files.id AND gft.tag = ?)")
conditions << params[:tag]
end end
traces = Trace.find(:all, :include => :user, :conditions => conditions, traces = traces.order("timestamp DESC")
:order => "timestamp DESC", :limit => 20) traces = traces.limit(20)
traces = traces.includes(:user)
rss = OSM::GeoRSS.new rss = OSM::GeoRSS.new
@ -423,7 +414,7 @@ private
end end
# Finally save the user's preferred privacy level # Finally save the user's preferred privacy level
if pref = @user.preferences.find(:first, :conditions => {:k => "gps.trace.visibility"}) if pref = @user.preferences.where(:k => "gps.trace.visibility").first
pref.v = visibility pref.v = visibility
pref.save pref.save
else else
@ -441,11 +432,11 @@ private
end end
def default_visibility def default_visibility
visibility = @user.preferences.find(:first, :conditions => {:k => "gps.trace.visibility"}) visibility = @user.preferences.where(:k => "gps.trace.visibility").first
if visibility if visibility
visibility.v visibility.v
elsif @user.preferences.find(:first, :conditions => {:k => "gps.trace.public", :v => "default"}).nil? elsif @user.preferences.where(:k => "gps.trace.public", :v => "default").first.nil?
"private" "private"
else else
"public" "public"

View file

@ -80,7 +80,7 @@ class UserController < ApplicationController
def save def save
@title = t 'user.new.title' @title = t 'user.new.title'
if Acl.find_by_address(request.remote_ip, :conditions => {:k => "no_account_creation"}) if Acl.address(request.remote_ip).where(:k => "no_account_creation").exists?
render :action => 'new' render :action => 'new'
elsif params[:decline] elsif params[:decline]
if @user if @user
@ -139,7 +139,7 @@ class UserController < ApplicationController
def account def account
@title = t 'user.account.title' @title = t 'user.account.title'
@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.where('oauth_tokens.invalidated_at is null and oauth_tokens.authorized_at is not null')
if params[:user] and params[:user][:display_name] and params[:user][:description] if params[:user] and params[:user][:display_name] and params[:user][:description]
@user.display_name = params[:user][:display_name] @user.display_name = params[:user][:display_name]
@ -208,7 +208,7 @@ class UserController < ApplicationController
@title = t 'user.lost_password.title' @title = t 'user.lost_password.title'
if params[:user] and params[:user][:email] if params[:user] and params[:user][:email]
user = User.find_by_email(params[:user][:email], :conditions => {:status => ["pending", "active", "confirmed"]}) user = User.where(:email => params[:user][:email], :status => ["pending", "active", "confirmed"]).first
if user if user
token = user.tokens.create token = user.tokens.create
@ -410,7 +410,7 @@ class UserController < ApplicationController
def make_friend def make_friend
if params[:display_name] if params[:display_name]
name = params[:display_name] name = params[:display_name]
new_friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]}) new_friend = User.where(:display_name => name, :status => ["active", "confirmed"]).first
friend = Friend.new friend = Friend.new
friend.user_id = @user.id friend.user_id = @user.id
friend.friend_user_id = new_friend.id friend.friend_user_id = new_friend.id
@ -436,7 +436,7 @@ class UserController < ApplicationController
def remove_friend def remove_friend
if params[:display_name] if params[:display_name]
name = params[:display_name] name = params[:display_name]
friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]}) friend = User.where(:display_name => name, :status => ["active", "confirmed"]).first
if @user.is_friends_with?(friend) if @user.is_friends_with?(friend)
Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}" Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name

View file

@ -60,7 +60,7 @@ class WayController < ApplicationController
end end
def full def full
way = Way.find(params[:id], :include => {:nodes => :node_tags}) way = Way.includes(:nodes => :node_tags).find(params[:id])
if way.visible if way.visible
changeset_cache = {} changeset_cache = {}
@ -105,9 +105,7 @@ class WayController < ApplicationController
# :id parameter. note that this used to return deleted ways as well, but # :id parameter. note that this used to return deleted ways as well, but
# this seemed not to be the expected behaviour, so it was removed. # this seemed not to be the expected behaviour, so it was removed.
def ways_for_node def ways_for_node
wayids = WayNode.find(:all, wayids = WayNode.where(:node_id => params[:id]).collect { |ws| ws.id[0] }.uniq
:conditions => ['node_id = ?', params[:id]]
).collect { |ws| ws.id[0] }.uniq
doc = OSM::API.new.get_xml_doc doc = OSM::API.new.get_xml_doc

View file

@ -1,15 +1,5 @@
class Acl < ActiveRecord::Base class Acl < ActiveRecord::Base
def self.find_by_address(address, options) scope :address, lambda { |address| where("#{inet_aton} & netmask = address", address) }
self.with_scope(:find => {:conditions => ["#{inet_aton} & netmask = address", address]}) do
return self.find(:first, options)
end
end
def self.find_all_by_address(address, options)
self.with_scope(:find => {:conditions => ["#{inet_aton} & netmask = address", address]}) do
return self.find(:all, options)
end
end
private private

View file

@ -30,6 +30,9 @@ class Node < ActiveRecord::Base
validate :validate_position validate :validate_position
validates_associated :changeset validates_associated :changeset
scope :visible, where(:visible => true)
scope :invisible, where(:visible => false)
# Sanity check the latitude and longitude and add an error if it's broken # Sanity check the latitude and longitude and add an error if it's broken
def validate_position def validate_position
errors.add_to_base("Node is not in the world") unless in_world? errors.add_to_base("Node is not in the world") unless in_world?

View file

@ -23,6 +23,12 @@ class Relation < ActiveRecord::Base
validates_numericality_of :changeset_id, :version, :integer_only => true validates_numericality_of :changeset_id, :version, :integer_only => true
validates_associated :changeset validates_associated :changeset
scope :visible, where(:visible => true)
scope :invisible, where(:visible => false)
scope :nodes, lambda { |*ids| joins(:relation_members).where(:current_relation_members => { :member_type => "Node", :member_id => ids }) }
scope :ways, lambda { |*ids| joins(:relation_members).where(:current_relation_members => { :member_type => "Way", :member_id => ids }) }
scope :relations, lambda { |*ids| joins(:relation_members).where(:current_relation_members => { :member_type => "Relation", :member_id => ids }) }
TYPES = ["node", "way", "relation"] TYPES = ["node", "way", "relation"]
def self.from_xml(xml, create=false) def self.from_xml(xml, create=false)
@ -148,36 +154,6 @@ class Relation < ActiveRecord::Base
return el1 return el1
end end
def self.find_for_nodes(ids, options = {})
if ids.empty?
return []
else
self.with_scope(:find => { :joins => "INNER JOIN current_relation_members AS crm ON crm.id = current_relations.id", :conditions => "crm.member_type = 'Node' AND crm.member_id IN (#{ids.join(',')})" }) do
return self.find(:all, options)
end
end
end
def self.find_for_ways(ids, options = {})
if ids.empty?
return []
else
self.with_scope(:find => { :joins => "INNER JOIN current_relation_members AS crm ON crm.id = current_relations.id", :conditions => "crm.member_type = 'Way' AND crm.member_id IN (#{ids.join(',')})" }) do
return self.find(:all, options)
end
end
end
def self.find_for_relations(ids, options = {})
if ids.empty?
return []
else
self.with_scope(:find => { :joins => "INNER JOIN current_relation_members AS crm ON crm.id = current_relations.id", :conditions => "crm.member_type = 'Relation' AND crm.member_id IN (#{ids.join(',')})" }) do
return self.find(:all, options)
end
end
end
# FIXME is this really needed? # FIXME is this really needed?
def members def members
unless @members unless @members

View file

@ -25,6 +25,9 @@ class Way < ActiveRecord::Base
validates_numericality_of :id, :on => :update, :integer_only => true validates_numericality_of :id, :on => :update, :integer_only => true
validates_associated :changeset validates_associated :changeset
scope :visible, where(:visible => true)
scope :invisible, where(:visible => false)
# Read in xml as text and return it's Way object representation # Read in xml as text and return it's Way object representation
def self.from_xml(xml, create=false) def self.from_xml(xml, create=false)
begin begin

View file

@ -14,7 +14,7 @@
</tr> </tr>
<tr valign="top"> <tr valign="top">
<td class="fieldName"><%= t 'diary_entry.edit.language' -%></td> <td class="fieldName"><%= t 'diary_entry.edit.language' -%></td>
<td><%= f.collection_select :language_code, Language.find(:all, :order => :english_name), :code, :name %></td> <td><%= f.collection_select :language_code, Language.order(:english_name), :code, :name %></td>
</tr> </tr>
<tr valign="top"> <tr valign="top">
<td class="fieldName"><%= t 'diary_entry.edit.location' -%></td> <td class="fieldName"><%= t 'diary_entry.edit.location' -%></td>

View file

@ -1,6 +1,6 @@
<h1><%= t 'user.new.heading' %></h1> <h1><%= t 'user.new.heading' %></h1>
<% if Acl.find_by_address(request.remote_ip, :conditions => {:k => "no_account_creation"}) %> <% if Acl.address(request.remote_ip).where(:k => "no_account_creation").exists? %>
<p><%= t 'user.new.no_auto_account_create' %></p> <p><%= t 'user.new.no_auto_account_create' %></p>

View file

@ -5,7 +5,7 @@ module GeoRecord
SCALE = 10000000 SCALE = 10000000
def self.included(base) def self.included(base)
base.extend(ClassMethods) base.scope :bbox, lambda { |minlat,minlon,maxlat,maxlon| base.where(OSM.sql_for_area(minlat, minlon, maxlat, maxlon)) }
base.before_save :update_tile base.before_save :update_tile
end end
@ -44,13 +44,5 @@ private
def lat2y(a) def lat2y(a)
180/Math::PI * Math.log(Math.tan(Math::PI/4+a*(Math::PI/180)/2)) 180/Math::PI * Math.log(Math.tan(Math::PI/4+a*(Math::PI/180)/2))
end end
module ClassMethods
def find_by_area(minlat, minlon, maxlat, maxlon, options)
self.with_scope(:find => {:conditions => OSM.sql_for_area(minlat, minlon, maxlat, maxlon)}) do
return self.find(:all, options)
end
end
end
end end