Class: AttributeChanger::AttributeChange

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/attribute_changer/attribute_change.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_obj(obj) ⇒ Object



15
# 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

.pendingObject



16
# File 'lib/attribute_changer/attribute_change.rb', line 16

def pending; where("status = ?", 'pending') end

.waitingObject



17
# File 'lib/attribute_changer/attribute_change.rb', line 17

def waiting; where("status = ?", 'waiting') end

Instance Method Details

#dependent_attribs(o = obj) ⇒ Object



45
46
47
48
49
50
51
52
53
# 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

#objObject



26
27
28
29
30
# 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) ⇒ Object



32
33
34
35
# 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_typeObject



20
21
22
23
24
# File 'lib/attribute_changer/attribute_change.rb', line 20

def obj_type
  if val = read_attribute(:obj_type)
    val.constantize
  end
end

#valueObject



41
42
43
# File 'lib/attribute_changer/attribute_change.rb', line 41

def value
   deserialize
end

#value=(val) ⇒ Object



37
38
39
# File 'lib/attribute_changer/attribute_change.rb', line 37

def value=(val)
  write_attribute :value, serialize(val)
end