class AttributeChanger::AttributeChange

Public Class Methods

from_obj(obj) click to toggle source
# File lib/attribute_changer/attribute_change.rb, line 15
def from_obj(obj); where("obj_type = ? AND obj_id = ?", obj.class.name, obj.id) end
pending() click to toggle source
# File lib/attribute_changer/attribute_change.rb, line 16
def pending; where("status = ?", 'pending') end
waiting() click to toggle source
# File lib/attribute_changer/attribute_change.rb, line 17
def waiting; where("status = ?", 'waiting') end

Public Instance Methods

dependent_attribs(o = obj) click to toggle source
# File lib/attribute_changer/attribute_change.rb, line 45
def dependent_attribs(o = obj)
  attrib_was = o.send "#{attrib}"
  o.send "#{attrib}=", value
  o.valid?
  da = o.errors.messages.map{|attr, errors| attr if errors.any?}.compact
  o.send "#{attrib}=", attrib_was
  o.valid?
  da
end
obj() click to toggle source
# File lib/attribute_changer/attribute_change.rb, line 26
def obj
  if obj_type && obj_id
    @obj ||= obj_type.find_by_id obj_id
  end
end
obj=(val) click to toggle source
# File lib/attribute_changer/attribute_change.rb, line 32
def obj=(val)
  self.obj_id = val.id
  self.obj_type = val.class.to_s
end
obj_type() click to toggle source
# File lib/attribute_changer/attribute_change.rb, line 20
def obj_type
  if val = read_attribute(:obj_type)
    val.constantize
  end
end
value() click to toggle source
# File lib/attribute_changer/attribute_change.rb, line 41
def value
   deserialize
end
value=(val) click to toggle source
# File lib/attribute_changer/attribute_change.rb, line 37
def value=(val)
  write_attribute :value, serialize(val)
end

Private Instance Methods

change_status() click to toggle source
# File lib/attribute_changer/attribute_change.rb, line 62
def change_status
  self.class.where('obj_id = ?', obj_id).where('obj_type = ?', obj_type.to_s).where('attrib = ?', attrib).
    where('status = ?', 'pending').where('created_at < ?', created_at).each do |o|
      o.update_attribute :status, 'obsolete'
  end
end
deserialize() click to toggle source
# File lib/attribute_changer/attribute_change.rb, line 73
def deserialize
  Marshal.load Base64.decode64(read_attribute(:value))
end
serialize(val) click to toggle source
# File lib/attribute_changer/attribute_change.rb, line 69
def serialize(val)
  Base64.encode64(Marshal.dump(val))
end
set_default_values() click to toggle source
# File lib/attribute_changer/attribute_change.rb, line 58
def set_default_values
  self.status ||= 'pending'
end