Class: Admin::Home::IndexPresenter

Inherits:
Object
  • Object
show all
Defined in:
app/presenters/admin/home/index_presenter.rb

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ IndexPresenter

Returns a new instance of IndexPresenter


2
3
4
5
6
7
8
9
10
# File 'app/presenters/admin/home/index_presenter.rb', line 2

def initialize(user)
  @user = user
  @animals_count = Animal.where(organization_id: @user.organization_id).count()
  @animal_update = Animal.order(updated_at: :desc).where(organization_id: @user.organization_id).first().try(:updated_at)

  @species_count = Species.where(organization_id: @user.organization_id).count()

  @events_count = PublicActivity::Activity.where(recipient_id: @user.organization_id).count()
end

Instance Method Details

#animals_sparklineObject


89
90
91
92
93
# File 'app/presenters/admin/home/index_presenter.rb', line 89

def animals_sparkline
  Rails.cache.fetch("animal_sparkline_hash_user_#{@user.organization_id}_#{@animals_count}_#{@animal_update.to_i}", expires_in: 1.hour) do
    Report.item_per_day(@user.organization_id, 'animal', 14)
  end
end

#colors_chartObject


24
25
26
27
28
# File 'app/presenters/admin/home/index_presenter.rb', line 24

def colors_chart
  Rails.cache.fetch("animal_colors_hash_user_#{@user.organization_id}_#{@animals_count}_#{@animal_update.to_i}") do
    Report.new_chart(@user.organization_id, 'animal_color')
  end
end

#contacts_sparklineObject


101
102
103
104
105
# File 'app/presenters/admin/home/index_presenter.rb', line 101

def contacts_sparkline
  Rails.cache.fetch("contact_sparkline_hash_user_#{@user.organization_id}_#{@animals_count}_#{@animal_update.to_i}", expires_in: 1.hour) do
    Report.contacts_per_day(@user.organization_id, 14)
  end
end

#events_sparklineObject


107
108
109
110
111
# File 'app/presenters/admin/home/index_presenter.rb', line 107

def events_sparkline
  Rails.cache.fetch("event_sparkline_hash_user_#{@user.organization_id}_#{@animals_count}_#{@animal_update.to_i}_#{@events_count}", expires_in: 1.hour) do
    Report.item_per_day(@user.organization_id, 'activity', 14, 'recipient_id')
  end
end

#latest_activityObject


30
31
32
33
34
# File 'app/presenters/admin/home/index_presenter.rb', line 30

def latest_activity
  Rails.cache.fetch("latest_activity_hash_user_#{@user.organization_id}_#{@events_count}") do
    PublicActivity::Activity.where(recipient_id: @user.organization_id).order(created_at: :desc).limit(5).to_a
  end
end

#public_animalsObject


36
37
38
39
40
# File 'app/presenters/admin/home/index_presenter.rb', line 36

def public_animals
  Rails.cache.fetch("public_animals_hash_user_#{@user.organization_id}_#{@animals_count}_#{@animal_update.to_i}") do
    Animal.where(organization_id: @user.organization_id, public: 1).to_a.sort! { |a,b| b.impressions_count <=> a.impressions_count }
  end
end

#sex_chartObject


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/presenters/admin/home/index_presenter.rb', line 66

def sex_chart
  Rails.cache.fetch("animal_sex_hash_user_#{@user.organization_id}_#{@animals_count}_#{@animal_update.to_i}") do
    @final_status_array = []
    sex = {}
    sex[:male] = Animal.where(organization_id: @user.organization_id, animal_sex_id: 1).count()
    sex[:female] = Animal.where(organization_id: @user.organization_id, animal_sex_id: 2).count()
    sex[:unknown] = Animal.where(organization_id: @user.organization_id, animal_sex_id: 3).count()

    color = Paleta::Color.new(:hex, '7761a7')
    palette = Paleta::Palette.generate(type: :analogous, from: :color, color: color, size: 3)
    colors = palette.to_array(color_model = :hex)
    colors.shuffle!
    cnt = 0
    sex.each do |key, value|
      percent = ((value.to_f / @animals_count.to_f) * 100)
      @final_status_array << {value: value, color: "##{colors[cnt]}", label: "#{key.capitalize}", percent: percent}
      cnt += 1
    end

    @final_status_array
  end
end

#species_chartObject


18
19
20
21
22
# File 'app/presenters/admin/home/index_presenter.rb', line 18

def species_chart
  Rails.cache.fetch("animal_species_hash_user_#{@user.organization_id}_#{@animals_count}_#{@animal_update.to_i}_#{@species_count}") do
    Report.new_chart(@user.organization_id, 'species')
  end
end

#species_sparklineObject


95
96
97
98
99
# File 'app/presenters/admin/home/index_presenter.rb', line 95

def species_sparkline
  Rails.cache.fetch("species_sparkline_hash_user_#{@user.organization_id}_#{@animals_count}_#{@animal_update.to_i}_#{@species_count}", expires_in: 1.hour) do
    Report.item_per_day(@user.organization_id, 'species', 14)
  end
end

#status_chartObject


12
13
14
15
16
# File 'app/presenters/admin/home/index_presenter.rb', line 12

def status_chart
  Rails.cache.fetch("animal_status_hash_user_#{@user.organization_id}_#{@animals_count}_#{@animal_update.to_i}") do
    Report.new_chart(@user.organization_id, 'status')
  end
end

#total_animalsObject


42
43
44
# File 'app/presenters/admin/home/index_presenter.rb', line 42

def total_animals
  @animals_count
end

#total_contactsObject


50
51
52
53
54
55
56
57
58
59
60
# File 'app/presenters/admin/home/index_presenter.rb', line 50

def total_contacts
  Rails.cache.fetch("total_contacts_hash_user_#{@user.organization_id}_#{@animals_count}_#{@animal_update.to_i}") do
    vet_contacts = VetContact.where(organization_id: @user.organization_id).count()
    volunteer_contacts = VolunteerContact.where(organization_id: @user.organization_id).count()
    adoption_contacts = AdoptionContact.where(organization_id: @user.organization_id).count()
    relinquishment_contacts = RelinquishmentContact.where(organization_id: @user.organization_id).count()
    foster_contacts = FosterContact.where(organization_id: @user.organization_id).count()

    vet_contacts + volunteer_contacts + adoption_contacts + relinquishment_contacts + foster_contacts
  end
end

#total_eventsObject


62
63
64
# File 'app/presenters/admin/home/index_presenter.rb', line 62

def total_events
  @events_count
end

#total_speciesObject


46
47
48
# File 'app/presenters/admin/home/index_presenter.rb', line 46

def total_species
  @species_count
end