Class: Effective::Trash

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/effective/trash.rb

Instance Method Summary collapse

Instance Method Details

#detailsObject



26
27
28
# File 'app/models/effective/trash.rb', line 26

def details
  self[:details] || {}
end

#restore!Object

So this is a Trash item When we delete ourselves, we restore this trash item first



51
52
53
54
# File 'app/models/effective/trash.rb', line 51

def restore!
  to_object.save!(validate: false)
  destroy!
end

#to_objectObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/effective/trash.rb', line 30

def to_object
  raise 'no attributes to consider' unless details.kind_of?(Hash) && details[:attributes].present?

  resource = Effective::Resource.new(trashed_type)
  object = trashed_type.constantize.new(details[:attributes])

  resource.nested_resources.each do |association|
    if details[association.name].present? && object.respond_to?("#{association.name}_attributes=")
      nested_attributes = details[association.name].inject({}) do |h, (index, nested)|
        h[index] = nested[:attributes].except('id', association.inverse_of&.foreign_key); h
      end

      object.send("#{association.name}_attributes=", nested_attributes)
    end
  end

  object
end

#to_sObject



22
23
24
# File 'app/models/effective/trash.rb', line 22

def to_s
  trashed_to_s.presence || [trashed_type, trashed_id].join(' ').presence || 'New Trash item'
end