diff --git a/app/models/account_summary.rb b/app/models/account_summary.rb index 30ada50cc..327c0ef30 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 000000000..8b6672e29 --- /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 6b49a3ca6..7ac9e6dfb 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 0fd31c809..3bd4b924a 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 38287c2a6..87b86a24d 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