Sha256: 8d6d7c1b1a0f84b5b3e12c513c4d87ce4fe0ada855228a673d016ffb25bb84c2

Contents?: true

Size: 875 Bytes

Versions: 10

Compression:

Stored size: 875 Bytes

Contents

module Tenon
  class ItemVersion < Tenon::ApplicationRecord
    default_scope -> { order('created_at DESC') }
    scope :autosave, -> { where('save_type = ?', 'autosave') }

    belongs_to :item, polymorphic: true
    belongs_to :user, foreign_key: :creator_id
    before_create :destroy_old_autosave

    def self.orphans(item_type)
      where('item_type = ?', item_type)
      .where('item_id IS NULL OR item_id = ?', 0)
    end

    def attrs=(hash)
      super(Tenon::HasHistory::AttrSerializer.serialize(hash, self))
    end

    private

    def destroy_old_autosave
      same_user_autosave.offset(2).destroy_all if save_type == 'autosave'
    end

    def same_user_autosave
      if item
        item.versions.autosave.where(creator_id: creator_id)
      else
        self.class.autosave.orphans(item_type).where(creator_id: creator_id)
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
tenon-2.1.0 app/models/tenon/item_version.rb
tenon-2.0.8 app/models/tenon/item_version.rb
tenon-2.0.7 app/models/tenon/item_version.rb
tenon-2.0.6 app/models/tenon/item_version.rb
tenon-2.0.5 app/models/tenon/item_version.rb
tenon-2.0.4 app/models/tenon/item_version.rb
tenon-2.0.3 app/models/tenon/item_version.rb
tenon-2.0.2 app/models/tenon/item_version.rb
tenon-2.0.1 app/models/tenon/item_version.rb
tenon-2.0.0 app/models/tenon/item_version.rb