<commit_before>module ActsAsTimeTrackable
  module ActiveRecordStores
    class TimeEntry < ActiveRecord::Base 
      belongs_to :time_trackable, polymorphic: true
      belongs_to :time_tracker, polymorphic: true

      validates :time_trackable, presence: true
      validates :time_tracker, presence: true
      validates :started_at, presence: true
      validate :stopped_at_must_be_after_started_at

      scope :time_tracking, -> { where(stopped_at: nil) }
      scope :stopped, -> { where.not(stopped_at: nil) }

      def duration
        stopped_at_or_now - started_at
      end

      def formatted_duration(format = '%h:%m:%s')
        Time.diff(started_at, stopped_at_or_now, format)[:diff]
      end

      def stop
        update!(stopped_at: Time.now)
      end

      private
        def stopped_at_or_now
          (stopped_at.presence || Time.now)
        end

        def stopped_at_must_be_after_started_at
          return if started_at.blank? || stopped_at.blank?

          if started_at >= stopped_at
            errors.add(:stopped_at, 'must be after the started at')
          end
        end
    end
  end
end<commit_msg>Fix validation of time_entry<commit_after>module ActsAsTimeTrackable
  module ActiveRecordStores
    class TimeEntry < ActiveRecord::Base 
      belongs_to :time_trackable, polymorphic: true
      belongs_to :time_tracker, polymorphic: true

      validates :time_trackable, presence: true
      validates :time_tracker, presence: true
      validates :started_at, presence: true
      validate :stopped_at_must_be_after_started_at

      scope :time_tracking, -> { where(stopped_at: nil) }
      scope :stopped, -> { where.not(stopped_at: nil) }

      def duration
        stopped_at_or_now - started_at
      end

      def formatted_duration(format = '%h:%m:%s')
        Time.diff(started_at, stopped_at_or_now, format)[:diff]
      end

      def stop
        update!(stopped_at: Time.now)
      end

      private
        def stopped_at_or_now
          (stopped_at.presence || Time.now)
        end

        def stopped_at_must_be_after_started_at
          return if started_at.blank? || stopped_at.blank?

          if started_at > stopped_at
            errors.add(:stopped_at, 'must be after the started at')
          end
        end
    end
  end
end
{'fraction_non_alphanumeric': 0.07396193771626297, 'fraction_numerical': 0.0, 'mean_word_length': 2.5694444444444446, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '415974', 'n_tokens_mistral': 742, 'n_tokens_neox': 701, 'n_words': 164}