From 86500e3312394de52413254e81bbc91f1a9333a0 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 6 Feb 2024 04:08:07 -0500 Subject: [PATCH] Extract scenic view model common methods to concern (#28111) --- app/models/account_summary.rb | 14 +++--------- app/models/concerns/database_view_record.rb | 25 +++++++++++++++++++++ app/models/follow_recommendation.rb | 12 ++-------- app/models/instance.rb | 10 ++------- app/models/user_ip.rb | 6 ++--- 5 files changed, 34 insertions(+), 33 deletions(-) create mode 100644 app/models/concerns/database_view_record.rb diff --git a/app/models/account_summary.rb b/app/models/account_summary.rb index 30ada50cc0..327c0ef305 100644 --- a/app/models/account_summary.rb +++ b/app/models/account_summary.rb @@ -10,21 +10,13 @@ # class AccountSummary < ApplicationRecord + include DatabaseViewRecord + self.primary_key = :account_id - has_many :follow_recommendation_suppressions, primary_key: :account_id, foreign_key: :account_id, inverse_of: false + has_many :follow_recommendation_suppressions, primary_key: :account_id, foreign_key: :account_id, inverse_of: false, dependent: nil scope :safe, -> { where(sensitive: false) } scope :localized, ->(locale) { order(Arel::Nodes::Case.new.when(arel_table[:language].eq(locale)).then(1).else(0).desc) } scope :filtered, -> { where.missing(:follow_recommendation_suppressions) } - - def self.refresh - Scenic.database.refresh_materialized_view(table_name, concurrently: true, cascade: false) - rescue ActiveRecord::StatementInvalid - Scenic.database.refresh_materialized_view(table_name, concurrently: false, cascade: false) - end - - def readonly? - true - end end diff --git a/app/models/concerns/database_view_record.rb b/app/models/concerns/database_view_record.rb new file mode 100644 index 0000000000..8b6672e299 --- /dev/null +++ b/app/models/concerns/database_view_record.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +module DatabaseViewRecord + extend ActiveSupport::Concern + + class_methods do + def refresh + Scenic.database.refresh_materialized_view( + table_name, + concurrently: true, + cascade: false + ) + rescue ActiveRecord::StatementInvalid + Scenic.database.refresh_materialized_view( + table_name, + concurrently: false, + cascade: false + ) + end + end + + def readonly? + true + end +end diff --git a/app/models/follow_recommendation.rb b/app/models/follow_recommendation.rb index 6b49a3ca66..7ac9e6dfb9 100644 --- a/app/models/follow_recommendation.rb +++ b/app/models/follow_recommendation.rb @@ -10,6 +10,8 @@ # class FollowRecommendation < ApplicationRecord + include DatabaseViewRecord + self.primary_key = :account_id self.table_name = :global_follow_recommendations @@ -17,14 +19,4 @@ class FollowRecommendation < ApplicationRecord belongs_to :account scope :localized, ->(locale) { joins(:account_summary).merge(AccountSummary.localized(locale)) } - - def self.refresh - Scenic.database.refresh_materialized_view(table_name, concurrently: true, cascade: false) - rescue ActiveRecord::StatementInvalid - Scenic.database.refresh_materialized_view(table_name, concurrently: false, cascade: false) - end - - def readonly? - true - end end diff --git a/app/models/instance.rb b/app/models/instance.rb index 0fd31c8097..3bd4b924ae 100644 --- a/app/models/instance.rb +++ b/app/models/instance.rb @@ -9,6 +9,8 @@ # class Instance < ApplicationRecord + include DatabaseViewRecord + self.primary_key = :domain attr_accessor :failure_days @@ -27,10 +29,6 @@ class Instance < ApplicationRecord scope :by_domain_and_subdomains, ->(domain) { where("reverse('.' || domain) LIKE reverse(?)", "%.#{domain}") } scope :with_domain_follows, ->(domains) { where(domain: domains).where(domain_account_follows) } - def self.refresh - Scenic.database.refresh_materialized_view(table_name, concurrently: true, cascade: false) - end - def self.domain_account_follows Arel.sql( <<~SQL.squish @@ -44,10 +42,6 @@ class Instance < ApplicationRecord ) end - def readonly? - true - end - def delivery_failure_tracker @delivery_failure_tracker ||= DeliveryFailureTracker.new(domain) end diff --git a/app/models/user_ip.rb b/app/models/user_ip.rb index 38287c2a60..87b86a24d4 100644 --- a/app/models/user_ip.rb +++ b/app/models/user_ip.rb @@ -10,11 +10,9 @@ # class UserIp < ApplicationRecord + include DatabaseViewRecord + self.primary_key = :user_id belongs_to :user - - def readonly? - true - end end