Override mongoid-tree callback condition for default position

This commit is contained in:
chris 2013-10-07 18:23:54 +08:00
parent b3e16ce23b
commit 061225f56c
2 changed files with 8 additions and 1 deletions

View File

@ -52,6 +52,7 @@ class User
accepts_nested_attributes_for :attribute_values, :allow_destroy => true accepts_nested_attributes_for :attribute_values, :allow_destroy => true
before_create :initialize_desktop before_create :initialize_desktop
before_save :assign_default_position, :if => :assign_default_position?
before_save :rebuild_status_record before_save :rebuild_status_record
scope :remote_account, where(:nccu_id.ne => nil) scope :remote_account, where(:nccu_id.ne => nil)
scope :not_guest_user, all_of(:name.ne => "guest") scope :not_guest_user, all_of(:name.ne => "guest")
@ -289,7 +290,7 @@ class User
end end
end end
protected private
def rebuild_status_record def rebuild_status_record
self.status_record = {} self.status_record = {}
@ -299,4 +300,9 @@ class User
end end
end end
# override mongoid-tree's method
def assign_default_position?
!self['name'].eql?('guest') && (self.position.nil? || self.parent_id_changed?)
end
end end

View File

@ -92,6 +92,7 @@ namespace :new_ui do
end end
task :reset_user_position => :environment do 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.not_guest_user.each_with_index do |user, i|
user.update_attribute(:position, i) user.update_attribute(:position, i)
end end