Sha256: dec2dd804a2845a06645412de697882cf0618c3950cab09728e15129a00ad93c

Contents?: true

Size: 869 Bytes

Versions: 83

Compression:

Stored size: 869 Bytes

Contents

module Tenon
  class ItemVersion < ActiveRecord::Base
    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

83 entries across 83 versions & 1 rubygems

Version Path
tenon-1.1.8 app/models/tenon/item_version.rb
tenon-1.1.7 app/models/tenon/item_version.rb
tenon-1.1.6 app/models/tenon/item_version.rb
tenon-1.1.5 app/models/tenon/item_version.rb
tenon-1.1.4 app/models/tenon/item_version.rb
tenon-1.1.3 app/models/tenon/item_version.rb
tenon-1.1.2 app/models/tenon/item_version.rb
tenon-1.1.1 app/models/tenon/item_version.rb
tenon-1.0.76 app/models/tenon/item_version.rb
tenon-1.0.75 app/models/tenon/item_version.rb
tenon-1.0.74 app/models/tenon/item_version.rb
tenon-1.0.73 app/models/tenon/item_version.rb
tenon-1.0.72 app/models/tenon/item_version.rb
tenon-1.0.71 app/models/tenon/item_version.rb
tenon-1.0.70 app/models/tenon/item_version.rb
tenon-1.0.69 app/models/tenon/item_version.rb
tenon-1.0.68 app/models/tenon/item_version.rb
tenon-1.0.67 app/models/tenon/item_version.rb
tenon-1.0.66 app/models/tenon/item_version.rb
tenon-1.0.65 app/models/tenon/item_version.rb