Class: SiteActivity

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/site_activity.rb

Overview

Schema Information

Table name: site_activities

id           :integer          not null, primary key
title        :string(255)
value        :float            default(0.0)
min_value    :float
max_value    :float
is_increment :boolean
sort_order   :integer          default(1)
last_value   :integer
last_date    :date
created_at   :datetime
updated_at   :datetime
parent_id    :integer
is_procent   :boolean

Instance Method Summary collapse

Instance Method Details

#current_day?(date = nil) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
# File 'app/models/site_activity.rb', line 67

def current_day?(date = nil)
  date ||= created_at

  date.to_date == Date.current
end

#displayed_valueObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/site_activity.rb', line 31

def displayed_value
  return (value * parent.displayed_value).to_i unless parent_id.nil?
  return value if current_day?

  Rails.cache.fetch(cached_key('displayed_value'), expires_in: 2.hours) do
    result = if is_increment
               increment_value
             else
               n = value % 1 > 0 ? 10.0 : 1
               ((min_value * n).to_i..(max_value * n).to_i).to_a.sample / n
    end

    result
  end
end

#increment_valueObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/site_activity.rb', line 52

def increment_value
  last = last_value || value

  if last_date.nil? || !current_day?(last_date)
    self.last_date = Date.current

    self.last_value = last + (min_value.to_i..max_value.to_i).to_a.sample

    save(validate: false)
    return last_value
  else
    last
  end
end

#viewObject



47
48
49
50
# File 'app/models/site_activity.rb', line 47

def view
  _value = format('%g', displayed_value)
  is_procent ? "#{_value}%" : _value
end