From da03add37b83e536963fd9f3c659c786426913bc Mon Sep 17 00:00:00 2001 From: Wojciech Todryk Date: Sat, 23 Jul 2011 21:55:26 +0200 Subject: [PATCH] setup form finished --- app/controllers/application_controller.rb | 28 ++++++-- app/controllers/messages_controller.rb | 8 ++- app/controllers/user_controller.rb | 67 +++++++++++++++----- app/helpers/application_helper.rb | 50 +++++++++++++++ app/models/server.rb | 4 ++ app/models/user.rb | 6 ++ config/locales/en.yml | 5 ++ config/routes.rb | 1 + db/migrate/20110723115402_create_users.rb | 15 +++++ db/migrate/20110723153214_create_servers.rb | 14 ++++ db/schema.rb | 31 +++++++++ test/fixtures/servers.yml | 9 +++ test/fixtures/users.yml | 11 ++++ test/unit/server_test.rb | 8 +++ test/unit/user_test.rb | 8 +++ themes/olive/images/tick.png | Bin 0 -> 537 bytes themes/olive/stylesheets/base.css | 2 +- themes/olive/stylesheets/style.css | 7 +- themes/olive/views/layouts/simple.html.erb | 5 +- themes/olive/views/user/login.html.erb | 12 ++-- themes/olive/views/user/setup.html.erb | 20 ++++++ themes/olive/views/user/unknown.html.erb | 6 +- 22 files changed, 285 insertions(+), 32 deletions(-) create mode 100644 app/models/server.rb create mode 100644 app/models/user.rb create mode 100644 db/migrate/20110723115402_create_users.rb create mode 100644 db/migrate/20110723153214_create_servers.rb create mode 100644 db/schema.rb create mode 100644 test/fixtures/servers.yml create mode 100644 test/fixtures/users.yml create mode 100644 test/unit/server_test.rb create mode 100644 test/unit/user_test.rb create mode 100644 themes/olive/images/tick.png create mode 100755 themes/olive/views/user/setup.html.erb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0ad5869..004d67d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,9 +3,7 @@ require 'yaml' class ApplicationController < ActionController::Base protect_from_forgery - - before_filter :load_defaults - before_filter :set_locale + before_filter :load_defaults,:set_locale,:current_user protected @@ -14,11 +12,31 @@ class ApplicationController < ActionController::Base end def theme_resolver - @defaults['theme'] + if @current_user.nil? + @defaults['theme'] + else + @current_user.theme + end end def set_locale - I18n.locale = @defaults['locale'] || I18n.default_locale + if @current_user.nil? + I18n.locale = @defaults['locale'] || I18n.default_locale + else + I18n.locale = @current_user.locale || I18n.default_locale + end + end + + def current_user + @current_user ||= User.find(session[:user_id]) if session[:user_id] + end + + def check_current_user + if @current_user.nil? + session["return_to"] = request.fullpath + redirect_to :controller=>"user", :action => "login" + return false + end end end diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 0a33b1f..3ddfcfb 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -1,5 +1,9 @@ class MessagesController < ApplicationController - def index - end + + before_filter :check_current_user + theme :theme_resolver + + def index + end end diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index e70a6b0..a4cfe5b 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -1,26 +1,59 @@ class UserController < ApplicationController - theme :theme_resolver - layout "simple" + theme :theme_resolver + layout "simple" - def login - end + def login + end - def logout - reset_session - flash[:notice] = t(:user_logged_out) - redirect_to :action => "login" - end + def logout + reset_session + flash[:notice] = t(:user_logged_out) + redirect_to :action => "login" + end - def authenticate - redirect_to :action => 'unknown' - #redirect_to :controller => "messages", :action => "index" - end + def authenticate + user = User.find_by_email(params["user"]["email"]) + if user.nil? + redirect_to :action => 'unknown' + else + auten = false + if auten == true - def setup - end + else + flash[:error] = t(:login_failure) + redirect_to :action => 'login' + end + end + end - def unknown - end + def loginfailure + end + + def setup + @user = User.new + @server = Server.new + end + + def unknown + end + + def create + @user = User.new + @server = Server.new + @user.email = params["user_email"] + @user.first_name = params["user_first_name"] + @user.last_name = params["user_last_name"] + @server.name = params["server_name"] + if @user.valid? and @server.valid? + @user.save + @server.user_id = @user.id + @server.save + flash[:notice] = t(:setup_done) + redirect_to :action => 'login' + else + render "setup" + end + end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index de6be79..8db6bf8 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,2 +1,52 @@ module ApplicationHelper + +def form_field(object,field,flabel,example) + + html = "" + html << "
" + if object.errors[field.to_sym] + html << "
" + + end + html << "" + if object.errors[field.to_sym] + html << " " + html << object.errors[field.to_sym].to_s + html << "" + html << "
" + end + html << "" + html << "" + html << t(:example) + html << ": " + html << example + html << "" + html << "
" + +end + +def form_button(text) + html = "" + html << "
" + html << "
" +end + end diff --git a/app/models/server.rb b/app/models/server.rb new file mode 100644 index 0000000..3897d85 --- /dev/null +++ b/app/models/server.rb @@ -0,0 +1,4 @@ +class Server < ActiveRecord::Base + validates_presence_of :name + belongs_to :user +end diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..87710b9 --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,6 @@ +class User < ActiveRecord::Base + validates_presence_of :email, :on => :create + validates_presence_of :first_name,:last_name + validates_uniqueness_of :email + has_many :servers, :dependent => :destroy +end diff --git a/config/locales/en.yml b/config/locales/en.yml index 376c0d1..2f7e42c 100755 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -71,3 +71,8 @@ en: unknown_user_flash: Your email identifier was not found in database unknown_user_login: Go to login page and try to login once more. unknown_user_setup: Go to setup page and do the setup of Your mail account. + setup_title: Setup + example: example + server_name: Server name + setup_done: Setup done. Please log in + login_failure: Login failure. Bad email or password diff --git a/config/routes.rb b/config/routes.rb index 8e2963d..660dcb6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,7 @@ Mailr::Application.routes.draw do get "messages/index" get "user/logout" post "user/authenticate" + post "user/create" get "user/login" get "user/setup" get "user/unknown" diff --git a/db/migrate/20110723115402_create_users.rb b/db/migrate/20110723115402_create_users.rb new file mode 100644 index 0000000..2232901 --- /dev/null +++ b/db/migrate/20110723115402_create_users.rb @@ -0,0 +1,15 @@ +class CreateUsers < ActiveRecord::Migration + def self.up + create_table :users do |t| + t.string :email + t.string :first_name + t.string :last_name + + t.timestamps + end + end + + def self.down + drop_table :users + end +end diff --git a/db/migrate/20110723153214_create_servers.rb b/db/migrate/20110723153214_create_servers.rb new file mode 100644 index 0000000..e69c4aa --- /dev/null +++ b/db/migrate/20110723153214_create_servers.rb @@ -0,0 +1,14 @@ +class CreateServers < ActiveRecord::Migration + def self.up + create_table :servers do |t| + t.string :name + t.string :port + t.references :user + t.timestamps + end + end + + def self.down + drop_table :servers + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..3c49415 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,31 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended to check this file into your version control system. + +ActiveRecord::Schema.define(:version => 20110723153214) do + + create_table "servers", :force => true do |t| + t.string "name" + t.string "port" + t.integer "user_id" + t.datetime "created_at" + t.datetime "updated_at" + end + + create_table "users", :force => true do |t| + t.string "email" + t.string "first_name" + t.string "last_name" + t.datetime "created_at" + t.datetime "updated_at" + end + +end diff --git a/test/fixtures/servers.yml b/test/fixtures/servers.yml new file mode 100644 index 0000000..f44a2a8 --- /dev/null +++ b/test/fixtures/servers.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html + +one: + name: MyString + port: MyString + +two: + name: MyString + port: MyString diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 0000000..7060ff2 --- /dev/null +++ b/test/fixtures/users.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html + +one: + email: MyString + first_name: MyString + last_name: MyString + +two: + email: MyString + first_name: MyString + last_name: MyString diff --git a/test/unit/server_test.rb b/test/unit/server_test.rb new file mode 100644 index 0000000..2e90285 --- /dev/null +++ b/test/unit/server_test.rb @@ -0,0 +1,8 @@ +require 'test_helper' + +class ServerTest < ActiveSupport::TestCase + # Replace this with your real tests. + test "the truth" do + assert true + end +end diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb new file mode 100644 index 0000000..a64d2d3 --- /dev/null +++ b/test/unit/user_test.rb @@ -0,0 +1,8 @@ +require 'test_helper' + +class UserTest < ActiveSupport::TestCase + # Replace this with your real tests. + test "the truth" do + assert true + end +end diff --git a/themes/olive/images/tick.png b/themes/olive/images/tick.png new file mode 100644 index 0000000000000000000000000000000000000000..a9925a06ab02db30c1e7ead9c701c15bc63145cb GIT binary patch literal 537 zcmV+!0_OdRP)Hs{AQG2a)rMyf zFQK~pm1x3+7!nu%-M`k}``c>^00{o_1pjWJUTfl8mg=3qGEl8H@}^@w`VUx0_$uy4 z2FhRqKX}xI*?Tv1DJd8z#F#0c%*~rM30HE1@2o5m~}ZyoWhqv>ql{V z1ZGE0lgcoK^lx+eqc*rAX1Ky;Xx3U%u#zG!m-;eD1Qsn@kf3|F9qz~|95=&g3(7!X zB}JAT>RU;a%vaNOGnJ%e1=K6eAh43c(QN8RQ6~GP%O}Jju$~Ld*%`mO1p - <%=t(:mailr) %> + <%=t(:mailr) %> - + <%= yield :title %> + <%=stylesheet_link_tag current_theme_stylesheet_path('base') %> <%=stylesheet_link_tag current_theme_stylesheet_path('style') %> <%= yield %> + diff --git a/themes/olive/views/user/login.html.erb b/themes/olive/views/user/login.html.erb index 4ff69b7..b28867c 100755 --- a/themes/olive/views/user/login.html.erb +++ b/themes/olive/views/user/login.html.erb @@ -1,6 +1,10 @@ +<% content_for :title do %> + <%= t(:please_login) %> +<% end %> +
-
-