Skip to content

Commit

Permalink
Refatorando modelo de usuários para usar primeiro nome e sobrenome
Browse files Browse the repository at this point in the history
  • Loading branch information
daniloisr committed Nov 8, 2012
1 parent 8be7a7b commit 2e22681
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Admin::UsersController < ApplicationController
before_filter :authenticate!, :only_admin!

def index
@users = User.order('LOWER(name)').page(params[:page]).per(10)
@users = User.order('LOWER(first_name)').page(params[:page]).per(10)
end

def new
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class ApplicationController < ActionController::Base
protect_from_forgery

include Cranelift::Auth

# Adiciona método de log
include Log::CrLogger
end
16 changes: 15 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class User < ActiveRecord::Base
:presence => true,
:length => { :in => 2..32 }

validates :name,
validates :first_name,
:presence => true,
:length => {:in => 3..32}

Expand All @@ -24,6 +24,10 @@ class User < ActiveRecord::Base
:uniqueness => true,
:format => { :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i }

def name
[self.first_name, self.last_name].join(' ')
end

# TODO fazer login com LDAP http://rubygems.org/gems/net-ldap
# http://redmine.rubyforge.org/svn/trunk/app/models/auth_source_ldap.rb
def self.authenticate(login, password)
Expand Down Expand Up @@ -52,6 +56,16 @@ def send_password_reset
UserMailer.password_reset(self).deliver
end

def name=(name)
names = name.split
self.first_name = names.shift
self.last_name = names.join(' ')
end

def name
[self.first_name, self.last_name].join(' ')
end

private
def encrypt_password
if password.present?
Expand Down
3 changes: 2 additions & 1 deletion app/views/admin/users/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

<%= f.input :ip_block %>

<%= f.input :name %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :email %>

<%= f.input :active %>
Expand Down
2 changes: 2 additions & 0 deletions config/locales/models/users/pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pt-BR:
ip_block: Bloquear IP?
last_action: Última ação
name: Nome
first_name: Nome
last_name: Sobrenome
email: Email
active: Ativo
login_type: Tipo de login
26 changes: 26 additions & 0 deletions db/migrate/20121107193644_add_first_and_last_name_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class AddFirstAndLastNameToUsers < ActiveRecord::Migration
def up
add_column :users, :first_name, :string
add_column :users, :last_name, :string

User.all.each do |user|
names = user.name.split
user.first_name = names.shift
user.last_name = names.join(' ')
user.save
end

remove_column :users, :name
end

def down
add_column :users, :name, :string

User.all.each do |user|
user.name = [user.first_name, user.last_name].join(' ')
end

remove_column :users, :first_name
remove_column :users, :last_name
end
end
5 changes: 3 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20121012140623) do
ActiveRecord::Schema.define(:version => 20121107193644) do

create_table "ips", :force => true do |t|
t.string "ip"
Expand Down Expand Up @@ -92,14 +92,15 @@
t.integer "role_id"
t.boolean "ip_block", :default => true
t.datetime "last_action"
t.string "name"
t.string "email"
t.boolean "active", :default => false
t.string "login_type"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "password_reset_token"
t.datetime "password_reset_sent_at"
t.string "first_name"
t.string "last_name"
end

end
2 changes: 0 additions & 2 deletions lib/cranelift.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@
require 'cranelift/scm'
require 'cranelift/auth'

ApplicationController.send(:include, Cranelift::Auth)

module Cranelift
end

0 comments on commit 2e22681

Please sign in to comment.