1class TrendingDomain < ActiveRecord::Base |
|
2 belongs_to :petition |
|
4 validates :domain, presence: true, length: { maximum: 100 } |
|
5 validates :count, presence: true, numericality: { only_integer: true, greater_than: 0 } |
|
6 validates :starts_at, presence: true |
|
8 attr_readonly(:domain, :count, :starts_at) |
|
10 class << self |
|
11 def default_scope |
|
12 order(created_at: :desc, count: :desc) |
|
13 end |
|
15 def log!(time, domain, count) |
|
16 create!(domain: domain, count: count, starts_at: time) |
|
17 end |
|
19 def search(query, options = {}) |
|
20 query = query.to_s
|
|
21 page = [options[:page].to_i, 1].max |
|
23 if query.present? |
|
24 scope = where(domain: query) |
|
25 else |
|
26 scope = all
|
|
27 end |
|
29 scope.paginate(page: page, per_page: 50) |
|
30 end |
|
31 end |
|
|
33 def ends_at |
34 starts_at.advance(hours: 1) |
|
35 end |
|
|
37 def window |
38 starts_at.getutc.iso8601(0) |
|
39 end |
|
40end |