Class: Effective::Trash
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Effective::Trash
- Defined in:
- app/models/effective/trash.rb
Instance Method Summary collapse
- #details ⇒ Object
-
#restore! ⇒ Object
So this is a Trash item When we delete ourselves, we restore this trash item first.
- #to_object ⇒ Object
- #to_s ⇒ Object
Instance Method Details
#details ⇒ Object
30 31 32 |
# File 'app/models/effective/trash.rb', line 30 def details Hash(self[:details]) end |
#restore! ⇒ Object
So this is a Trash item When we delete ourselves, we restore this trash item first
55 56 57 58 |
# File 'app/models/effective/trash.rb', line 55 def restore! to_object.save!(validate: false) destroy! end |
#to_object ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/models/effective/trash.rb', line 34 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_s ⇒ Object
26 27 28 |
# File 'app/models/effective/trash.rb', line 26 def to_s trashed_to_s.presence || [trashed_type, trashed_id].join(' ').presence || 'New Trash item' end |