cirandas.net

ref: master

app/sweepers/profile_sweeper.rb


 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
31
32
33
34
35
36
37
38
39
# This is not a proper observer since is explicitly called in the profile model
class ProfileSweeper # < ActiveRecord::Observer
#  observe :profile
  include SweeperHelper

  def after_update(profile)
    self.delay.expire_caches profile
  end

  def after_create(profile)
  end

protected

  def expire_caches(profile)
    profile.members.each do |member|
      expire_communities(member) if profile.community?
      expire_enterprises(member) if profile.enterprise?
      expire_profile_index(member) if profile.enterprise?
    end

    expire_profile_index(profile) if profile.person?

    expire_profile_blocks(profile.blocks)

    expire_blogs(profile) if profile.organization?
  end

  def expire_blogs(profile)
    profile.blogs.select{|b| !b.empty?}.each do |blog|
      pages = blog.posts.count / blog.posts_per_page + 1
      ([nil] + (1..pages).to_a).each do |i|
        expire_timeout_fragment(blog.cache_key({:npage => i}))
        expire_timeout_fragment(blog.cache_key({:npage => i}, profile))
      end
    end
  end

end