app/models/shift.rb
def combine_with_surrounding_shifts
    if (shift_later = Shift.where("start = ? AND user_id = ? AND location_id = ? AND calendars.active = ?", self.end, self.user_id, self.location_id, self.calendar.active?).includes(:calendar).first) && (!shift_later.has_sub?)
          if (self.report.nil? || self.report.departed.nil?) && (shift_later.report.nil?)
            self.end = shift_later.end
            shift_later.sub_requests.each { |s| s.shift = self }
            shift_later.destroy
            self.save(validate: false)
          end
        end
        if (shift_earlier = Shift.where("end = ? AND user_id = ? AND location_id = ? AND calendars.active = ?", self.start, self.user_id, self.location_id, self.calendar.active?).includes(:calendar).first) && (!shift_earlier.has_sub?)
          if (self.report.nil?) && (shift_earlier.report.nil? || shift_earlier.report.departed.nil?)
            self.start = shift_earlier.start
            shift_earlier.sub_requests.each {|s| s.shift = self}
            unless shift_earlier.report.nil?
              shift_earlier.report.shift = nil
              shift_earlier.report.save! #we have to disassociate the report first, or it will be destroyed too
              self.report = shift_earlier.report
              shift_earlier.report = nil
            end
            self.signed_in = shift_earlier.signed_in
            shift_earlier.destroy
            self.save(validate: false)
          end
        end
  end
# If new shift runs up against another compatible shift, combine them and save,
# preserving the earlier shift's information
If new shift runs up against another compatible shift, combine them and save,
preserving the earlier shift's information
If new shift runs up against another compatible shift combine them and save preserving the earlier shift ' s information
def combine_with_surrounding_shifts
    if (shift_later = Shift.where("start = ? AND user_id = ? AND location_id = ? AND calendars.active = ?", self.end, self.user_id, self.location_id, self.calendar.active?).includes(:calendar).first) && (!shift_later.has_sub?)
          if (self.report.nil? || self.report.departed.nil?) && (shift_later.report.nil?)
            self.end = shift_later.end
            shift_later.sub_requests.each { |s| s.shift = self }
            shift_later.destroy
            self.save(validate: false)
          end
        end
        if (shift_earlier = Shift.where("end = ? AND user_id = ? AND location_id = ? AND calendars.active = ?", self.start, self.user_id, self.location_id, self.calendar.active?).includes(:calendar).first) && (!shift_earlier.has_sub?)
          if (self.report.nil?) && (shift_earlier.report.nil? || shift_earlier.report.departed.nil?)
            self.start = shift_earlier.start
            shift_earlier.sub_requests.each {|s| s.shift = self}
            unless shift_earlier.report.nil?
              shift_earlier.report.shift = nil
              shift_earlier.report.save! 
              self.report = shift_earlier.report
              shift_earlier.report = nil
            end
            self.signed_in = shift_earlier.signed_in
            shift_earlier.destroy
            self.save(validate: false)
          end
        end
  end
def combine_with_surrounding_shifts if ( shift_later = Shift . where ( "start = ? AND user_id = ? AND location_id = ? AND calendars.active = ?" , self . end , self . user_id , self . location_id , self . calendar . active? ) . includes ( :calendar ) . first ) && ( ! shift_later . has_sub? ) if ( self . report . nil? || self . report . departed . nil? ) && ( shift_later . report . nil? ) self . end = shift_later . end shift_later . sub_requests . each { | s | s . shift = self } shift_later . destroy self . save ( validate : false ) end end if ( shift_earlier = Shift . where ( "end = ? AND user_id = ? AND location_id = ? AND calendars.active = ?" , self . start , self . user_id , self . location_id , self . calendar . active? ) . includes ( :calendar ) . first ) && ( ! shift_earlier . has_sub? ) if ( self . report . nil? ) && ( shift_earlier . report . nil? || shift_earlier . report . departed . nil? ) self . start = shift_earlier . start shift_earlier . sub_requests . each { | s | s . shift = self } unless shift_earlier . report . nil? shift_earlier . report . shift = nil shift_earlier . report . save! self . report = shift_earlier . report shift_earlier . report = nil end self . signed_in = shift_earlier . signed_in shift_earlier . destroy self . save ( validate : false ) end end end
If new shift runs up against another compatible shift, combine them and save,
preserving the earlier shift's information
If new shift runs up against another compatible shift combine them and save preserving the earlier shift ' s information
#we have to disassociate the report first, or it will be destroyed too