Module: Pottery::ClassMethods

Defined in:
lib/pottery.rb

Instance Method Summary collapse

Instance Method Details

#restore(name) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pottery.rb', line 20

def restore name
  existing = Soup[name]
  if existing && !(existing.is_a?(Array) && existing.empty?)
    if existing.is_a? Array
      last = existing.pop
      puts 'deleting duplicates for id: ' + name
      existing.each {|x| x.destroy}
      existing = last
    end
    instance = self.new
    attributes = existing.attributes
    unless attributes.empty?
      id_name = attributes.delete('name')
      name = attributes.delete('name_name')
      attributes['id_name'] = id_name if id_name
      attributes['name'] = name if name
      instance.morph attributes
    end
    instance
  else
    nil
  end
end