Module: Sinatra::Helpers

Defined in:
lib/sinatra-authentication.rb

Instance Method Summary collapse

Instance Method Details

#current_userObject



139
140
141
142
143
144
145
# File 'lib/sinatra-authentication.rb', line 139

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



156
157
158
159
160
161
162
163
164
# File 'lib/sinatra-authentication.rb', line 156

def get_view_as_string(filename)
  view = options.lil_authentication_view_path + filename
  data = ""
  f = File.open(view, "r")
  f.each_line do |line|
    data += line
  end
  return data
end

#logged_in?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/sinatra-authentication.rb', line 147

def logged_in?
  !!session[:user]
end

#login_requiredObject



129
130
131
132
133
134
135
136
137
# File 'lib/sinatra-authentication.rb', line 129

def 
  if session[:user]
    return true
  else
    session[:return_to] = request.fullpath
    redirect '/login'
    return false
  end
end

#render_login_logout(html_attributes = {:class => ""}) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/sinatra-authentication.rb', line 166

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> "
    result += "<a href='/logout' class='#{css_classes} sinatra-authentication-logout' #{logout_parameters}>logout</a>"
  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)


151
152
153
# File 'lib/sinatra-authentication.rb', line 151

def use_layout?
  !request.xhr?
end