application record: drop execute_sql, select_value_sql methods.

This commit is contained in:
evazion
2020-02-16 05:27:51 -06:00
parent 9caa9d8117
commit d3bbd82d8b
8 changed files with 19 additions and 69 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -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)

View File

@@ -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

View File

@@ -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")