Copy Rani's code to rails and separate to diff files