profile_controller.rb
1.94 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
require_dependency 'profile_controller'
class ProfileController
before_filter :hit_view_page
def communities
type = []
params[:type].downcase! unless params[:type].nil?
if params[:type] == "software"
type = profile.softwares
elsif params[:type] == "institution"
type = profile.institutions
else
profile.communities.select do |community|
type << community unless community.software? || community.institution?
end
end
if is_cache_expired?(profile.communities_cache_key(params))
@communities = type.paginate(:per_page => per_page, :page => params[:npage], :total_entries => type.count)
end
end
def members
if is_cache_expired?(profile.members_cache_key(params))
sort = (params[:sort] == 'desc') ? params[:sort] : 'asc'
@profile_admins = profile.admins.includes(relations_to_include).order("name #{sort}").paginate(:per_page => members_per_page, :page => params[:npage])
@profile_members = profile.members.order("name #{sort}").paginate(:per_page => members_per_page, :page => params[:npage])
@profile_members_url = url_for(:controller => 'profile', :action => 'members')
end
end
def user_is_a_bot?
user_agent= request.env["HTTP_USER_AGENT"]
user_agent.blank? ||
user_agent.match(/bot/) ||
user_agent.match(/spider/) ||
user_agent.match(/crawler/) ||
user_agent.match(/\(.*https?:\/\/.*\)/)
end
def already_visited?(element)
user_id = if user.nil? then -1 else current_user.id end
user_id = "#{user_id}_#{element.id}_#{element.class}"
if cookies.signed[:visited] == user_id
return true
else
cookies.permanent.signed[:visited] = user_id
return false
end
end
def hit_view_page
if profile
community = profile
community.hit unless user_is_a_bot? ||
already_visited?(community) ||
community.class != Community
end
end
end