From 9ab2471a9280f0029b63b831ffa4b319ec54b1bf Mon Sep 17 00:00:00 2001 From: Navin Date: Thu, 15 Aug 2013 17:52:18 +0200 Subject: [PATCH 1/2] Fix rails4 deprecation warnings That appear when running topic_spec.rb --- app/models/category.rb | 10 +++++++++- lib/topic_query.rb | 12 ++++++------ lib/trashable.rb | 2 +- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/app/models/category.rb b/app/models/category.rb index 853d94ddfde..942fa06a5aa 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -1,9 +1,17 @@ class Category < ActiveRecord::Base belongs_to :topic, dependent: :destroy - belongs_to :topic_only_relative_url, + if rails4? + belongs_to :topic_only_relative_url, + -> { select "id, title, slug" }, + class_name: "Topic", + foreign_key: "topic_id" + else + belongs_to :topic_only_relative_url, select: "id, title, slug", class_name: "Topic", foreign_key: "topic_id" + end + belongs_to :user has_many :topics diff --git a/lib/topic_query.rb b/lib/topic_query.rb index 58d6abef0fc..eea9926c686 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -212,23 +212,23 @@ class TopicQuery end result = result.listable_topics.includes(category: :topic_only_relative_url) - result = result.where('categories.name is null or categories.name <> ?', options[:exclude_category]) if options[:exclude_category] - result = result.where('categories.name = ?', options[:only_category]) if options[:only_category] + result = (rails4? ? result.where('categories.name is null or categories.name <> ?', options[:exclude_category]).references(:categories) : result.where('categories.name is null or categories.name <> ?', options[:exclude_category])) if options[:exclude_category] + result = (rails4? ? result.where('categories.name = ?', options[:only_category]).references(:categories) : result.where('categories.name = ?', options[:only_category])) if options[:only_category] result = result.limit(options[:per_page]) unless options[:limit] == false result = result.visible if options[:visible] || @user.nil? || @user.regular? - result = result.where('topics.id <> ?', options[:except_topic_id]) if options[:except_topic_id] + result = (rails4? ? result.where('topics.id <> ?', options[:except_topic_id]).references(:topics) : result.where('topics.id <> ?', options[:except_topic_id])) if options[:except_topic_id] result = result.offset(options[:page].to_i * options[:per_page]) if options[:page] if options[:topic_ids] - result = result.where('topics.id in (?)', options[:topic_ids]) + result = rails4? ? result.where('topics.id in (?)', options[:topic_ids]).references(:topics) : result.where('topics.id in (?)', options[:topic_ids]) end unless @user && @user.moderator? category_ids = @user.secure_category_ids if @user if category_ids.present? - result = result.where('categories.read_restricted IS NULL OR categories.read_restricted = ? OR categories.id IN (?)', false, category_ids) + result = rails4? ? result.where('categories.read_restricted IS NULL OR categories.read_restricted = ? OR categories.id IN (?)', false, category_ids).references(:categories) : result.where('categories.read_restricted IS NULL OR categories.read_restricted = ? OR categories.id IN (?)', false, category_ids) else - result = result.where('categories.read_restricted IS NULL OR categories.read_restricted = ?', false) + result = rails4? ? result.where('categories.read_restricted IS NULL OR categories.read_restricted = ?', false).references(:categories) : result.where('categories.read_restricted IS NULL OR categories.read_restricted = ?', false) end end diff --git a/lib/trashable.rb b/lib/trashable.rb index ef6fd724373..0dc1aef274b 100644 --- a/lib/trashable.rb +++ b/lib/trashable.rb @@ -2,7 +2,7 @@ module Trashable extend ActiveSupport::Concern included do - default_scope where(with_deleted_scope_sql) + default_scope { where(with_deleted_scope_sql) } # scope unscoped does not work belongs_to :deleted_by, class_name: 'User' From 2e700dd26d611e3ab388e7cf2317fe276086f4ee Mon Sep 17 00:00:00 2001 From: Navin Date: Fri, 16 Aug 2013 14:53:40 +0200 Subject: [PATCH 2/2] Patch ActiveRecord::Relation#references and clean up --- lib/freedom_patches/active_record_relation.rb | 14 ++++++++++++++ lib/search.rb | 13 +++---------- lib/topic_query.rb | 12 ++++++------ lib/trashable.rb | 7 ++----- 4 files changed, 25 insertions(+), 21 deletions(-) create mode 100644 lib/freedom_patches/active_record_relation.rb diff --git a/lib/freedom_patches/active_record_relation.rb b/lib/freedom_patches/active_record_relation.rb new file mode 100644 index 00000000000..ab5cb77eafa --- /dev/null +++ b/lib/freedom_patches/active_record_relation.rb @@ -0,0 +1,14 @@ +unless Rails.version =~ /^4/ + module ActiveRecord + class Relation + # Patch Rails 3 ActiveRecord::Relation to noop on Rails 4 references + # thereby getting code that works for rails 3 and 4 without + # deprecation warnings + + def references(*args) + self + end + + end + end +end diff --git a/lib/search.rb b/lib/search.rb index 3d6b7001660..0d356323425 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -118,9 +118,7 @@ class Search .order("topics_month DESC") .secured(@guardian) .limit(@limit) - if rails4? - categories = categories.references(:category_search_data) - end + .references(:category_search_data) categories.each do |c| @results.add_result(SearchResult.from_category(c)) @@ -133,9 +131,7 @@ class Search .order("CASE WHEN username_lower = '#{@original_term.downcase}' THEN 0 ELSE 1 END") .order("last_posted_at DESC") .limit(@limit) - if rails4? - users = users.references(:user_search_data) - end + .references(:user_search_data) users.each do |u| @results.add_result(SearchResult.from_user(u)) @@ -148,10 +144,7 @@ class Search .where("topics.deleted_at" => nil) .where("topics.visible") .where("topics.archetype <> ?", Archetype.private_message) - - if rails4? - posts = posts.references(:post_search_data, {:topic => :category}) - end + .references(:post_search_data, {:topic => :category}) # If we have a search context, prioritize those posts first if @search_context.present? diff --git a/lib/topic_query.rb b/lib/topic_query.rb index eea9926c686..571f49fcd6e 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -212,23 +212,23 @@ class TopicQuery end result = result.listable_topics.includes(category: :topic_only_relative_url) - result = (rails4? ? result.where('categories.name is null or categories.name <> ?', options[:exclude_category]).references(:categories) : result.where('categories.name is null or categories.name <> ?', options[:exclude_category])) if options[:exclude_category] - result = (rails4? ? result.where('categories.name = ?', options[:only_category]).references(:categories) : result.where('categories.name = ?', options[:only_category])) if options[:only_category] + result = result.where('categories.name is null or categories.name <> ?', options[:exclude_category]).references(:categories) if options[:exclude_category] + result = result.where('categories.name = ?', options[:only_category]).references(:categories) if options[:only_category] result = result.limit(options[:per_page]) unless options[:limit] == false result = result.visible if options[:visible] || @user.nil? || @user.regular? - result = (rails4? ? result.where('topics.id <> ?', options[:except_topic_id]).references(:topics) : result.where('topics.id <> ?', options[:except_topic_id])) if options[:except_topic_id] + result = result.where('topics.id <> ?', options[:except_topic_id]).references(:topics) if options[:except_topic_id] result = result.offset(options[:page].to_i * options[:per_page]) if options[:page] if options[:topic_ids] - result = rails4? ? result.where('topics.id in (?)', options[:topic_ids]).references(:topics) : result.where('topics.id in (?)', options[:topic_ids]) + result = result.where('topics.id in (?)', options[:topic_ids]).references(:topics) end unless @user && @user.moderator? category_ids = @user.secure_category_ids if @user if category_ids.present? - result = rails4? ? result.where('categories.read_restricted IS NULL OR categories.read_restricted = ? OR categories.id IN (?)', false, category_ids).references(:categories) : result.where('categories.read_restricted IS NULL OR categories.read_restricted = ? OR categories.id IN (?)', false, category_ids) + result = result.where('categories.read_restricted IS NULL OR categories.read_restricted = ? OR categories.id IN (?)', false, category_ids).references(:categories) else - result = rails4? ? result.where('categories.read_restricted IS NULL OR categories.read_restricted = ?', false).references(:categories) : result.where('categories.read_restricted IS NULL OR categories.read_restricted = ?', false) + result = result.where('categories.read_restricted IS NULL OR categories.read_restricted = ?', false).references(:categories) end end diff --git a/lib/trashable.rb b/lib/trashable.rb index 0dc1aef274b..5d0e74b2850 100644 --- a/lib/trashable.rb +++ b/lib/trashable.rb @@ -15,11 +15,8 @@ module Trashable # # with this in place Post.limit(10).with_deleted, will work as expected # - if rails4? - scope = self.all.with_default_scope - else - scope = self.scoped.with_default_scope - end + scope = rails4? ? self.all.with_default_scope : self.scoped.with_default_scope + scope.where_values.delete(with_deleted_scope_sql) scope end