From d3bbd82d8bd446fdfb6a77bc379388b996598624 Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 16 Feb 2020 05:27:51 -0600 Subject: [PATCH] application record: drop execute_sql, select_value_sql methods. --- app/logical/post_query_builder.rb | 4 ++-- app/models/application_record.rb | 10 ---------- app/models/note.rb | 6 +++--- app/models/pool.rb | 8 -------- app/models/post_vote.rb | 9 ++++++--- app/models/tag.rb | 5 ++--- test/unit/note_test.rb | 16 ++++++---------- test/unit/pool_test.rb | 30 ------------------------------ 8 files changed, 19 insertions(+), 69 deletions(-) diff --git a/app/logical/post_query_builder.rb b/app/logical/post_query_builder.rb index 46d1b96ce..b7e056300 100644 --- a/app/logical/post_query_builder.rb +++ b/app/logical/post_query_builder.rb @@ -449,10 +449,10 @@ class PostQueryBuilder relation = add_tag_string_search_relation(q[:tags], relation) if q[:ordpool].present? - pool_id = q[:ordpool].to_i + pool_name = q[:ordpool] # XXX unify with Pool#posts - pool_posts = Pool.joins("CROSS JOIN unnest(pools.post_ids) WITH ORDINALITY AS row(post_id, pool_index)").where(id: pool_id).select(:post_id, :pool_index) + pool_posts = Pool.named(pool_name).joins("CROSS JOIN unnest(pools.post_ids) WITH ORDINALITY AS row(post_id, pool_index)").select(:post_id, :pool_index) relation = relation.joins("JOIN (#{pool_posts.to_sql}) pool_posts ON pool_posts.post_id = posts.id").order("pool_posts.pool_index ASC") end diff --git a/app/models/application_record.rb b/app/models/application_record.rb index 535bd19f6..20c8cedd3 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -401,16 +401,6 @@ class ApplicationRecord < ActiveRecord::Base connection.execute("SET STATEMENT_TIMEOUT = #{CurrentUser.user.try(:statement_timeout) || 3_000}") unless Rails.env == "test" end end - - %w(execute select_value select_values select_all).each do |method_name| - define_method("#{method_name}_sql") do |sql, *params| - self.class.connection.__send__(method_name, self.class.send(:sanitize_sql_array, [sql, *params])) - end - - self.class.__send__(:define_method, "#{method_name}_sql") do |sql, *params| - connection.__send__(method_name, send(:sanitize_sql_array, [sql, *params])) - end - end end concerning :PostgresExtensions do diff --git a/app/models/note.rb b/app/models/note.rb index b549e9e26..428a070be 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -55,10 +55,10 @@ class Note < ApplicationRecord def update_post if self.saved_changes? - if Note.where(:is_active => true, :post_id => post_id).exists? - execute_sql("UPDATE posts SET last_noted_at = ? WHERE id = ?", updated_at, post_id) + if post.notes.active.exists? + post.update_columns(last_noted_at: updated_at) else - execute_sql("UPDATE posts SET last_noted_at = NULL WHERE id = ?", post_id) + post.update_columns(last_noted_at: nil) end end end diff --git a/app/models/pool.rb b/app/models/pool.rb index 5856baa69..679134772 100644 --- a/app/models/pool.rb +++ b/app/models/pool.rb @@ -87,14 +87,6 @@ class Pool < ApplicationRecord extend SearchMethods - def self.name_to_id(name) - if name =~ /^\d+$/ - name.to_i - else - select_value_sql("SELECT id FROM pools WHERE lower(name) = ?", name.downcase.tr(" ", "_")).to_i - end - end - def self.normalize_name(name) name.gsub(/[_[:space:]]+/, "_").gsub(/\A_|_\z/, "") end diff --git a/app/models/post_vote.rb b/app/models/post_vote.rb index 859925074..7aa63f2ed 100644 --- a/app/models/post_vote.rb +++ b/app/models/post_vote.rb @@ -11,16 +11,19 @@ class PostVote < ApplicationRecord after_create :update_post_on_create after_destroy :update_post_on_destroy + scope :positive, -> { where("post_votes.score > 0") } + scope :negative, -> { where("post_votes.score < 0") } + def self.positive_user_ids - select_values_sql("select user_id from post_votes where score > 0 group by user_id having count(*) > 100") + positive.group(:user_id).having("count(*) > 100").pluck(:user_id) end def self.negative_post_ids(user_id) - select_values_sql("select post_id from post_votes where score < 0 and user_id = ?", user_id) + negative.where(user_id: user_id).pluck(:post_id) end def self.positive_post_ids(user_id) - select_values_sql("select post_id from post_votes where score > 0 and user_id = ?", user_id) + positive.where(user_id: user_id).pluck(:post_id) end def self.visible(user = CurrentUser.user) diff --git a/app/models/tag.rb b/app/models/tag.rb index 6c04deb30..79214498e 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -154,7 +154,7 @@ class Tag < ApplicationRecord end def select_category_for(tag_name) - select_value_sql("SELECT category FROM tags WHERE name = ?", tag_name).to_i + Tag.where(name: tag_name).pick(:category).to_i end def category_for(tag_name, options = {}) @@ -596,8 +596,7 @@ class Tag < ApplicationRecord q[:pool] << g2 when "ordpool" - pool_id = Pool.name_to_id(g2) - q[:ordpool] = pool_id + q[:ordpool] = g2 when "-favgroup" favgroup = FavoriteGroup.find_by_name_or_id!(g2, CurrentUser.user) diff --git a/test/unit/note_test.rb b/test/unit/note_test.rb index c9235b735..ac652bf4d 100644 --- a/test/unit/note_test.rb +++ b/test/unit/note_test.rb @@ -33,13 +33,10 @@ class NoteTest < ActiveSupport::TestCase end context "when the note is deleted the post" do - setup do - @note.toggle!(:is_active) - end - should "null out its last_noted_at_field" do - @post.reload - assert_nil(@post.last_noted_at) + assert_not_nil(@post.reload.last_noted_at) + @note.update!(is_active: false) + assert_nil(@post.reload.last_noted_at) end end end @@ -127,10 +124,9 @@ class NoteTest < ActiveSupport::TestCase end should "update the post's last_noted_at field" do - assert_nil(@post.last_noted_at) - @note.update(x: 500) - @post.reload - assert_equal(@post.last_noted_at.to_i, @note.updated_at.to_i) + assert_equal(@post.reload.last_noted_at.to_i, @note.updated_at.to_i) + assert_changes("@post.reload.last_noted_at") { @note.update(x: 500) } + assert_equal(@post.reload.last_noted_at.to_i, @note.reload.updated_at.to_i) end should "create a version" do diff --git a/test/unit/pool_test.rb b/test/unit/pool_test.rb index 4e4bc7b76..466b093e4 100644 --- a/test/unit/pool_test.rb +++ b/test/unit/pool_test.rb @@ -20,36 +20,6 @@ class PoolTest < ActiveSupport::TestCase CurrentUser.ip_addr = nil end - context "A name" do - setup do - @pool = FactoryBot.create(:pool, :name => "xxx") - end - - should "be mapped to a pool id" do - assert_equal(@pool.id, Pool.name_to_id("xxx")) - end - end - - context "A multibyte character name" do - setup do - @mb_pool = FactoryBot.create(:pool, :name => "àáâãäå") - end - - should "be mapped to a pool id" do - assert_equal(@mb_pool.id, Pool.name_to_id("àáâãäå")) - end - end - - context "An id number" do - setup do - @pool = FactoryBot.create(:pool) - end - - should "be mapped to a pool id" do - assert_equal(@pool.id, Pool.name_to_id(@pool.id.to_s)) - end - end - context "Searching pools" do should "find pools by name" do @pool = FactoryBot.create(:pool, name: "Test Pool")