Class: StatBoard::GraphHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/stat_board/graph_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sa, ea) ⇒ GraphHelper

Returns a new instance of GraphHelper.



5
6
7
8
# File 'lib/stat_board/graph_helper.rb', line 5

def initialize(sa, ea)
  @start_at = sa.to_date
  @end_at   = ea.to_date
end

Instance Attribute Details

#end_atObject (readonly)

Returns the value of attribute end_at.



3
4
5
# File 'lib/stat_board/graph_helper.rb', line 3

def end_at
  @end_at
end

#start_atObject (readonly)

Returns the value of attribute start_at.



3
4
5
# File 'lib/stat_board/graph_helper.rb', line 3

def start_at
  @start_at
end

Instance Method Details

#date_stepsObject

number of days per interval on the graph’s x axis



25
26
27
# File 'lib/stat_board/graph_helper.rb', line 25

def date_steps
  @date_steps ||= date_range.count / [date_range.count, 50].min
end

#resources_by_date(klass_name) ⇒ Object

a string of the array of the count of (klass) objects along the date_range, on every displayed interval



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/stat_board/graph_helper.rb', line 12

def resources_by_date(klass_name)
  @resrouces_cache ||= {}
  @resrouces_cache[klass_name] ||= begin
    klass = klass_name.to_s.constantize
    steps = date_range.step(date_steps).map(&:end_of_day)

    steps.map do |step_end|
      klass.where("created_at <= ?", step_end).count
    end
  end
end