Module: LoyalCore::ActsAsViewsCountAble::ClassMethods

Defined in:
lib/loyal_core/acts/views_count_able.rb

Instance Method Summary collapse

Instance Method Details

#loyal_core_acts_as_views_count_able(*args) ⇒ Object

具备计数功能 views_count



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/loyal_core/acts/views_count_able.rb', line 13

def loyal_core_acts_as_views_count_able *args
  options = ::LoyalCore::ArrayUtil.extract_options!(args)

  define_method :increment_views_count! do
    Rails.logger.debug " -> loyal_core_acts_as_views_count_able: #{self.id} increment_views_count!"
    self.increment(:views_count)

    # 刷新更新时间
    self.refresh_tiny_cache_updated_at
    self.write_tiny_cache # 写入缓存

    # 频率间隔, 默认为10次
    if self.views_count % (options[:interval] || 10).to_i == 0
      self.class.update_all({:views_count => self.views_count}, :id => self.id)
    end
  end
end