step.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
class CommunityTrackPlugin::Step < Folder
settings_items :hidden, :type => :boolean, :default => false
alias :tools :children
acts_as_list :scope => :parent
def belong_to_track
errors.add(:parent, "Step not allowed at this parent.") if !parent.kind_of?(CommunityTrackPlugin::Track)
end
validate :belong_to_track
validates_presence_of :start_date, :end_date
validate :end_date_equal_or_after_start_date
after_save :schedule_activation
before_create do |step|
step.published = false
true
end
before_create :set_hidden_position
before_save :set_hidden_position
def set_hidden_position
if hidden
decrement_positions_on_lower_items
self[:position] = 0
elsif position == 0
add_to_list_bottom
end
end
def end_date_equal_or_after_start_date
if end_date && start_date
errors.add(:end_date, _('must be equal or after start date.')) unless end_date >= start_date
end
end
def self.short_description
_("Step")
end
def self.description
_('Defines a step.')
end
def accept_comments?
false
end
def enabled_tools
{TinyMceArticle => {:name => _('Article')}, Forum => {:name => _('Forum')}}
end
def to_html(options = {})
step = self
lambda do
render :file => 'content_viewer/step.rhtml', :locals => {:step => step}
end
end
def active?
(start_date..end_date).include?(Date.today)
end
def finished?
Date.today > end_date
end
def waiting?
Date.today < start_date
end
def schedule_activation
return if !changes['start_date'] && !changes['end_date'] && !changes['published']
today = Date.today
if today <= end_date || published
schedule_date = !published ? start_date : end_date + 1.day
CommunityTrackPlugin::ActivationJob.find(id).destroy_all
Delayed::Job.enqueue(CommunityTrackPlugin::ActivationJob.new(self.id), 0, schedule_date)
end
end
def publish
self[:published] = active? && !hidden
save!
end
class CommunityTrackPlugin::ActivationJob < Struct.new(:step_id)
def self.find(step_id)
Delayed::Job.where(:handler => "--- !ruby/struct:CommunityTrackPlugin::ActivationJob \nstep_id: #{step_id}\n")
end
def perform
step = CommunityTrackPlugin::Step.find(step_id)
step.publish
end
end
end