diff --git a/app/models/user.rb b/app/models/user.rb index 689cc7b2..331ff699 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -137,7 +137,7 @@ class User < Principal has_one :user_extensions,:dependent => :destroy ## end - default_scope -> { includes(:user_extensions, :user_score) } + # default_scope -> { includes(:user_extensions, :user_score) } scope :teacher, -> { joins(:user_extensions).where('user_extensions.identity = ?', UserExtensions::TEACHER) } @@ -201,6 +201,7 @@ class User < Principal # ====================================================================== # 集中处理 User 扩展表为空的问题 + # 合并 user_score 属性 validate :valid_user_extensions after_save :save_user_extensions @@ -208,21 +209,32 @@ class User < Principal self.user_extensions ||= UserExtensions.new end + def user_score_attr + self.user_score ||= UserScore.new + end + + alias_method :ori_respond_to?, :respond_to? def respond_to?(m, include_private = false) - flag = UserExtensions.instance_methods.include? m.to_sym - if flag - flag - else - super - end + flag = false + flag = ori_respond_to? m.to_sym unless flag + flag = UserExtensions.new.respond_to? m.to_sym unless flag + flag = UserScore.new.respond_to? m.to_sym unless flag + + flag 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.class.send(:define_method, "_meta_#{m}".to_sym) do |*args, &block| self.extensions.__send__ m.to_sym, *args end - __send__ m.to_sym, *args, &block + __send__ "_meta_#{m}".to_sym, *args, &block + + elsif user_score_attr.respond_to? m.to_sym + self.class.send(:define_method, "_meta_#{m}".to_sym) do |*args, &block| + self.user_score_attr.__send__ m.to_sym, *args + end + __send__ "_meta_#{m}".to_sym, *args, &block else super @@ -241,6 +253,7 @@ class User < Principal def save_user_extensions self.extensions.save + self.user_score_attr.save end # 集中处理 User 扩展表为空的问题 < end # ====================================================================== diff --git a/app/views/users/_show_score.html.erb b/app/views/users/_show_score.html.erb index 0297a4e0..447e04e8 100644 --- a/app/views/users/_show_score.html.erb +++ b/app/views/users/_show_score.html.erb @@ -1,9 +1,7 @@ -<% unless @user.id == 1%> <% messages_count = @user.messages.count %> <% messages_score = messages_count * 0.05%> <% finall_messages_score = messages_score %> -<% end %> <% journals_count = @user.journals.count %> <% journals_score = journals_count * 0.1 %> @@ -73,31 +71,6 @@ -