From 061225f56c8b27d94c84aeee808d8eee96e329c1 Mon Sep 17 00:00:00 2001 From: chris Date: Mon, 7 Oct 2013 18:23:54 +0800 Subject: [PATCH] Override mongoid-tree callback condition for default position --- app/models/user/user.rb | 8 +++++++- lib/tasks/new_ui.rake | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/app/models/user/user.rb b/app/models/user/user.rb index 90b61f6d4..2b1651fab 100644 --- a/app/models/user/user.rb +++ b/app/models/user/user.rb @@ -52,6 +52,7 @@ class User accepts_nested_attributes_for :attribute_values, :allow_destroy => true before_create :initialize_desktop + before_save :assign_default_position, :if => :assign_default_position? before_save :rebuild_status_record scope :remote_account, where(:nccu_id.ne => nil) scope :not_guest_user, all_of(:name.ne => "guest") @@ -289,7 +290,7 @@ class User end end - protected + private def rebuild_status_record self.status_record = {} @@ -299,4 +300,9 @@ class User end end + # override mongoid-tree's method + def assign_default_position? + !self['name'].eql?('guest') && (self.position.nil? || self.parent_id_changed?) + end + end diff --git a/lib/tasks/new_ui.rake b/lib/tasks/new_ui.rake index f212182fc..365165d19 100644 --- a/lib/tasks/new_ui.rake +++ b/lib/tasks/new_ui.rake @@ -92,6 +92,7 @@ namespace :new_ui do end task :reset_user_position => :environment do + User.where(email: /guest/).each{|d|d.update_attribute(:position, nil)} User.not_guest_user.each_with_index do |user, i| user.update_attribute(:position, i) end