From: Tom Hughes Date: Thu, 9 Sep 2010 09:56:19 +0000 (+0100) Subject: Update ActiveRecord queries to use arel X-Git-Tag: live~6099 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/226c41be692452129227f30d67f2d847e510015c Update ActiveRecord queries to use arel --- diff --git a/app/controllers/amf_controller.rb b/app/controllers/amf_controller.rb index 534e41a80..e776c8d8b 100644 --- a/app/controllers/amf_controller.rb +++ b/app/controllers/amf_controller.rb @@ -314,7 +314,7 @@ class AmfController < ApplicationController relations = sql_find_relations_in_area_and_ways(xmin, ymin, xmax, ymax, ways.collect {|x| x[0]}) else # 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| visible_ways = node.ways.select { |w| w.visible? } 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 # find the relations used by those nodes and ways - relations = Relation.find_for_nodes(nodes_in_area.collect { |n| n.id }, :conditions => {:visible => true}) + - Relation.find_for_ways(ways.collect { |w| w[0] }, :conditions => {:visible => true}) + relations = Relation.nodes(nodes_in_area.collect { |n| n.id }).visible + + Relation.ways(ways.collect { |w| w[0] }).visible relations = relations.collect { |relation| [relation.id,relation.version] }.uniq end @@ -348,8 +348,8 @@ class AmfController < ApplicationController # see /config/application.yml 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) - way_ids = nodes_in_area.collect { |node| node.ways_via_history_ids }.flatten.uniq + 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.invisible.collect { |way| way.id } }.flatten.uniq [0,'',way_ids] end diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb index 95cc88869..35fc86c52 100644 --- a/app/controllers/api_controller.rb +++ b/app/controllers/api_controller.rb @@ -39,7 +39,7 @@ class ApiController < ApplicationController end # 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.encoding = XML::Encoding::UTF_8 @@ -145,7 +145,7 @@ class ApiController < ApplicationController end # 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 # need to be @nodes (instance var) so tests in /spec can be performed #@nodes = Node.search(bbox, params[:tag]) @@ -191,7 +191,7 @@ class ApiController < ApplicationController nodes_to_fetch = (list_of_way_nodes.uniq - node_ids) - [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 visible_nodes = {} @@ -213,15 +213,15 @@ class ApiController < ApplicationController end end - relations = Relation.find_for_nodes(visible_nodes.keys, :conditions => {:visible => true}) + - Relation.find_for_ways(way_ids, :conditions => {:visible => true}) + relations = Relation.nodes(visible_nodes.keys).visible + + Relation.ways(way_ids).visible # 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 # another way B also referenced, that is not returned. But we do make # 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) - 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 # 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 mask = (1 << zoom) - 1 - tiles = Node.count(:conditions => ["timestamp BETWEEN ? AND ?", starttime, endtime], - :group => "maptile_for_point(latitude, longitude, #{zoom})") + tiles = Node.where(:timestamp => starttime .. endtime).group("maptile_for_point(latitude, longitude, #{zoom})").count doc = OSM::API.new.get_xml_doc changes = XML::Node.new 'changes' diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 904388b9d..5018dcc42 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -11,7 +11,7 @@ class ApplicationController < ActionController::Base def authorize_web 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" session.delete(:user) diff --git a/app/controllers/changeset_controller.rb b/app/controllers/changeset_controller.rb index 8789476db..b580ea786 100644 --- a/app/controllers/changeset_controller.rb +++ b/app/controllers/changeset_controller.rb @@ -177,9 +177,7 @@ class ChangesetController < ApplicationController created << elt.to_xml_node else # get the previous version from the element history - prev_elt = elt.class.find(:first, :conditions => - ['id = ? and version = ?', - elt.id, elt.version]) + prev_elt = elt.class.where(:id => elt.id, :version => elt.version).first unless elt.visible # if the element isn't visible then it must have been deleted, so # output the *previous* XML diff --git a/app/controllers/diary_entry_controller.rb b/app/controllers/diary_entry_controller.rb index 47c638f3a..0946c0136 100644 --- a/app/controllers/diary_entry_controller.rb +++ b/app/controllers/diary_entry_controller.rb @@ -20,7 +20,7 @@ class DiaryEntryController < ApplicationController @diary_entry.user = @user 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 default_lang.v = @diary_entry.language_code default_lang.save! @@ -32,7 +32,7 @@ class DiaryEntryController < ApplicationController render :action => 'edit' end 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 @diary_entry = DiaryEntry.new(:language_code => lang_code) render :action => 'edit' @@ -71,7 +71,7 @@ class DiaryEntryController < ApplicationController def list 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 @title = t 'diary_entry.list.user_title', :user => @this_user.display_name @@ -111,17 +111,13 @@ class DiaryEntryController < ApplicationController end def rss + @entries = DiaryEntry.includes(:user).order("created_at DESC").limit(20) + 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 - @entries = DiaryEntry.find(:all, - :conditions => { - :user_id => user.id, - :visible => true - }, - :order => 'created_at DESC', - :limit => 20) + @entries = @entries.where(:user_id => user.id, :visible => true ) @title = I18n.t('diary_entry.feed.user.title', :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" @@ -129,25 +125,15 @@ class DiaryEntryController < ApplicationController render :nothing => true, :status => :not_found end elsif params[:language] - @entries = DiaryEntry.find(:all, :include => :user, - :conditions => { - :users => { :status => ["active", "confirmed"] }, - :visible => true, - :language_code => params[:language] - }, - :order => 'created_at DESC', - :limit => 20) + @entries = @entries.where(:users => { :status => ["active", "confirmed"] }, + :visible => true, + :language_code => params[:language]) @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) @link = "http://#{SERVER_URL}/diary/#{params[:language]}" else - @entries = DiaryEntry.find(:all, :include => :user, - :conditions => { - :users => { :status => ["active", "confirmed"] }, - :visible => true - }, - :order => 'created_at DESC', - :limit => 20) + @entries = @entries.where(:users => { :status => ["active", "confirmed"] }, + :visible => true) @title = I18n.t('diary_entry.feed.all.title') @description = I18n.t('diary_entry.feed.all.description') @link = "http://#{SERVER_URL}/diary" @@ -155,14 +141,12 @@ class DiaryEntryController < ApplicationController end 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 - @entry = DiaryEntry.find(:first, :conditions => { - :id => params[:id], - :user_id => user.id, - :visible => true - }) + @entry = DiaryEntry.where(:id => params[:id], + :user_id => user.id, + :visible => true).first if @entry @title = t 'diary_entry.view.title', :user => params[:display_name], :title => @entry.title else diff --git a/app/controllers/message_controller.rb b/app/controllers/message_controller.rb index d97ca1bef..6e0c717b5 100644 --- a/app/controllers/message_controller.rb +++ b/app/controllers/message_controller.rb @@ -15,7 +15,7 @@ class MessageController < ApplicationController @to_user = User.find_by_display_name(params[:display_name]) if @to_user 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' else @message = Message.new(params[:message]) @@ -103,7 +103,7 @@ class MessageController < ApplicationController def mark if 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' message_read = false notice = t 'message.mark.as_unread' @@ -134,7 +134,7 @@ class MessageController < ApplicationController def delete if 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.to_user_visible = false if message.recipient == @user if message.save diff --git a/app/controllers/old_node_controller.rb b/app/controllers/old_node_controller.rb index c20dacf48..ea443216c 100644 --- a/app/controllers/old_node_controller.rb +++ b/app/controllers/old_node_controller.rb @@ -18,18 +18,15 @@ class OldNodeController < ApplicationController end def version - old_node = OldNode.find(:first, :conditions => {:id => params[:id], :version => params[:version]} ) - if old_node.nil? - # (RecordNotFound is not raised with find :first...) + if old_node = OldNode.where(:id => params[:id], :version => params[:version]).first + 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" + else 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" end end diff --git a/app/controllers/old_relation_controller.rb b/app/controllers/old_relation_controller.rb index eb274c4de..3f4b884f6 100644 --- a/app/controllers/old_relation_controller.rb +++ b/app/controllers/old_relation_controller.rb @@ -17,18 +17,15 @@ class OldRelationController < ApplicationController end def version - old_relation = OldRelation.find(:first, :conditions => {:id => params[:id], :version => params[:version]} ) - if old_relation.nil? - # (RecordNotFound is not raised with find :first...) + if old_relation = OldRelation.where(:id => params[:id], :version => params[:version]).first + 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" + else render :nothing => true, :status => :not_found - return 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 diff --git a/app/controllers/old_way_controller.rb b/app/controllers/old_way_controller.rb index 96c88d492..ac1c6cd40 100644 --- a/app/controllers/old_way_controller.rb +++ b/app/controllers/old_way_controller.rb @@ -18,18 +18,15 @@ class OldWayController < ApplicationController end def version - old_way = OldWay.find(:first, :conditions => {:id => params[:id], :version => params[:version]} ) - if old_way.nil? - # (RecordNotFound is not raised with find :first...) + if old_way = OldWay.where(:id => params[:id], :version => params[:version]).first + 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" + else render :nothing => true, :status => :not_found - return 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 diff --git a/app/controllers/relation_controller.rb b/app/controllers/relation_controller.rb index 390806f80..db68847e5 100644 --- a/app/controllers/relation_controller.rb +++ b/app/controllers/relation_controller.rb @@ -81,8 +81,8 @@ class RelationController < ApplicationController # next load the relations and the ways. - relations = Relation.find(relation_ids, :include => [:relation_tags]) - ways = Way.find(way_ids, :include => [:way_nodes, :way_tags]) + relations = Relation.where(:id => relation_ids).includes(:relation_tags) + ways = Way.where(:id => way_ids).includes(:way_nodes, :way_tags) # now additionally collect nodes referenced by ways. Note how we # recursively evaluate ways but NOT relations. @@ -91,7 +91,7 @@ class RelationController < ApplicationController way.way_nodes.collect { |way_node| way_node.node_id } } 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. doc = OSM::API.new.get_xml_doc @@ -157,7 +157,7 @@ class RelationController < ApplicationController end 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 diff --git a/app/controllers/trace_controller.rb b/app/controllers/trace_controller.rb index 52392fddd..1381e71eb 100644 --- a/app/controllers/trace_controller.rb +++ b/app/controllers/trace_controller.rb @@ -27,7 +27,7 @@ class TraceController < ApplicationController # from display name, pick up user id if one user's traces only display_name = params[:display_name] 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? @title = t'trace.no_such_user.title' @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 if target_user.nil? # all traces 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 - conditions = ["gpx_files.visibility in ('public', 'identifiable')"] #2 + @traces = Trace.where("visibility IN ('public', 'identifiable')") #2 end else 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 - 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 if 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 - conditions[0] += " AND gpx_files.id IN (#{files.join(',')})" - else - conditions[0] += " AND 0 = 1" + @traces = @traces.where(:id => files.collect { |tt| tt.gpx_id }) end end - conditions[0] += " AND gpx_files.visible = ?" - conditions << true - @page = (params[:page] || 1).to_i @page_size = 20 - @traces = Trace.find(:all, - :include => [:user, :tags], - :conditions => conditions, - :order => "gpx_files.timestamp DESC", - :offset => (@page - 1) * @page_size, - :limit => @page_size) + @traces = @traces.where(:visible => true) + @traces = @traces.order("timestamp DESC") + @traces = @traces.offset((@page - 1) * @page_size) + @traces = @traces.limit(@page_size) + @traces = @traces.includes(:user, :tags) # put together SET of tags across traces, for related links tagset = Hash.new - if @traces - @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.each do |tag| - tagset[tag.tag] = tag.tag - end + @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.each do |tag| + tagset[tag.tag] = tag.tag end end @@ -222,20 +214,19 @@ class TraceController < ApplicationController end def georss - conditions = ["gpx_files.visibility in ('public', 'identifiable')"] + traces = Trace.where(:visibility => [:public, :identifiable]) if params[:display_name] - conditions[0] += " AND users.display_name = ?" - conditions << params[:display_name] + traces = traces.where(:users => {:display_name => params[:display_name]}) end if params[:tag] - conditions[0] += " AND EXISTS (SELECT * FROM gpx_file_tags AS gft WHERE gft.gpx_id = gpx_files.id AND gft.tag = ?)" - conditions << params[:tag] + traces = traces.where("EXISTS (SELECT * FROM gpx_file_tags AS gft WHERE gft.gpx_id = gpx_files.id AND gft.tag = ?)") end - traces = Trace.find(:all, :include => :user, :conditions => conditions, - :order => "timestamp DESC", :limit => 20) + traces = traces.order("timestamp DESC") + traces = traces.limit(20) + traces = traces.includes(:user) rss = OSM::GeoRSS.new @@ -423,7 +414,7 @@ private end # 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.save else @@ -441,11 +432,11 @@ private end def default_visibility - visibility = @user.preferences.find(:first, :conditions => {:k => "gps.trace.visibility"}) + visibility = @user.preferences.where(:k => "gps.trace.visibility").first if visibility 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" else "public" diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 52573cf5c..9bc18c16b 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -80,7 +80,7 @@ class UserController < ApplicationController def save @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' elsif params[:decline] if @user @@ -139,7 +139,7 @@ class UserController < ApplicationController def account @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] @user.display_name = params[:user][:display_name] @@ -208,7 +208,7 @@ class UserController < ApplicationController @title = t 'user.lost_password.title' 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 token = user.tokens.create @@ -410,7 +410,7 @@ class UserController < ApplicationController def make_friend if 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.user_id = @user.id friend.friend_user_id = new_friend.id @@ -436,7 +436,7 @@ class UserController < ApplicationController def remove_friend if 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) Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}" flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name diff --git a/app/controllers/way_controller.rb b/app/controllers/way_controller.rb index f2cda21bc..f19d6db7d 100644 --- a/app/controllers/way_controller.rb +++ b/app/controllers/way_controller.rb @@ -60,7 +60,7 @@ class WayController < ApplicationController end def full - way = Way.find(params[:id], :include => {:nodes => :node_tags}) + way = Way.includes(:nodes => :node_tags).find(params[:id]) if way.visible changeset_cache = {} @@ -105,9 +105,7 @@ class WayController < ApplicationController # :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. def ways_for_node - wayids = WayNode.find(:all, - :conditions => ['node_id = ?', params[:id]] - ).collect { |ws| ws.id[0] }.uniq + wayids = WayNode.where(:node_id => params[:id]).collect { |ws| ws.id[0] }.uniq doc = OSM::API.new.get_xml_doc diff --git a/app/models/acl.rb b/app/models/acl.rb index 3ff19d35f..94e377343 100644 --- a/app/models/acl.rb +++ b/app/models/acl.rb @@ -1,15 +1,5 @@ class Acl < ActiveRecord::Base - def self.find_by_address(address, options) - 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 + scope :address, lambda { |address| where("#{inet_aton} & netmask = address", address) } private diff --git a/app/models/node.rb b/app/models/node.rb index 1aff4101e..3de6e154c 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -30,6 +30,9 @@ class Node < ActiveRecord::Base validate :validate_position 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 def validate_position errors.add_to_base("Node is not in the world") unless in_world? diff --git a/app/models/relation.rb b/app/models/relation.rb index e3ba69b56..dd4d49258 100644 --- a/app/models/relation.rb +++ b/app/models/relation.rb @@ -23,6 +23,12 @@ class Relation < ActiveRecord::Base validates_numericality_of :changeset_id, :version, :integer_only => true 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"] def self.from_xml(xml, create=false) @@ -148,36 +154,6 @@ class Relation < ActiveRecord::Base return el1 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? def members unless @members diff --git a/app/models/way.rb b/app/models/way.rb index cc9343e7c..8206eecb3 100644 --- a/app/models/way.rb +++ b/app/models/way.rb @@ -25,6 +25,9 @@ class Way < ActiveRecord::Base validates_numericality_of :id, :on => :update, :integer_only => true 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 def self.from_xml(xml, create=false) begin diff --git a/app/views/diary_entry/edit.html.erb b/app/views/diary_entry/edit.html.erb index 8728427be..c0a231ab2 100644 --- a/app/views/diary_entry/edit.html.erb +++ b/app/views/diary_entry/edit.html.erb @@ -14,7 +14,7 @@ <%= t 'diary_entry.edit.language' -%> - <%= f.collection_select :language_code, Language.find(:all, :order => :english_name), :code, :name %> + <%= f.collection_select :language_code, Language.order(:english_name), :code, :name %> <%= t 'diary_entry.edit.location' -%> diff --git a/app/views/user/new.html.erb b/app/views/user/new.html.erb index 9fcbfdcba..04c86b5a8 100644 --- a/app/views/user/new.html.erb +++ b/app/views/user/new.html.erb @@ -1,6 +1,6 @@

<%= t 'user.new.heading' %>

-<% 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? %>

<%= t 'user.new.no_auto_account_create' %>

diff --git a/lib/geo_record.rb b/lib/geo_record.rb index 0728a6aa1..d338f1b66 100644 --- a/lib/geo_record.rb +++ b/lib/geo_record.rb @@ -5,7 +5,7 @@ module GeoRecord SCALE = 10000000 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 end @@ -44,13 +44,5 @@ private def lat2y(a) 180/Math::PI * Math.log(Math.tan(Math::PI/4+a*(Math::PI/180)/2)) 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