I have a photo_shouts_controller.rb and text_shouts_controller.rb which both have almost similar implementations, and I would like to refactor it.
I have implemented a refactoring but I am not too sure if that is the best way to do it.
Current implementation
class PhotoShoutsController < ApplicationController
def create
content = build_content
shout = current_user.shouts.build(content: content)
if shout.save
redirect_to dashboard_path
else
redirect_to dashboard_path, notice: 'not created!'
end
end
private
def build_content
PhotoShout.new(photo_shouts_parameters)
end
def photo_shouts_parameters
params.require(:photo_shout).permit(:image)
end
end
class TextShoutsController < ApplicationController
def create
content = build_content
shout = current_user.shouts.build(content: content)
if shout.save
redirect_to dashboard_path
else
redirect_to dashboard_path, notice: 'not created!'
end
end
private
def build_content
TextShout.new(text_shouts_parameters)
end
def text_shouts_parameters
params.require(:text_shout).permit(:body)
end
end
My new implementation
ShoutBuild.rb
class ShoutBuild
def initialize(current_user, shout_parameters, shout_class)
@shout_parameters = shout_parameters
@shout_class = shout_class
@user = current_user
end
def build
content = build_content
@user.shouts.build(content: content)
end
private
def build_content
@shout_class.new(@shout_parameters)
end
end
Controllers
class TextShoutsController < ApplicationController
def create
shout = ShoutBuild.new(current_user, text_shouts_parameters, TextShout).build
if shout.save
redirect_to dashboard_path
else
redirect_to dashboard_path, notice: 'not created'
end
end
private
def text_shouts_parameters
params.require(:text_shout).permit(:body)
end
end
class PhotoShoutsController < ApplicationController
def create
shout = ShoutBuild.new(current_user, photo_shouts_parameters, PhotoShout).build
if shout.save
redirect_to dashboard_path
else
redirect_to dashboard_path, notice: 'not created!'
end
end
private
def photo_shouts_parameters
params.require(:photo_shout).permit(:image)
end
end