statistic_block.rb
1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class SoftwareCommunitiesPlugin::StatisticBlock < Block
settings_items :benefited_people, :type => :integer, :default => 0
settings_items :saved_resources, :type => :float, :default => 0.0
attr_accessible :benefited_people, :saved_resources
def self.description
_('Software Statistics')
end
def help
_('This block displays software statistics.')
end
def content(args={})
download_blocks = get_profile_download_blocks(self.owner)
downloads = download_blocks.map do |download_block|
get_downloads_from_block(download_block)
end
block = self
statistics = get_software_statistics
lambda do |object|
render(
:file => 'blocks/software_statistics',
:locals => {
:block => block,
:total_downloads => downloads.sum,
:statistics => statistics
}
)
end
end
def cacheable?
false
end
private
def get_profile_download_blocks profile
SoftwareCommunitiesPlugin::DownloadBlock.joins(:box).where("boxes.owner_id = ?", profile.id)
end
def get_downloads_from_block download_block
downloads = download_block.downloads.map do |download|
download[:total_downloads] unless download[:total_downloads].nil?
end
downloads.select! {|value| not value.nil? }
downloads.sum
end
def get_software_statistics
statistics = {}
software = SoftwareCommunitiesPlugin::SoftwareInfo.find_by_community_id(self.owner.id)
if software.present?
statistics[:saved_resources] = software.saved_resources
statistics[:benefited_people] = software.benefited_people
else
statistics[:saved_resources] = 0
statistics[:benefited_people] = 0
end
statistics
end
end