From 9970de0d7b2a32140851415490088c833e232b9f Mon Sep 17 00:00:00 2001 From: Dana Scheider Date: Sun, 20 Jul 2014 21:18:54 -0700 Subject: [PATCH 1/3] Change 3-line conditional to 1 line Changed the unless statement defining the default template engine to a single line --- lib/sinatra-authentication.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/sinatra-authentication.rb b/lib/sinatra-authentication.rb index 3f679bc..c65be72 100755 --- a/lib/sinatra-authentication.rb +++ b/lib/sinatra-authentication.rb @@ -11,9 +11,7 @@ def self.registered(app) #so to get around I have to do it totally manually by #loading the view from this path into a string and rendering it app.set :sinatra_authentication_view_path, File.expand_path('../views/', __FILE__) - unless defined?(settings.template_engine) - app.set :template_engine, :haml - end + app.set :template_engine, :haml unless defined?(settings.template_engine) app.get '/users/?' do login_required From 588d8c545f7812ddb859313b011cd2528c0db735 Mon Sep 17 00:00:00 2001 From: Dana Scheider Date: Sun, 20 Jul 2014 21:40:14 -0700 Subject: [PATCH 2/3] Clean up some short conditionals --- lib/sinatra-authentication.rb | 28 +++++++--------------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/lib/sinatra-authentication.rb b/lib/sinatra-authentication.rb index c65be72..9f82f9c 100755 --- a/lib/sinatra-authentication.rb +++ b/lib/sinatra-authentication.rb @@ -35,13 +35,9 @@ def self.registered(app) send settings.template_engine, get_view_as_string("show.#{settings.template_engine}"), :layout => use_layout? end - #convenience for ajax but maybe entirely stupid and unnecesary + #convenience for ajax but maybe entirely stupid and unnecessary app.get '/logged_in' do - if session[:user] - "true" - else - "false" - end + session[:user] ? "true" : "false" end app.get '/login/?' do @@ -56,9 +52,7 @@ def self.registered(app) if user = User.authenticate(params[:email], params[:password]) session[:user] = user.id - if Rack.const_defined?('Flash') - flash[:notice] = "Login successful." - end + flash[:notice] = "Login successful." if Rack.const_defined?('Flash') if session[:return_to] redirect_url = session[:return_to] @@ -68,18 +62,14 @@ def self.registered(app) redirect '/' end else - if Rack.const_defined?('Flash') - flash[:error] = "The email or password you entered is incorrect." - end + flash[:error] = "The email or password you entered is incorrect." if Rack.const_defined?('Flash') redirect '/login' end end app.get '/logout/?' do session[:user] = nil - if Rack.const_defined?('Flash') - flash[:notice] = "Logout successful." - end + flash[:notice] = "Logout successful." if Rack.const_defined?('Flash') return_to = ( session[:return_to] ? session[:return_to] : '/' ) redirect return_to end @@ -96,9 +86,7 @@ def self.registered(app) @user = User.set(params[:user]) if @user.valid && @user.id session[:user] = @user.id - if Rack.const_defined?('Flash') - flash[:notice] = "Account created." - end + flash[:notice] = "Account created." if Rack.const_defined?('Flash') redirect '/' else if Rack.const_defined?('Flash') @@ -127,9 +115,7 @@ def self.registered(app) end if user.update(user_attributes) - if Rack.const_defined?('Flash') - flash[:notice] = 'Account updated.' - end + flash[:notice] = 'Account updated.' if Rack.const_defined?('Flash') redirect '/' else if Rack.const_defined?('Flash') From c59469b9813b72aca9047902077e00b27144d6b8 Mon Sep 17 00:00:00 2001 From: danascheider Date: Sun, 20 Jul 2014 21:58:44 -0700 Subject: [PATCH 3/3] Extract helper method flash_defined? --- lib/sinatra-authentication.rb | 42 +++++++++++++++-------------------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/lib/sinatra-authentication.rb b/lib/sinatra-authentication.rb index 9f82f9c..3a8bb4f 100755 --- a/lib/sinatra-authentication.rb +++ b/lib/sinatra-authentication.rb @@ -52,7 +52,7 @@ def self.registered(app) if user = User.authenticate(params[:email], params[:password]) session[:user] = user.id - flash[:notice] = "Login successful." if Rack.const_defined?('Flash') + flash[:notice] = "Login successful." if flash_defined? if session[:return_to] redirect_url = session[:return_to] @@ -62,14 +62,14 @@ def self.registered(app) redirect '/' end else - flash[:error] = "The email or password you entered is incorrect." if Rack.const_defined?('Flash') + flash[:error] = "The email or password you entered is incorrect." if flash_defined? redirect '/login' end end app.get '/logout/?' do session[:user] = nil - flash[:notice] = "Logout successful." if Rack.const_defined?('Flash') + flash[:notice] = "Logout successful." if flash_defined? return_to = ( session[:return_to] ? session[:return_to] : '/' ) redirect return_to end @@ -86,10 +86,10 @@ def self.registered(app) @user = User.set(params[:user]) if @user.valid && @user.id session[:user] = @user.id - flash[:notice] = "Account created." if Rack.const_defined?('Flash') + flash[:notice] = "Account created." if flash_defined? redirect '/' else - if Rack.const_defined?('Flash') + if flash_defined? flash[:error] = "There were some problems creating your account: #{@user.errors}." end redirect '/signup?' + hash_to_query_string(params['user']) @@ -115,10 +115,10 @@ def self.registered(app) end if user.update(user_attributes) - flash[:notice] = 'Account updated.' if Rack.const_defined?('Flash') + flash[:notice] = 'Account updated.' if flash_defined? redirect '/' else - if Rack.const_defined?('Flash') + if flash_defined? flash[:error] = "Whoops, looks like there were some problems with your updates: #{user.errors}." end redirect "/users/#{user.id}/edit?" + hash_to_query_string(user_attributes) @@ -130,13 +130,9 @@ def self.registered(app) redirect "/users" unless current_user.admin? || current_user.id.to_s == params[:id] if User.delete(params[:id]) - if Rack.const_defined?('Flash') - flash[:notice] = "User deleted." - end + flash[:notice] = "User deleted." if flash_defined? else - if Rack.const_defined?('Flash') - flash[:error] = "Deletion failed." - end + flash[:error] = "Deletion failed." if flash_defined? end redirect '/' end @@ -193,12 +189,12 @@ def login_required end end + def flash_defined? + Rack.const_defined?('Flash') + end + def current_user - if session[:user] - User.get(:id => session[:user]) - else - GuestUser.new - end + session[:user] ? User.get(:id => session[:user]) : GuestUser.new end def logged_in? @@ -214,16 +210,14 @@ def get_view_as_string(filename) view = File.join(settings.sinatra_authentication_view_path, filename) data = "" f = File.open(view, "r") - f.each_line do |line| - data += line - end + f.each_line do {|line| data += line } return data end def render_login_logout(html_attributes = {:class => ""}) - css_classes = html_attributes.delete(:class) - parameters = '' - html_attributes.each_pair do |attribute, value| + css_classes = html_attributes.delete(:class) + parameters = '' + html_attributes.each_pair do |attribute, value| parameters += "#{attribute}=\"#{value}\" " end