From ca30b879f6eba3f513bcfa53545dba43d09b2023 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Thu, 27 Jun 2013 00:35:56 +0100 Subject: [PATCH] Eliminate deprecated scope and association arguments --- app/models/access_token.rb | 2 +- app/models/diary_entry.rb | 16 ++++------------ app/models/node.rb | 6 +++--- app/models/note.rb | 5 +---- app/models/oauth_token.rb | 2 +- app/models/old_relation.rb | 2 +- app/models/relation.rb | 14 +++++++------- app/models/trace.rb | 8 ++++---- app/models/user.rb | 24 ++++++++++++------------ app/models/way.rb | 10 +++++----- lib/geo_record.rb | 2 +- lib/redactable.rb | 2 +- 12 files changed, 41 insertions(+), 52 deletions(-) diff --git a/app/models/access_token.rb b/app/models/access_token.rb index a1888343a..3aa6f9daf 100644 --- a/app/models/access_token.rb +++ b/app/models/access_token.rb @@ -2,7 +2,7 @@ class AccessToken < OauthToken belongs_to :user belongs_to :client_application - scope :valid, where(:invalidated_at => nil) + scope :valid, -> { where(:invalidated_at => nil) } validates_presence_of :user, :secret diff --git a/app/models/diary_entry.rb b/app/models/diary_entry.rb index f7584c637..58f8710f9 100644 --- a/app/models/diary_entry.rb +++ b/app/models/diary_entry.rb @@ -2,18 +2,10 @@ class DiaryEntry < ActiveRecord::Base belongs_to :user, :counter_cache => true belongs_to :language, :foreign_key => 'language_code' - has_many :comments, :class_name => "DiaryComment", - :include => :user, - :order => "diary_comments.id" - has_many :visible_comments, :class_name => "DiaryComment", - :include => :user, - :conditions => { - :users => { :status => ["active", "confirmed" ] }, - :visible => true - }, - :order => "diary_comments.id" - - scope :visible, where(:visible => true) + has_many :comments, -> { order(:id).preload(:user) }, :class_name => "DiaryComment" + has_many :visible_comments, -> { joins(:user).where(:visible => true, :users => { :status => ["active", "confirmed"] }).order(:id) }, :class_name => "DiaryComment" + + scope :visible, -> { where(:visible => true) } validates_presence_of :title, :body validates_length_of :title, :within => 1..255 diff --git a/app/models/node.rb b/app/models/node.rb index 81b910f37..e592648de 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -9,7 +9,7 @@ class Node < ActiveRecord::Base belongs_to :changeset - has_many :old_nodes, :order => :version + has_many :old_nodes, -> { order(:version) } has_many :way_nodes has_many :ways, :through => :way_nodes @@ -31,8 +31,8 @@ class Node < ActiveRecord::Base validate :validate_position validates_associated :changeset - scope :visible, where(:visible => true) - scope :invisible, where(:visible => false) + 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 diff --git a/app/models/note.rb b/app/models/note.rb index 67222191d..9dc122721 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -1,10 +1,7 @@ class Note < ActiveRecord::Base include GeoRecord - has_many :comments, :class_name => "NoteComment", - :foreign_key => :note_id, - :order => :created_at, - :conditions => { :visible => true } + has_many :comments, -> { where(:visible => true).order(:created_at) }, :class_name => "NoteComment", :foreign_key => :note_id validates_presence_of :id, :on => :update validates_uniqueness_of :id diff --git a/app/models/oauth_token.rb b/app/models/oauth_token.rb index f9255e56c..0fe0bea30 100644 --- a/app/models/oauth_token.rb +++ b/app/models/oauth_token.rb @@ -2,7 +2,7 @@ class OauthToken < ActiveRecord::Base belongs_to :client_application belongs_to :user - scope :authorized, where("authorized_at IS NOT NULL and invalidated_at IS NULL") + scope :authorized, -> { where("authorized_at IS NOT NULL and invalidated_at IS NULL") } validates_uniqueness_of :token validates_presence_of :client_application, :token diff --git a/app/models/old_relation.rb b/app/models/old_relation.rb index b2e86c930..890ba1eed 100644 --- a/app/models/old_relation.rb +++ b/app/models/old_relation.rb @@ -12,7 +12,7 @@ class OldRelation < ActiveRecord::Base belongs_to :redaction belongs_to :current_relation, :class_name => "Relation", :foreign_key => "relation_id" - has_many :old_members, :class_name => 'OldRelationMember', :foreign_key => [:relation_id, :version], :order => :sequence_id + has_many :old_members, -> { order(:sequence_id) }, :class_name => 'OldRelationMember', :foreign_key => [:relation_id, :version] has_many :old_tags, :class_name => 'OldRelationTag', :foreign_key => [:relation_id, :version] validates_associated :changeset diff --git a/app/models/relation.rb b/app/models/relation.rb index e402d0d08..581dce16f 100644 --- a/app/models/relation.rb +++ b/app/models/relation.rb @@ -8,9 +8,9 @@ class Relation < ActiveRecord::Base belongs_to :changeset - has_many :old_relations, :order => 'version' + has_many :old_relations, -> { order(:version) } - has_many :relation_members, :order => 'sequence_id' + has_many :relation_members, -> { order(:sequence_id) } has_many :relation_tags has_many :containing_relation_members, :class_name => "RelationMember", :as => :member @@ -24,11 +24,11 @@ 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 }) } + scope :visible, -> { where(:visible => true) } + scope :invisible, -> { where(:visible => false) } + scope :nodes, ->(*ids) { joins(:relation_members).where(:current_relation_members => { :member_type => "Node", :member_id => ids.flatten }) } + scope :ways, ->(*ids) { joins(:relation_members).where(:current_relation_members => { :member_type => "Way", :member_id => ids.flatten }) } + scope :relations, ->(*ids) { joins(:relation_members).where(:current_relation_members => { :member_type => "Relation", :member_id => ids.flatten }) } TYPES = ["node", "way", "relation"] diff --git a/app/models/trace.rb b/app/models/trace.rb index 1553c894b..5394ca01d 100644 --- a/app/models/trace.rb +++ b/app/models/trace.rb @@ -5,10 +5,10 @@ class Trace < ActiveRecord::Base has_many :tags, :class_name => 'Tracetag', :foreign_key => 'gpx_id', :dependent => :delete_all has_many :points, :class_name => 'Tracepoint', :foreign_key => 'gpx_id', :dependent => :delete_all - scope :visible, where(:visible => true) - scope :visible_to, lambda { |u| visible.where("visibility IN ('public', 'identifiable') OR user_id = ?", u) } - scope :public, where(:visibility => ["public", "identifiable"]) - scope :tagged, lambda { |t| joins(:tags).where(:gpx_file_tags => { :tag => t }) } + scope :visible, -> { where(:visible => true) } + scope :visible_to, ->(u) { visible.where("visibility IN ('public', 'identifiable') OR user_id = ?", u) } + scope :public, -> { where(:visibility => ["public", "identifiable"]) } + scope :tagged, ->(t) { joins(:tags).where(:gpx_file_tags => { :tag => t }) } validates_presence_of :user_id, :name, :timestamp validates_presence_of :description, :on => :create diff --git a/app/models/user.rb b/app/models/user.rb index 20f1ba432..89f11a923 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,22 +1,22 @@ class User < ActiveRecord::Base require 'xml/libxml' - has_many :traces, :conditions => { :visible => true } - has_many :diary_entries, :order => 'created_at DESC' - has_many :diary_comments, :order => 'created_at DESC' - has_many :messages, :foreign_key => :to_user_id, :conditions => { :to_user_visible => true }, :order => 'sent_on DESC', :include => [:sender, :recipient] - has_many :new_messages, :class_name => "Message", :foreign_key => :to_user_id, :conditions => { :to_user_visible => true, :message_read => false }, :order => 'sent_on DESC' - has_many :sent_messages, :class_name => "Message", :foreign_key => :from_user_id, :conditions => { :from_user_visible => true }, :order => 'sent_on DESC', :include => [:sender, :recipient] - has_many :friends, :include => :befriendee, :conditions => "users.status IN ('active', 'confirmed')" + has_many :traces, -> { where(:visible => true) } + has_many :diary_entries, -> { order(:created_at => :desc) } + has_many :diary_comments, -> { order(:created_at => :desc) } + has_many :messages, -> { where(:to_user_visible => true).order(:sent_on => :desc).preload(:sender, :recipient) }, :foreign_key => :to_user_id + has_many :new_messages, -> { where(:to_user_visible => true, :message_read => false).order(:sent_on => :desc) }, :class_name => "Message", :foreign_key => :to_user_id + has_many :sent_messages, -> { where(:from_user_visible => true).order(:sent_on => :desc).preload(:sender, :recipient) }, :class_name => "Message", :foreign_key => :from_user_id + has_many :friends, -> { joins(:befriendee).where(:users => { :status => ["active", "confirmed"] }) } has_many :friend_users, :through => :friends, :source => :befriendee has_many :tokens, :class_name => "UserToken" has_many :preferences, :class_name => "UserPreference" - has_many :changesets, :order => 'created_at DESC' + has_many :changesets, -> { order(:created_at => :desc) } has_many :note_comments, :foreign_key => :author_id has_many :notes, :through => :note_comments has_many :client_applications - has_many :oauth_tokens, :class_name => "OauthToken", :order => "authorized_at desc", :include => [:client_application] + has_many :oauth_tokens, -> { order(:authorized_at => :desc).preload(:client_application) }, :class_name => "OauthToken" has_many :blocks, :class_name => "UserBlock" has_many :blocks_created, :class_name => "UserBlock", :foreign_key => :creator_id @@ -24,9 +24,9 @@ class User < ActiveRecord::Base has_many :roles, :class_name => "UserRole" - scope :visible, where(:status => ["pending", "active", "confirmed"]) - scope :active, where(:status => ["active", "confirmed"]) - scope :public, where(:data_public => true) + scope :visible, -> { where(:status => ["pending", "active", "confirmed"]) } + scope :active, -> { where(:status => ["active", "confirmed"]) } + scope :public, -> { where(:data_public => true) } validates_presence_of :email, :display_name validates_confirmation_of :email#, :message => ' addresses must match' diff --git a/app/models/way.rb b/app/models/way.rb index 9bd792ddc..58a9214fa 100644 --- a/app/models/way.rb +++ b/app/models/way.rb @@ -8,10 +8,10 @@ class Way < ActiveRecord::Base belongs_to :changeset - has_many :old_ways, :order => 'version' + has_many :old_ways, -> { order(:version) } - has_many :way_nodes, :order => 'sequence_id' - has_many :nodes, :through => :way_nodes, :order => 'sequence_id' + has_many :way_nodes, -> { order(:sequence_id) } + has_many :nodes, -> { order("sequence_id") }, :through => :way_nodes has_many :way_tags @@ -26,8 +26,8 @@ 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) + 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) diff --git a/lib/geo_record.rb b/lib/geo_record.rb index 0c261dc69..fbfb0c2ce 100644 --- a/lib/geo_record.rb +++ b/lib/geo_record.rb @@ -5,7 +5,7 @@ module GeoRecord SCALE = 10000000 def self.included(base) - base.scope :bbox, lambda { |bbox| base.where(OSM.sql_for_area(bbox)) } + base.scope :bbox, ->(bbox) { base.where(OSM.sql_for_area(bbox)) } base.before_save :update_tile end diff --git a/lib/redactable.rb b/lib/redactable.rb index d8367d7bd..620b2196e 100644 --- a/lib/redactable.rb +++ b/lib/redactable.rb @@ -4,7 +4,7 @@ module Redactable def self.included(base) # this is used to extend activerecord bases, as these aren't # in scope for the module itself. - base.scope :unredacted, base.where(:redaction_id => nil) + base.scope :unredacted, -> { base.where(:redaction_id => nil) } end def redacted? -- 2.39.5