mirror of
https://github.com/mastodon/mastodon.git
synced 2024-08-20 21:08:15 -07:00
Extract scenic view model common methods to concern (#28111)
This commit is contained in:
parent
1666b19559
commit
86500e3312
5 changed files with 34 additions and 33 deletions
|
@ -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
|
||||
|
|
25
app/models/concerns/database_view_record.rb
Normal file
25
app/models/concerns/database_view_record.rb
Normal 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
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -10,11 +10,9 @@
|
|||
#
|
||||
|
||||
class UserIp < ApplicationRecord
|
||||
include DatabaseViewRecord
|
||||
|
||||
self.primary_key = :user_id
|
||||
|
||||
belongs_to :user
|
||||
|
||||
def readonly?
|
||||
true
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue