Module: Sinatra::Helpers
- Defined in:
- lib/sinatra-authentication.rb,
lib/sinatra-authentication-o.rb
Instance Method Summary collapse
- #current_user ⇒ Object
-
#get_view_as_string(filename) ⇒ Object
BECAUSE sinatra 9.1.1 can’t load views from different paths properly.
- #hash_to_query_string(hash) ⇒ Object
- #logged_in? ⇒ Boolean
- #login_required ⇒ Object
- #render_facebook_connect_link(text = 'Login using facebook', options = {:size => 'small'}) ⇒ Object
- #render_login_logout(html_attributes = {:class => ""}) ⇒ Object
- #use_layout? ⇒ Boolean
Instance Method Details
#current_user ⇒ Object
208 209 210 211 212 213 214 |
# File 'lib/sinatra-authentication.rb', line 208 def current_user if session[:user] User.get(:id => session[:user]) else GuestUser.new end end |
#get_view_as_string(filename) ⇒ Object
BECAUSE sinatra 9.1.1 can’t load views from different paths properly
225 226 227 228 229 230 231 232 233 |
# File 'lib/sinatra-authentication.rb', line 225 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 return data end |
#hash_to_query_string(hash) ⇒ Object
193 194 195 |
# File 'lib/sinatra-authentication.rb', line 193 def hash_to_query_string(hash) hash.collect {|k,v| "#{k}=#{v}"}.join('&') end |
#logged_in? ⇒ Boolean
216 217 218 |
# File 'lib/sinatra-authentication.rb', line 216 def logged_in? !!session[:user] end |
#login_required ⇒ Object
197 198 199 200 201 202 203 204 205 206 |
# File 'lib/sinatra-authentication.rb', line 197 def login_required #not as efficient as checking the session. but this inits the fb_user if they are logged in if current_user.class != GuestUser return true else session[:return_to] = request.fullpath redirect '/login' return false end end |
#render_facebook_connect_link(text = 'Login using facebook', options = {:size => 'small'}) ⇒ Object
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/sinatra-authentication.rb', line 266 def render_facebook_connect_link(text = 'Login using facebook', = {:size => 'small'}) if [:size] == 'small' size = 'Small' elsif [:size] == 'medium' size = 'Medium' elsif [:size] == 'large' size = 'Large' elsif [:size] == 'xlarge' size = 'BigPun' else size = 'Small' end %[<a href="#" onclick="FB.Connect.requireSession(function(){document.location = '/connect';}); return false;" class="fbconnect_login_button FBConnectButton FBConnectButton_#{size}"> <span id="RES_ID_fb_login_text" class="FBConnectButton_Text"> #{text} </span> </a>] end |
#render_login_logout(html_attributes = {:class => ""}) ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/sinatra-authentication.rb', line 235 def render_login_logout(html_attributes = {:class => ""}) css_classes = html_attributes.delete(:class) parameters = '' html_attributes.each_pair do |attribute, value| parameters += "#{attribute}=\"#{value}\" " end result = "<div id='sinatra-authentication-login-logout' >" if logged_in? logout_parameters = html_attributes # a tad janky? logout_parameters.delete(:rel) result += "<a href='/users/#{current_user.id}/edit' class='#{css_classes} sinatra-authentication-edit' #{parameters}>Edit account</a> " if Sinatra.const_defined?('FacebookObject') if fb[:user] result += "<a href='javascript:FB.Connect.logoutAndRedirect(\"/logout\");' class='#{css_classes} sinatra-authentication-logout' #{logout_parameters}>Logout</a>" else result += "<a href='/logout' class='#{css_classes} sinatra-authentication-logout' #{logout_parameters}>Logout</a>" end else result += "<a href='/logout' class='#{css_classes} sinatra-authentication-logout' #{logout_parameters}>Logout</a>" end else result += "<a href='/signup' class='#{css_classes} sinatra-authentication-signup' #{parameters}>Signup</a> " result += "<a href='/login' class='#{css_classes} sinatra-authentication-login' #{parameters}>Login</a>" end result += "</div>" end |
#use_layout? ⇒ Boolean
220 221 222 |
# File 'lib/sinatra-authentication.rb', line 220 def use_layout? !request.xhr? end |