Skip to content

Commit

Permalink
- implementing a simple flash
Browse files Browse the repository at this point in the history
- registration template and action

Signed-off-by: Balint Erdi <balint.erdi@gmail.com>
  • Loading branch information
balinterdi committed Feb 23, 2009
1 parent 4733c28 commit 232b53d
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 11 deletions.
51 changes: 51 additions & 0 deletions my_helpers.rb
@@ -0,0 +1,51 @@
require "ruby-debug"

module Sinatra::MyHelpers

def self.included(base)
base.class_eval do
include Flash
end
end

module Flash
# NOTE: Yes, at one point I peaked at how Rails
# implements the flash and now I pretty much
# try to implement a rudimentary version of it.
class FlashHash < Hash
def initialize
@keys_to_keep = []
end

def []=(key, value)
keep(key)
super
end

def keep(key)
# that key should be kept for the next action to be shown
@keys_to_keep << key
end

def reset
self.keys.each { |k| delete(k) unless @keys_to_keep.include?(k) }
# flash messages are only preserved for the next action
@keys_to_keep = []
end

end

def flash
if !defined?(@_flash)
@_flash = (session["flash"] ||= FlashHash.new)
end
@_flash
end

def reset_flash
@_flash.reset if defined?(@_flash)
session.delete("flash")
end
end

end
6 changes: 3 additions & 3 deletions spec/group_spec.rb
Expand Up @@ -12,12 +12,12 @@
it "should not be able to auth. with bad password" do
Group.authenticate(@group.login, @group.password + "x").should == nil
end

it "should be able to authenticate with the correct credentials" do
Group.authenticate(@group.login, @group.password).should == @group
end

it "should retain the logged in group" do
end

end
33 changes: 33 additions & 0 deletions spec/helpers_spec.rb
@@ -0,0 +1,33 @@
require File.dirname(__FILE__) + '/spec_helper'

describe "Sinatra::MyHelpers" do
before do
class Sinatra::Application
include Sinatra::MyHelpers
end
end

describe "param hash handling" do
describe "with no nested attributes" do
before do
# @params = { "status" => "all right" }
# @new_params = convert_with_nested_attributes(@params)
end

it "should not change the params hash" do
mock_app {
get '/' do
@params = { "status" => "all right" }
@new_params = convert_with_nested_attributes(@params)
end
}
get '/'
@params["status"].should == @new_params["status"]
end
end

it "makes a hash of value params if they have a []" do
pending
end
end
end
17 changes: 16 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -11,4 +11,19 @@
:logging => false
)

DataMapper.setup(:default, :adapter => 'sqlite3', :database => 'db/test.sqlite3')
DataMapper.setup(:default, :adapter => 'sqlite3', :database => 'db/test.sqlite3')

# copied from the helper.rb in sinatra's test library
module Sinatra::Test
# Sets up a Sinatra::Base subclass defined with the block
# given. Used in setup or individual spec methods to establish
# the application.
def mock_app(base=Sinatra::Base, &block)
@app = Sinatra.new(base, &block)
end
end

class Sinatra::Base
# Allow assertions in request context
include Test::Unit::Assertions
end
51 changes: 48 additions & 3 deletions the_pot.rb
Expand Up @@ -13,44 +13,89 @@
require "pot"
require "user"

require "my_helpers"

configure :development do
set :mode, 'development'
enable :sessions
DataMapper.setup(:default, :adapter => 'sqlite3', :database => 'db/development.sqlite3')
DataMapper.setup(:default, :adapter => 'sqlite3', :database => 'db/development.sqlite3')
end

helpers do

include Sinatra::MyHelpers

# alias_method :old_render, :render

old_render = instance_method(:render)

define_method(:render) do |engine, template, options|
# puts "XXX Hello"
old_render.bind(self).call(engine, template, options)
end

def do_auth
group = Group.authenticate(params[:login], params[:password])
self.current_group = group
return group
end

def current_group=(group)
session["group"] = group.id
end

def current_group
Group.get(session["group"])
end

end

before do
# reset_flash
end

get '/main.css' do
content_type 'text/css', :charset => 'utf-8'
sass :main
end

get '/register' do
haml :register
end

post '/register' do
if params[:group][:password] != params[:group][:re_password]
flash[:error] = "The passwords do not match."
haml :register
else
# NOTE: params[:group].delete(:re_password) will not work!
# probably that's because :re_password is not found as key
# when looking for the value to delete so it does not do anything.
params[:group].delete("re_password")
group = Group.create(params[:group])
if group
current_group = group
flash[:ok] = "The new group has been created."
redirect '/'
else
flash[:error] = "Bad data. Registration failed. Try again."
haml :register
end
end
end

get '/login' do
haml :login
end

post '/login' do
auth = do_auth
redirect '/secret' if auth
puts "invalid credentials."
haml :login
end

get '/' do
"I'm The Pot"
haml :dashboard
end

get '/secret' do
Expand Down
2 changes: 2 additions & 0 deletions views/dashboard.haml
@@ -0,0 +1,2 @@
/ %p= "Welcome, #{current_group.name}"
%p Welcome
9 changes: 8 additions & 1 deletion views/layout.haml
Expand Up @@ -7,5 +7,12 @@

/ %link{ :rel => "stylesheet", :href => "/stylesheets/main.css", :type => "text/css", :media => "screen" }
%body
#flash
- if flash[:ok]
.ok= flash[:ok]
- if flash[:error]
.ok= flash[:error]
#container
= yield
= yield
- reset_flash

4 changes: 2 additions & 2 deletions views/login.haml
Expand Up @@ -3,8 +3,8 @@
%li{ :class => "field" }
%label{ :for => "login" } Login
%input{ :type => "text", :id => "login", :name => "login" }
%li
%li{ :class => "field" }
%label{ :for => "password" } Password
%input{ :type => "password", :id => "password", :name => "password" }
%li
%li{ :class => "field" }
%input{ :type => "submit", :value => "Log in"}
2 changes: 1 addition & 1 deletion views/new_expense.haml
@@ -1 +1 @@
%div= current_group.inspect
%div= current_group
17 changes: 17 additions & 0 deletions views/register.haml
@@ -0,0 +1,17 @@
%form{ :name => "register", :action => "/register", :method => "post" }
%ul{ :class => "fieldset" }
%li{ :class => "field" }
%label{ :for => "name" } Name
%input{ :type => "text", :id => "name", :name => "group[name]" }
%li{ :class => "field" }
%label{ :for => "login" } Login
%input{ :type => "text", :id => "login", :name => "group[login]" }
%li{ :class => "field" }
%label{ :for => "password" } Password
%input{ :type => "password", :id => "password", :name => "group[password]" }
%li{ :class => "field" }
%label{ :for => "re-password" } Re-password
%input{ :type => "password", :id => "re-password", :name => "group[re_password]" }

%li{ :class => "field" }
%input{ :type => "submit", :value => "Register"}

0 comments on commit 232b53d

Please sign in to comment.