Module: RTM::AR::TMDM::Movable

Included in:
Construct, Locator, ScopedObjectsTopic
Defined in:
lib/rtm/activerecord/tmdm.rb

Overview

Moves all fields in a collection to another owner (for classes using belongs_to in AR::Base class)

Instance Method Summary collapse

Instance Method Details

#move_all(*args) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rtm/activerecord/tmdm.rb', line 24

def move_all(*args)
  raise "All parameters must have a size of 3: [field_name, from, to]" unless args.all?{|a| a.size == 3}
  set_part = ""
  where_part = ""
  args.each do |field, from, to|
    set_part += ", " unless set_part.empty?
    where_part += " and " unless where_part.empty?
    from = "'#{from}'" unless from.is_a? Numeric
    to = "'#{to}'" unless to.is_a? Numeric
    where_part += "#{field} = #{from}"
    set_part += "#{field} = #{to}"
  end
  #puts "setpart: #{set_part.inspect}\nwherepart: #{where_part.inspect}"
  res = self.update_all(set_part, where_part)
  #puts "result: #{res.inspect}"
end