Module: Sinatra::Helpers
- Defined in:
- lib/sinatra-authentication.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.
- #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
193 194 195 196 197 198 199 |
# File 'lib/sinatra-authentication.rb', line 193 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
210 211 212 213 214 215 216 217 218 |
# File 'lib/sinatra-authentication.rb', line 210 def get_view_as_string(filename) view = .sinatra_authentication_view_path + filename data = "" f = File.open(view, "r") f.each_line do |line| data += line end return data end |
#logged_in? ⇒ Boolean
201 202 203 |
# File 'lib/sinatra-authentication.rb', line 201 def logged_in? !!session[:user] end |
#login_required ⇒ Object
182 183 184 185 186 187 188 189 190 191 |
# File 'lib/sinatra-authentication.rb', line 182 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
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/sinatra-authentication.rb', line 251 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
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/sinatra-authentication.rb', line 220 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
205 206 207 |
# File 'lib/sinatra-authentication.rb', line 205 def use_layout? !request.xhr? end |