Module: Sinatra::Helpers

Defined in:
lib/sinatra-authentication.rb,
lib/sinatra-authentication-o.rb

Instance Method Summary collapse

Instance Method Details

#current_userObject



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

Returns:

  • (Boolean)


216
217
218
# File 'lib/sinatra-authentication.rb', line 216

def logged_in?
  !!session[:user]
end

#login_requiredObject



197
198
199
200
201
202
203
204
205
206
# File 'lib/sinatra-authentication.rb', line 197

def 
  #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


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', options = {:size => 'small'})
    if options[:size] == 'small'
      size = 'Small'
    elsif options[:size] == 'medium'
      size = 'Medium'
    elsif options[:size] == 'large'
      size = 'Large'
    elsif options[: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 (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

Returns:

  • (Boolean)


220
221
222
# File 'lib/sinatra-authentication.rb', line 220

def use_layout?
  !request.xhr?
end