I can clearly see I'm repeating myself, but I don't know how to refractor this without making it really ugly. What I have:
class ChildModelController < ApplicationController
def new
@model = ParentModel.find(params[:id])
# Does the current user have permissions on the parent model
if signed_in?
unless current_user.admin? or current_user.id == @model.user_id
flash[:error] = t('errors.messages.not_authorized')
flash.keep
redirect_to root_path
end
else
unless session and session[:session_id] == @model.author_session_token
flash[:error] = t('errors.messages.not_authorized')
flash.keep
redirect_to root_path
end
end
#do stuff to the model
end
end
The only way I can think to make it smaller is to make it ugly:
class ChildModelController < ApplicationController
def new
@model = ParentModel.find(params[:id])
# Does the current user have permissions on the parent model
if (!signed_in? and session and session[:session_id] == @model.author_session_token ) || (signed_in and current_user.admin? or current_user.id == @model.user_id)
flash[:error] = t('errors.messages.not_authorized')
flash.keep
redirect_to root_path
end
#do stuff to the model
end
end
Am I missing an even easier way?