event_block.rb
2.3 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
class EventPlugin::EventBlock < Block
include DatesHelper
attr_accessible :all_env_events, :limit, :future_only, :date_distance_limit
settings_items :all_env_events, :type => :boolean, :default => false
settings_items :limit, :type => :integer, :default => 4
settings_items :future_only, :type => :boolean, :default => true
settings_items :date_distance_limit, :type => :integer, :default => 0
def self.description
_('Events')
end
def help
_('Show the profile events or all environment events.')
end
def events_source
return environment if all_env_events
if self.owner.kind_of? Environment
environment.portal_community ? environment.portal_community : environment
else
self.owner
end
end
def events(user = nil)
events = events_source.events.order('start_date')
events = user.nil? ? events.public : events.display_filter(user,nil)
if future_only
events = events.where('start_date >= ?', DateTime.now.beginning_of_day)
end
if date_distance_limit > 0
events = events.by_range([
DateTime.now.beginning_of_day - date_distance_limit,
DateTime.now.beginning_of_day + date_distance_limit
])
end
event_list = []
events.each do |event|
event_list << event if event.display_to? user
break if event_list.length >= limit
end
event_list
end
def content(args={})
block = self
proc do
render(
:file => 'blocks/event',
:locals => { :block => block }
)
end
end
def human_time_left(days_left)
months_left = (days_left/30.0).round
if days_left <= -60
n_('One month ago', '%d months ago', -months_left) % -months_left
elsif days_left < 0
n_('Yesterday', '%d days ago', -days_left) % -days_left
elsif days_left == 0
_("Today")
elsif days_left < 60
n_('Tomorrow', '%d days left to start', days_left) % days_left
else
n_('One month left to start', '%d months left to start', months_left) % months_left
end
end
def date_to_html(date)
content_tag(:span, show_day_of_week(date, true), :class => 'week-day') +
content_tag(:span, month_name(date.month, true), :class => 'month') +
content_tag(:span, date.day.to_s, :class => 'day') +
content_tag(:span, date.year.to_s, :class => 'year')
end
end