module Travis
  module Services
    class FindArtifact < Base
      register :find_artifact

      def run(options = {})
        result if result
      end

      def final?
        # TODO jobs can be requeued, so finished jobs are no more final
        # # TODO keep the state on the artifact
        # result && result.job && result.job.finished?
        false
      end

      # def updated_at
      #   result.updated_at
      # end

      private

        def result
          @result ||= if params[:id]
            scope(:artifact).find_by_id(params[:id])
          elsif params[:job_id]
            scope(:artifact).find_by_job_id(params[:job_id])
          end
        end
    end
  end
end

module Travis
  module Services
    class FindArtifact < Base
      register :find_artifact

      def run(options = {})
        result if result
      end

      def final?
        # TODO jobs can be requeued, so finished jobs are no more final
        # # TODO keep the state on the artifact
        # result && result.job && result.job.finished?
        false
      end

      # def updated_at
      #   result.updated_at
      # end

      private

        def result
          @result ||= if params[:id]
            scope(:artifact).find_by_id(params[:id])
          elsif params[:job_id]
            scope(:artifact).where(type: 'Artifact::Log', job_id: params[:job_id]).first
          end
        end
    end
  end
end

Use also type when querying Artifact by job_id
Use also type when querying Artifact by job_id

We have an index on (type, job_id), which is not used effectively when
querying only by job_id.
