class StatBoard::GraphHelper

Attributes

end_at[R]
start_at[R]

Public Class Methods

new(sa, ea) click to toggle source
# File lib/stat_board/graph_helper.rb, line 5
def initialize(sa, ea)
  @start_at = sa.to_date
  @end_at   = ea.to_date
end

Public Instance Methods

date_steps() click to toggle source

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

# 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) click to toggle source

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

# 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

Private Instance Methods

date_range() click to toggle source

range of dates shown on the graph (array of days)

# File lib/stat_board/graph_helper.rb, line 32
def date_range
  @date_range ||= start_at..end_at
end
first_day_of(klass_name) click to toggle source

returns the earliest `created_at` of a given class returns `Time.now` if none is available

# File lib/stat_board/graph_helper.rb, line 38
def first_day_of(klass_name)
  klass = klass_name.to_s.constantize
  klass.order("created_at ASC").first.try(:created_at) || Time.now
end