Refactor rake task into model