Module: Mover::ClassMethods

Defined in:
lib/mover.rb

Instance Method Summary collapse

Instance Method Details

#after_move(*to_class, &block) ⇒ Object



21
22
23
24
# File 'lib/mover.rb', line 21

def after_move(*to_class, &block)
  @after_move ||= []
  @after_move << [ to_class, block ]
end

#before_move(*to_class, &block) ⇒ Object



26
27
28
29
# File 'lib/mover.rb', line 26

def before_move(*to_class, &block)
  @before_move ||= []
  @before_move << [ to_class, block ]
end

#move_to(to_class, options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/mover.rb', line 31

def move_to(to_class, options={})
  from_class = self
  
  # Conditions
  conditions = options[:conditions] || '1'
  conditions = self.sanitize_sql(conditions)
  where = "WHERE #{conditions}"
  
  # Columns
  magic = options[:magic] || 'moved_at'
  insert = from_class.column_names & to_class.column_names
  insert -= [ magic ]
  insert.collect! { |col| connection.quote_column_name(col) }
  select = insert.clone
  
  # Magic columns
  if to_class.column_names.include?(magic)
    insert << connection.quote_column_name(magic)
    if options[:migrate]
      select << connection.quote_column_name(magic)
    else
      select << connection.quote(Time.now.utc)
    end
  end
  
  # Callbacks
  collector = lambda do |(classes, block)|
    classes.collect! { |c| eval(c.to_s) }
    block if classes.include?(to_class) || classes.empty?
  end
  before = (@before_move || []).collect(&collector).compact
  after = (@after_move || []).collect(&collector).compact
  
  # Instances
  instances =
    if options[:instance]
      [ options[:instance] ]
    elsif before.empty? && after.empty?
      []
    else
      self.find(:all, :conditions => conditions)
    end
  options.delete(:instance)
  
  # Callback executor
  exec_callbacks = lambda do |callbacks|
    callbacks.each do |block|
      instances.each do |instance|
        instance.move_options = options
        instance.instance_eval(&block)
        instance.move_options = nil
      end
    end
  end
  
  # Execute
  transaction do
    exec_callbacks.call before
    
    if options[:quick]
      connection.execute(<<-SQL)
        INSERT INTO #{to_class.table_name} (#{insert.join(', ')})
        SELECT #{select.join(', ')}
        FROM #{from_class.table_name}
        #{where}
      SQL
    elsif !options[:generic] && connection.class.to_s.include?('Mysql')
      update = insert.collect do |i|
        if i.include?("'")
          "#{to_class.table_name}.#{i} = #{i}"
        else
          "#{to_class.table_name}.#{i} = #{from_class.table_name}.#{i}"
        end
      end
      
      connection.execute(<<-SQL)
        INSERT INTO #{to_class.table_name} (#{insert.join(', ')})
        SELECT #{select.join(', ')}
        FROM #{from_class.table_name}
        #{where}
        ON DUPLICATE KEY
        UPDATE #{update.join(', ')};
      SQL
    else
      conditions.gsub!(to_class.table_name, 't')
      conditions.gsub!(from_class.table_name, 'f')
      select.collect! { |s| s.include?('`') ? "f.#{s}" : s }
      set = insert.collect do |i|
        if i.include?("'")
          "t.#{i} = #{i}"
        else
          "t.#{i} = f.#{i}"
        end
      end
      
      connection.execute(<<-SQL)
        UPDATE #{to_class.table_name}
          AS t
        INNER JOIN #{from_class.table_name}
          AS f
        ON f.id = t.id
          AND #{conditions}
        SET #{set.join(', ')}
      SQL
  
      connection.execute(<<-SQL)
        INSERT INTO #{to_class.table_name} (#{insert.join(', ')})
        SELECT #{select.join(', ')}
        FROM #{from_class.table_name}
          AS f
        LEFT OUTER JOIN #{to_class.table_name}
          AS t
        ON f.id = t.id
        WHERE (
          t.id IS NULL
          AND #{conditions}
        )
      SQL
    end
    
    unless options[:copy]
      connection.execute("DELETE FROM #{from_class.table_name} #{where}")
    end
    
    exec_callbacks.call after
  end
end

#reserve_idObject



159
160
161
162
163
164
165
166
# File 'lib/mover.rb', line 159

def reserve_id
  id = nil
  transaction do
    id = connection.insert("INSERT INTO #{self.table_name} () VALUES ()")
    connection.execute("DELETE FROM #{self.table_name} WHERE id = #{id}") if id
  end
  id
end