Sha256: 5a46317983a9303414d34fec0bf6b9365f773ec819eb310dce10815ef9089716

Contents?: true

Size: 1000 Bytes

Versions: 4

Compression:

Stored size: 1000 Bytes

Contents

module ActiveSnapshot
  class SnapshotItem < ActiveRecord::Base
    self.table_name = "snapshot_items"

    if defined?(ProtectedAttributes)
      attr_accessible :object, :item_id, :item_type, :child_group_name
    end

    belongs_to :snapshot, class_name: 'ActiveSnapshot::Snapshot'
    belongs_to :item, polymorphic: true

    validates :snapshot_id, presence: true
    validates :item_id, presence: true, uniqueness: { scope: [:snapshot_id, :item_type] }
    validates :item_type, presence: true, uniqueness: { scope: [:snapshot_id, :item_id] }

    def object
      @object ||= YAML.load(self[:object]).with_indifferent_access
    end

    def object=(h)
      @object = nil
      self[:object] = YAML.dump(h)
    end

    def restore_item!
      ### Add any custom logic here
      
      if !item
        item_klass = item_type.constantize

        self.item = item_klass.new
      end

      item.assign_attributes(object)

      item.save!(validate: false, touch: false)
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
active_snapshot-0.2.2 lib/active_snapshot/models/snapshot_item.rb
active_snapshot-0.2.1 lib/active_snapshot/models/snapshot_item.rb
active_snapshot-0.2.0 lib/active_snapshot/models/snapshot_item.rb
active_snapshot-0.1.1 lib/active_snapshot/models/snapshot_item.rb