Description:
add_email_to_user, fix empty problem for_in_place_editing git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@42 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r18:d8813c40713d - - 5 files changed: 42 inserted, 4 deleted

@@ -0,0 +1,9
1 + class AddEmailToUsers < ActiveRecord::Migration
2 + def self.up
3 + add_column :users, :email, :string
4 + end
5 +
6 + def self.down
7 + remove_column :users, :email
8 + end
9 + end
@@ -1,27 +1,28
1 1 class UsersController < ApplicationController
2 2
3 3 before_filter :authenticate
4 4
5 5 verify :method => :post, :only => [:chg_passwd],
6 6 :redirect_to => { :action => :index }
7 7
8 8 in_place_edit_for :user, :full_name
9 - in_place_edit_for :user, :alias
9 + in_place_edit_for :user, :alias_for_editing
10 + in_place_edit_for :user, :email_for_editing
10 11
11 12 def index
12 13 @user = User.find(session[:user_id])
13 14 end
14 15
15 16 def chg_passwd
16 17 user = User.find(session[:user_id])
17 18 user.password = params[:passwd]
18 19 user.password_confirmation = params[:passwd_verify]
19 20 if user.save
20 21 flash[:notice] = 'password changed'
21 22 else
22 23 flash[:notice] = 'Error: password changing failed'
23 24 end
24 25 redirect_to :action => 'index'
25 26 end
26 27
27 28 end
@@ -1,45 +1,69
1 1 require 'digest/sha1'
2 2
3 3 class User < ActiveRecord::Base
4 4
5 5 has_and_belongs_to_many :roles
6 6
7 7 validates_presence_of :login
8 8 validates_presence_of :full_name
9 9
10 10 validates_presence_of :password, :if => :password_required?
11 11 validates_length_of :password, :within => 4..20, :if => :password_required?
12 12 validates_confirmation_of :password, :if => :password_required?
13 13
14 14 attr_accessor :password
15 15
16 16 before_save :encrypt_new_password
17 17
18 18 def self.authenticate(login, password)
19 19 user = find_by_login(login)
20 20 return user if user && user.authenticated?(password)
21 21 end
22 22
23 23 def authenticated?(password)
24 24 hashed_password == encrypt(password,salt)
25 25 end
26 26
27 27 def admin?
28 28 self.roles.detect {|r| r.name == 'admin' }
29 29 end
30 30
31 - # protected
31 + def email_for_editing
32 + if self.email!=nil
33 + self.email
34 + else
35 + "unknown"
36 + end
37 + end
38 +
39 + def email_for_editing=(e)
40 + self.email=e
41 + end
42 +
43 + def alias_for_editing
44 + if self.alias!=nil
45 + self.alias
46 + else
47 + "unknown"
48 + end
49 + end
50 +
51 + def alias_for_editing=(e)
52 + self.alias=e
53 + end
54 +
55 + protected
32 56 def encrypt_new_password
33 57 return if password.blank?
34 58 self.salt = (10+rand(90)).to_s
35 59 self.hashed_password = encrypt(password,salt)
36 60 end
37 61
38 62 def password_required?
39 63 hashed_password.blank? || !password.blank?
40 64 end
41 65
42 66 def encrypt(string,salt)
43 67 Digest::SHA1.hexdigest(salt + string)
44 68 end
45 69 end
@@ -1,32 +1,35
1 1
2 2 %h1 Your account settings
3 3
4 4 %p
5 5 You can edit your full name and alias. Just click on the text and edit it.
6 6
7 7
8 8 %table.uinfo
9 9 %tr
10 10 %th.uinfo Login
11 11 %td.uinfo= @user.login
12 12 %tr
13 13 %th.uinfo Full name
14 14 %td.uinfo= in_place_editor_field :user, 'full_name', {}, :rows => 1
15 15 %tr
16 16 %th.uinfo Alias
17 - %td.uinfo= in_place_editor_field :user, 'alias', {}, :rows => 1
17 + %td.uinfo= in_place_editor_field :user, 'alias_for_editing', {}, :rows => 1
18 + %tr
19 + %th.uinfo E-mail
20 + %td.uinfo= in_place_editor_field :user, 'email_for_editing', {}, :rows => 1
18 21 %tr
19 22 %th.uinfo Password
20 23 %td.uinfo
21 24 - form_tag :action => 'chg_passwd', :method => 'post' do
22 25 %table
23 26 %tr
24 27 %td= password_field_tag 'passwd'
25 28 %td (new)
26 29 %tr
27 30 %td= password_field_tag 'passwd_verify'
28 31 %td (verify)
29 32 %tr
30 33 %td{:colspan => "2"}
31 34 = submit_tag 'change password'
32 35
@@ -1,92 +1,93
1 1 # This file is auto-generated from the current state of the database. Instead of editing this file,
2 2 # please use the migrations feature of ActiveRecord to incrementally modify your database, and
3 3 # then regenerate this schema definition.
4 4 #
5 5 # Note that this schema.rb definition is the authoritative source for your database schema. If you need
6 6 # to create the application database on another system, you should be using db:schema:load, not running
7 7 # all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
8 8 # you'll amass, the slower it'll run and the greater likelihood for issues).
9 9 #
10 10 # It's strongly recommended to check this file into your version control system.
11 11
12 - ActiveRecord::Schema.define(:version => 11) do
12 + ActiveRecord::Schema.define(:version => 12) do
13 13
14 14 create_table "languages", :force => true do |t|
15 15 t.string "name", :limit => 10
16 16 t.string "pretty_name"
17 17 t.string "ext", :limit => 10
18 18 end
19 19
20 20 create_table "problems", :force => true do |t|
21 21 t.string "name", :limit => 30
22 22 t.string "full_name"
23 23 t.integer "full_score"
24 24 t.date "date_added"
25 25 t.boolean "available"
26 26 end
27 27
28 28 create_table "rights", :force => true do |t|
29 29 t.string "name"
30 30 t.string "controller"
31 31 t.string "action"
32 32 end
33 33
34 34 create_table "rights_roles", :id => false, :force => true do |t|
35 35 t.integer "right_id"
36 36 t.integer "role_id"
37 37 end
38 38
39 39 add_index "rights_roles", ["role_id"], :name => "index_rights_roles_on_role_id"
40 40
41 41 create_table "roles", :force => true do |t|
42 42 t.string "name"
43 43 end
44 44
45 45 create_table "roles_users", :id => false, :force => true do |t|
46 46 t.integer "role_id"
47 47 t.integer "user_id"
48 48 end
49 49
50 50 add_index "roles_users", ["user_id"], :name => "index_roles_users_on_user_id"
51 51
52 52 create_table "sessions", :force => true do |t|
53 53 t.string "session_id"
54 54 t.text "data"
55 55 t.datetime "updated_at"
56 56 end
57 57
58 58 add_index "sessions", ["session_id"], :name => "index_sessions_on_session_id"
59 59 add_index "sessions", ["updated_at"], :name => "index_sessions_on_updated_at"
60 60
61 61 create_table "submissions", :force => true do |t|
62 62 t.integer "user_id"
63 63 t.integer "problem_id"
64 64 t.integer "language_id"
65 65 t.text "source"
66 66 t.binary "binary"
67 67 t.datetime "submitted_at"
68 68 t.datetime "compiled_at"
69 69 t.text "compiler_message"
70 70 t.datetime "graded_at"
71 71 t.integer "points"
72 72 t.text "grader_comment"
73 73 end
74 74
75 75 add_index "submissions", ["user_id", "problem_id"], :name => "index_submissions_on_user_id_and_problem_id"
76 76
77 77 create_table "tasks", :force => true do |t|
78 78 t.integer "submission_id"
79 79 t.datetime "created_at"
80 80 end
81 81
82 82 create_table "users", :force => true do |t|
83 83 t.string "login", :limit => 10
84 84 t.string "full_name"
85 85 t.string "hashed_password"
86 86 t.string "salt", :limit => 5
87 87 t.string "alias"
88 + t.string "email"
88 89 end
89 90
90 91 add_index "users", ["login"], :name => "index_users_on_login", :unique => true
91 92
92 93 end
You need to be logged in to leave comments. Login now