Module: Concerns::Scheduling

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/scheduling.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#create_schedule_infoObject



55
56
57
58
59
60
61
# File 'app/models/concerns/scheduling.rb', line 55

def create_schedule_info
  self.schedule_info = { 
    "start_time" => ConcertoConfig[:content_default_start_time], 
    "end_time" => ConcertoConfig[:content_default_end_time], 
    "criteria" => nil 
  } if self.schedule_info.nil?
end

#has_schedule?Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
# File 'app/models/concerns/scheduling.rb', line 20

def has_schedule?
  if self.blank? or self.schedule_info.blank? or !self.schedule_info.is_a?(Hash) or
    !self.schedule_info.include?("start_time") or !self.schedule_info.include?("end_time") or
    !self.schedule_info.include?("criteria") or self.schedule_info["start_time"].blank? or
    self.schedule_info["end_time"].blank? or self.schedule_info["criteria"].blank? or self.schedule_info["criteria"] == "null"
    return false
  end
  return true
end

#is_active_per_schedule?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/concerns/scheduling.rb', line 30

def is_active_per_schedule?
  active = true

  if !has_schedule?
    # missing or incomplete schedule information so assume active
  else
    # see if it is within the viewing window for the day
    begin
      if Clock.time >= Time.parse(self.schedule_info['start_time']) && Clock.time <= Time.parse(self.schedule_info['end_time'])
        # and it matches the criteria
        s = IceCube::Schedule.new(self.start_time)
        s.add_recurrence_rule(RecurringSelect.dirty_hash_to_rule(self.schedule_info['criteria']))
        active = s.occurs_on? Clock.time
      else
        active = false
      end
    rescue => e
      active = false
      Rails.logger.error("Unable to determine if schedule is active - #{e.message} for content #{self.id}")
    end
  end

  active
end

#load_schedule_infoObject



63
64
65
66
67
68
69
# File 'app/models/concerns/scheduling.rb', line 63

def load_schedule_info
  self.schedule_info = JSON.load(self.schedule) rescue {}
  create_schedule_info
  if self.schedule_info.has_key?('criteria') and self.schedule_info['criteria'] == 'null'
    self.schedule_info['criteria'] = nil
  end
end

#save_schedule_infoObject



71
72
73
# File 'app/models/concerns/scheduling.rb', line 71

def save_schedule_info
  self.schedule = JSON.dump(self.schedule_info)
end

#schedule_in_wordsObject



75
76
77
78
79
80
81
# File 'app/models/concerns/scheduling.rb', line 75

def schedule_in_words
  if has_schedule?
    s = IceCube::Schedule.new(self.start_time)
    s.add_recurrence_rule(RecurringSelect.dirty_hash_to_rule(self.schedule_info['criteria']))
    s.to_s
  end
end