Class: Version

Inherits:
Object
  • Object
show all
Includes:
MongoMapper::Document
Defined in:
lib/versioned/version.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.check_indexesObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/versioned/version.rb', line 19

def check_indexes
  unless missing_indexes.empty?
    msg = "Indexes have not been created for MongoMapper versioned docs. Run `rake versioned:create_indexes`."
    if Kernel.const_defined?(:IRB)
      puts "Warning: #{msg}"
    else
      ::Rails.logger.warn(msg)
    end
  end
end

.create_indexesObject



38
39
40
41
42
# File 'lib/versioned/version.rb', line 38

def create_indexes
  required_indexes.each do |index|
    ensure_index *index
  end
end

.missing_indexesObject



30
31
32
33
34
35
36
# File 'lib/versioned/version.rb', line 30

def missing_indexes
  existing_index_names = self.collection.index_information.keys
  required_index_names = required_indexes.collect do |i| 
    i.first.collect { |k| "#{k[0]}_#{k[1]}" }.join('_')
  end
  required_index_names - existing_index_names
end

.required_indexesObject



44
45
46
47
48
49
50
# File 'lib/versioned/version.rb', line 44

def required_indexes
  [
    [[[:versioned_id, 1], [:versioned_type, 1], [:created_at, -1]], background: true ],
    [[[:versioned_id, 1], [:versioned_type, 1], [:id2, -1]], background: true],
    [[[:versioned_id, 1], [:versioned_type, 1], [:id2, 1]], background: true]
  ]
end

Instance Method Details

#rollbackObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/versioned/version.rb', line 53

def rollback
  versioned.rollback do
    trouble = []
    versioned.version_id = self.id
    self.doc.each_pair do |attr, val|
      mutator = "#{attr}="
      if versioned.respond_to?(mutator)
        versioned.send(mutator, val)
      else
        trouble.push(attr)
      end
    end
    unless trouble.empty?
      raise "Trying to load a #{versioned.class.name} version that has unsupported attributes: #{trouble.to_sentence}" 
    end
    versioned.versions.destroy_all(:id2.gte => self.id2)
    versioned.save
  end
end