Module: Sinatra::Helpers

Defined in:
lib/redsafe.rb

Instance Method Summary collapse

Instance Method Details

#current_userObject



170
171
172
173
174
175
176
# File 'lib/redsafe.rb', line 170

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



187
188
189
190
191
192
193
194
195
# File 'lib/redsafe.rb', line 187

def get_view_as_string(filename)
  view = options.red_slash_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)


178
179
180
# File 'lib/redsafe.rb', line 178

def logged_in?
  !!session[:user]
end

#login_requiredObject



159
160
161
162
163
164
165
166
167
168
# File 'lib/redsafe.rb', line 159

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


228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/redsafe.rb', line 228

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



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/redsafe.rb', line 197

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)


182
183
184
# File 'lib/redsafe.rb', line 182

def use_layout?
  !request.xhr?
end