about summary refs log tree commit diff
path: root/app/models/instance.rb
blob: 0839894d985e4fcf4f3d48bcf892caeeebaca344 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# frozen_string_literal: true

class Instance
  include ActiveModel::Model

  attr_accessor :domain, :accounts_count, :domain_block, :updated_at

  def initialize(resource)
    @domain         = resource.domain
    @accounts_count = resource.is_a?(DomainBlock) ? nil : resource.accounts_count
    @domain_block   = resource.is_a?(DomainBlock) ? resource : DomainBlock.find_by(domain: domain)
    @updated_at     = resource.is_a?(DomainBlock) ? resource.updated_at : 0
  end

  def cached_sample_accounts
    Rails.cache.fetch("#{cache_key}/sample_accounts", expires_in: 12.hours) { Account.where(domain: domain).searchable.joins(:account_stat).popular.limit(3) }
  end

  def cached_accounts_count
    @accounts_count || Rails.cache.fetch("#{cache_key}/count", expires_in: 12.hours) { Account.where(domain: domain).count }
  end

  def to_param
    domain
  end

  def cache_key
    domain
  end
end