Extracting some logic from the huge Active Record method
Merge branch 'refactor-ar-logic'

[ci skip]