From 2e2268104651a50d21f71c63a7161aedee659ff5 Mon Sep 17 00:00:00 2001 From: Danilo Resende Date: Thu, 8 Nov 2012 11:29:43 -0200 Subject: [PATCH] =?UTF-8?q?Refatorando=20modelo=20de=20usu=C3=A1rios=20par?= =?UTF-8?q?a=20usar=20primeiro=20nome=20e=20sobrenome?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/admin/users_controller.rb | 2 +- app/controllers/application_controller.rb | 2 ++ app/models/user.rb | 16 +++++++++++- app/views/admin/users/_form.html.erb | 3 ++- config/locales/models/users/pt-BR.yml | 2 ++ ...193644_add_first_and_last_name_to_users.rb | 26 +++++++++++++++++++ db/schema.rb | 5 ++-- lib/cranelift.rb | 2 -- 8 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20121107193644_add_first_and_last_name_to_users.rb diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index db844d8..d67bb79 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -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 diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4d4605c..ba59256 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,6 +1,8 @@ class ApplicationController < ActionController::Base protect_from_forgery + include Cranelift::Auth + # Adiciona método de log include Log::CrLogger end diff --git a/app/models/user.rb b/app/models/user.rb index e939b56..0742595 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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} @@ -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) @@ -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? diff --git a/app/views/admin/users/_form.html.erb b/app/views/admin/users/_form.html.erb index e6454cf..5e974ef 100644 --- a/app/views/admin/users/_form.html.erb +++ b/app/views/admin/users/_form.html.erb @@ -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 %> diff --git a/config/locales/models/users/pt-BR.yml b/config/locales/models/users/pt-BR.yml index ad6ceae..03f0778 100644 --- a/config/locales/models/users/pt-BR.yml +++ b/config/locales/models/users/pt-BR.yml @@ -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 diff --git a/db/migrate/20121107193644_add_first_and_last_name_to_users.rb b/db/migrate/20121107193644_add_first_and_last_name_to_users.rb new file mode 100644 index 0000000..545e450 --- /dev/null +++ b/db/migrate/20121107193644_add_first_and_last_name_to_users.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 1079f16..5d8d43d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -92,7 +92,6 @@ 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" @@ -100,6 +99,8 @@ 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 diff --git a/lib/cranelift.rb b/lib/cranelift.rb index 14f609d..d12761c 100644 --- a/lib/cranelift.rb +++ b/lib/cranelift.rb @@ -4,7 +4,5 @@ require 'cranelift/scm' require 'cranelift/auth' -ApplicationController.send(:include, Cranelift::Auth) - module Cranelift end