14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/active_record/when_change.rb', line 14
def when_change attr, config={}, method = nil, &block
return if self.new_record? || config.blank? || (method == nil && !block_given? )
config.keys.each{|key| logger.debug("#{key} is not valid options key for ArctiveRecordChanged") unless VALID_OPTIONS.include?(key)}
new_attribute = self.send(attr.to_sym)
old_attribute = self.send("#{attr}_was".to_sym)
return if new_attribute == old_attribute
correct_form = ( (config[:from] and ( config[:from] != old_attribute )) ? false : true )
correct_to = ( (config[:to] and ( config[:to] != new_attribute )) ? false : true )
correct_if = ( (config[:if] and ( eval(config[:if]) != true ) ) ? false : true )
correct_unless = ( (config[:unless] and (eval(config[:unless]) == true) )? false : true )
if correct_form && correct_to && correct_if && correct_unless
if block_given?
block.call(self)
else
send(method)
end
end
end
|