My session controller has a method for creating new user session. According to Rubocop's output there is 'Assignment Branch Condition' metric is too high [15.17/15].
def create
agent = Agent.find_by(login: params[:session][:login])
if agent && agent.authenticate(params[:session][:password])
log_in agent
redirect_to dashboard_url
else
flash.now[:danger] = 'Invalid login or password'
render 'new'
end
end
log_in
method:
def log_in(agent)
session[:agent_id] = agent.id
end
I've extracted method for processing unsuccessful logins to reduce ABC size:
def create
agent = Agent.find_by(login: params[:session][:login])
if agent && agent.authenticate(params[:session][:password])
log_in agent
redirect_to dashboard_url
else
unsuccessful_login 'Invalid login or password'
end
end
private
def unsuccessful_login(message)
flash.now[:danger] = message
render 'new'
end
- Is it appropriate to make new method I'm going to use only once in this case?
- Is there any guideline when it's suitable to extract method?