diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index a8c8b7f..605db95 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -8,10 +8,9 @@ def dashboard private def authenticate - if Rails.env.production? - authenticate_or_request_with_http_basic do |username, password| - username == ENV['ADMIN_USERNAME'] && password == ENV['ADMIN_PASSWORD'] - end + return unless Rails.env.production? + authenticate_or_request_with_http_basic do |username, password| + username == ENV['ADMIN_USERNAME'] && password == ENV['ADMIN_PASSWORD'] end end end diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index 21b22b5..803524b 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -13,12 +13,9 @@ def create @question.save! params[:options].each do |option| - if option[:title] != '' - new_option = Option.new - new_option.title = option[:title] - new_option.question_id = @question.id - new_option.save! - end + next unless option[:title] != '' + new_option = Option.new(title: option[:title], question_id: @question.id) + new_option.save! end redirect_to "/#{@question.secret}" @@ -40,7 +37,7 @@ def check_secret_availability private def set_question - @question = Question.where(secret: params[:secret]).first + @question = Question.find_by_secret!(params[:secret]) end def question_params @@ -48,11 +45,9 @@ def question_params end def check_secret_is_unique - if defined? params[:question][:secret] - if Question.where({ secret: params[:question][:secret] }).exists? - @question = Question.new(question_params) - redirect_to :back, notice: 'Sorry that URL is taken' - end - end + return unless defined? params[:question][:secret] && + Question.where(secret: params[:question][:secret]).exists? + @question = Question.new(question_params) + redirect_to :back, notice: 'Sorry that URL is taken' end end diff --git a/app/controllers/votes_controller.rb b/app/controllers/votes_controller.rb index c26f685..13945ec 100644 --- a/app/controllers/votes_controller.rb +++ b/app/controllers/votes_controller.rb @@ -6,20 +6,20 @@ def update vote_id = cookies["vote_#{question_id}"] vote = Vote.where(secret: vote_id).first_or_initialize - question = Question.where(secret: question_id).first + question = Question.find_by_secret!(question_id) vote.secret = SecureRandom.urlsafe_base64(nil, false) unless vote.secret vote.question_id = question.id unless vote.question_id vote.option_id = Option.find(params[:vote][:option_id]).id Pusher[question_id].trigger('vote', {}) - if vote.save! - cookies.permanent["vote_#{question.secret}"] = vote.secret + return unless vote.save! - respond_to do |format| - format.html { redirect_to "/#{question.secret}" } - format.json { render json: {}, status: :created } - end + cookies.permanent["vote_#{question.secret}"] = vote.secret + + respond_to do |format| + format.html { redirect_to "/#{question.secret}" } + format.json { render json: {}, status: :created } end end