class Template < ActiveRecord::Base has_many :template_assets belongs_to :organization has_many :schedules # function for MT2W 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 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 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 end