Extract scenic view model common methods to concern (#28111)

This commit is contained in:
Matt Jankowski 2024-02-06 04:08:07 -05:00 committed by GitHub
parent 1666b19559
commit 86500e3312
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 34 additions and 33 deletions

View File

@ -10,21 +10,13 @@
# #
class AccountSummary < ApplicationRecord class AccountSummary < ApplicationRecord
include DatabaseViewRecord
self.primary_key = :account_id 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 :safe, -> { where(sensitive: false) }
scope :localized, ->(locale) { order(Arel::Nodes::Case.new.when(arel_table[:language].eq(locale)).then(1).else(0).desc) } 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) } 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 end

View File

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

View File

@ -10,6 +10,8 @@
# #
class FollowRecommendation < ApplicationRecord class FollowRecommendation < ApplicationRecord
include DatabaseViewRecord
self.primary_key = :account_id self.primary_key = :account_id
self.table_name = :global_follow_recommendations self.table_name = :global_follow_recommendations
@ -17,14 +19,4 @@ class FollowRecommendation < ApplicationRecord
belongs_to :account belongs_to :account
scope :localized, ->(locale) { joins(:account_summary).merge(AccountSummary.localized(locale)) } 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 end

View File

@ -9,6 +9,8 @@
# #
class Instance < ApplicationRecord class Instance < ApplicationRecord
include DatabaseViewRecord
self.primary_key = :domain self.primary_key = :domain
attr_accessor :failure_days attr_accessor :failure_days
@ -27,10 +29,6 @@ class Instance < ApplicationRecord
scope :by_domain_and_subdomains, ->(domain) { where("reverse('.' || domain) LIKE reverse(?)", "%.#{domain}") } scope :by_domain_and_subdomains, ->(domain) { where("reverse('.' || domain) LIKE reverse(?)", "%.#{domain}") }
scope :with_domain_follows, ->(domains) { where(domain: domains).where(domain_account_follows) } 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 def self.domain_account_follows
Arel.sql( Arel.sql(
<<~SQL.squish <<~SQL.squish
@ -44,10 +42,6 @@ class Instance < ApplicationRecord
) )
end end
def readonly?
true
end
def delivery_failure_tracker def delivery_failure_tracker
@delivery_failure_tracker ||= DeliveryFailureTracker.new(domain) @delivery_failure_tracker ||= DeliveryFailureTracker.new(domain)
end end

View File

@ -10,11 +10,9 @@
# #
class UserIp < ApplicationRecord class UserIp < ApplicationRecord
include DatabaseViewRecord
self.primary_key = :user_id self.primary_key = :user_id
belongs_to :user belongs_to :user
def readonly?
true
end
end end