plugins/community_track/lib/community_track_plugin/step.rb
class CommunityTrackPlugin::Step < Folder
settings_items :hidden, type: :boolean, default: false
settings_items :tool_type, type: String
attr_accessible :start_date, :end_date, :tool_type, :hidden
alias :tools :children
acts_as_list scope: :parent_id, add_new_at: :bottom
def belong_to_track
errors.add(:parent, _("Step not allowed at this parent.")) unless 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.accept_comments = false
true
end
def initialize(*args)
super(*args)
self.start_date ||= DateTime.now
self.end_date ||= DateTime.now + 1.day
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?
accept_comments
end
def enabled_tools
[TextArticle, Forum]
end
def to_html(options = {})
step = self
proc do
render file: "content_viewer/step", locals: { step: step }
end
end
def active?
(start_date..end_date).cover?(DateTime.now)
end
def finished?
DateTime.now > end_date
end
def waiting?
DateTime.now < start_date
end
def schedule_activation
return if !changes["start_date"] && !changes["end_date"]
if DateTime.now <= end_date || accept_comments
schedule_date = !accept_comments ? start_date : end_date + 1.day
CommunityTrackPlugin::ActivationJob.find(id).destroy_all
Delayed::Job.enqueue(CommunityTrackPlugin::ActivationJob.new(self.id), run_at: schedule_date)
end
end
def toggle_activation
accept_comments = active?
# set accept_comments = true on all children
self.class.toggle_activation(self, accept_comments)
end
def self.toggle_activation(article, accept_comments)
article.update_attribute(:accept_comments, accept_comments)
article.children.each { |a| toggle_activation(a, accept_comments) }
end
def tool_class
tool_type ? tool_type.constantize : nil
end
def tool
tools.where(type: tool_type).first
end
end