Class: Template

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/template.rb

Instance Method Summary collapse

Instance Method Details

#add_schedules(schedules) ⇒ Object

function for MT2W



7
8
9
10
11
12
13
14
15
16
17
# File 'app/models/template.rb', line 7

def add_schedules schedules
  Schedule.where(:id => schedules).each do |schedule|
        sch = Schedule.new(schedule.attributes.merge({:id => nil,  :template_id => self.id}))
        if sch.save!(:validate => false)
          schedule.shifts.each do |shf|
            shift = Shift.new(shf.attributes.merge({:id => nil, :schedule_id => sch.id}))
            shift.save!(:validate => false)
          end
        end
  end
end

#add_schedules_from_date(date, org) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/template.rb', line 19

def add_schedules_from_date date, org
  Schedule.where(:organization_id => org. id).not_template.week_schedule_by_date(date).each do |schedule|
        sch = Schedule.new(schedule.attributes.merge({:id => nil,  :template_id => self.id}))
        if sch.save!(:validate => false)
          schedule.shifts.each do |shf|
            shift = Shift.new(shf.attributes.merge({:id => nil, :schedule_id => sch.id}))
            shift.save!(:validate => false)
          end
        end
  end
end

#load_schedules(date, m_to_s) ⇒ Object



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
# File 'app/models/template.rb', line 31

def load_schedules date, m_to_s
  date = Date.parse date
  if m_to_s
    start_day = date - date.wday + 1.day
    end_day = date - date.wday + 7.day
  else
    start_day = date - date.wday + 0.day
    end_day = date - date.wday + 6.day
  end

  curr_schedules = Schedule.week_schedule_by_date(date).not_template
  curr_schedules.delete_all if curr_schedules.length > 0
  self.schedules.includes(:shifts).each do |schedule|
    sch_attr = schedule.attributes.merge({ :id => nil,  :template_id => nil, :start_date => start_day,
                                          :end_date =>  end_day, :publish => false })
    sch = Schedule.new(sch_attr)
    schedule.shifts.each do |shift|
      attributes = shift.attributes.merge({id: nil}).except("schedule_id")
      attributes['due_date'] = start_day + attributes['day_of_week']
      attributes['due_date'] = attributes['due_date'] - 1 if m_to_s
      sch.shifts.build(attributes)
    end
    sch.save!(:validate => false)
  end

  return true
end