From 37232cd5b20c62cf87b8845927dbbc7c40c1a7a7 Mon Sep 17 00:00:00 2001 From: yanxd Date: Tue, 3 Jun 2014 16:41:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=8A=E6=89=80=E6=9C=89user=5Fextensions?= =?UTF-8?q?=E7=9A=84=E9=80=BB=E8=BE=91=E7=A7=BB=E5=88=B0=E4=BA=86user?= =?UTF-8?q?=E4=B8=AD=EF=BC=8C=E4=B8=9A=E5=8A=A1=E4=BB=A3=E7=A0=81=E8=BF=98?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E4=BF=AE=E6=94=B9,=E4=B8=8B=E4=B8=80?= =?UTF-8?q?=E6=AD=A5=E6=98=AFuser=5Fscore=E5=92=8Cprojectxxx,=E5=85=88?= =?UTF-8?q?=E6=B5=8B=E6=B5=8B=E6=95=88=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 4 +++ ReadMe.txt | 7 ++++++ app/models/user.rb | 47 +++++++++++++++++++++++++++++++++++ app/views/my/account.html.erb | 1 - 4 files changed, 58 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index a04059bf..a3cc7b68 100644 --- a/Gemfile +++ b/Gemfile @@ -19,6 +19,10 @@ gem 'acts-as-taggable-on' group :development do gem 'better_errors', path: 'lib/better_errors' gem 'rack-mini-profiler', path: 'lib/rack-mini-profiler' + if ENV['PRY'] + gem 'pry' + gem 'pry-nav' + end end # Optional gem for LDAP authentication diff --git a/ReadMe.txt b/ReadMe.txt index e7444a6c..ad9f0512 100644 --- a/ReadMe.txt +++ b/ReadMe.txt @@ -6,6 +6,13 @@ alias name firstname+lastname login nickname xxx login realname xxx firstname+lastname + +User model经过修改,已经完全集成了user_extensions的功能 +例如 +user.gender=1 +user.save +user_extensions字段会自动保存 +不必要每次user.user_extensions.xxx以及判断是否为空 ================================================================================ app/models/setting.rb :165 # fixed domain url in development. tantantan's bug diff --git a/app/models/user.rb b/app/models/user.rb index edf70387..63e0b383 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -199,6 +199,53 @@ class User < Principal end } + + # ====================================================================== + # 闆嗕腑澶勭悊 User 鎵╁睍琛ㄤ负绌虹殑闂 + validate :valid_user_extensions + before_save :save_user_extensions + + def extensions + self.user_extensions ||= self.create_user_extensions + end + + def respond_to?(m, include_private = false) + flag = extensions.respond_to? m.to_sym + if flag + flag + else + super + end + end + + def method_missing m, *args, &block + if extensions.respond_to? m.to_sym + self.class.send :define_method, m.to_sym, *args do + self.extensions.__send__ m.to_sym, *args + end + __send__ m.to_sym, *args, &block + + else + super + end + end + + def valid_user_extensions + if !self.extensions.valid? + self.extensions.errors.messages.each do |key, values| + values.each do |value| + self.errors.add key, value + end + end + end + end + + def save_user_extensions + self.extensions.save + end + # 闆嗕腑澶勭悊 User 鎵╁睍琛ㄤ负绌虹殑闂 < end + # ====================================================================== + #閫夋嫨椤圭洰鎴愬憳鏃舵樉绀虹殑鐢ㄦ埛淇℃伅鏂囧瓧 def userInfo info=self.nickname + ' (' + self.realname + ')'; diff --git a/app/views/my/account.html.erb b/app/views/my/account.html.erb index 80554565..3853af34 100644 --- a/app/views/my/account.html.erb +++ b/app/views/my/account.html.erb @@ -34,7 +34,6 @@

<%= l(:label_my_account) %>

<%= error_messages_for 'user' %> -<%= error_messages_for 'se' %>