Class: Report

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

Overview

Report model

Class Method Summary collapse

Class Method Details

.contacts_per_day(org, days_past) ⇒ Object


35
36
37
38
39
40
41
42
43
# File 'app/models/report.rb', line 35

def self.contacts_per_day(org, days_past)
  set = [0]
  set = set.cycle.take days_past.to_i+1
  %w(vet_contact volunteer_contact adoption_contact relinquishment_contact foster_contact).each do |item|
    results = item_per_day(org, item, days_past)
    set = set.map.with_index{ |m,i| m + results[i].to_i }
  end
  set
end

.item_per_day(org, item, days_past, column = 'organization_id') ⇒ Object


20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/report.rb', line 20

def self.item_per_day(org, item, days_past, column='organization_id')
  date = Date.today - days_past.to_i

  items = item.camelize.constantize.where(column+" = ? and date(created_at) > ?", org, date).group("date(created_at)").count

  # fill empty dates with 0s
  date.upto(Date.today) do |x|
    items[x] ||= 0
  end

  sorted = items.sort_by { |date, value| date.to_s }
  sorted.map! {|array| array[1] }
  sorted
end

.new_chart(org, type) ⇒ Object


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/models/report.rb', line 3

def self.new_chart(org, type)
  animals_count = Animal.where(organization_id: org).count()     
  all_types = type.camelize.constantize.where(organization_id: org)
  final_status_array = Array.new

  color = Paleta::Color.new(:hex, '7761a7')
  palette = Paleta::Palette.generate(type: :analogous, from: :color, color: color, size: all_types.count)
  colors = palette.to_array(color_model = :hex)
  colors.shuffle!
  all_types.each_with_index do |object, index|
    count = Animal.where(:organization_id => org, "#{type}_id".to_sym => object.id).count()
    percent = ((count.to_f / animals_count.to_f) * 100)
    final_status_array << {value: count, color: "##{colors[index]}", label: "#{object.report_display_name}", percent: percent}
  end
  final_status_array
end