forked from git/git-scm.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoc_version.rb
64 lines (58 loc) · 1.78 KB
/
doc_version.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true
# t.belongs_to :version
# t.belongs_to :doc
# t.belongs_to :doc_file
# t.timestamps
# t.language
class DocVersion < ApplicationRecord
belongs_to :doc
belongs_to :version
belongs_to :doc_file
scope :with_includes, -> { includes(:doc) }
scope :for_version, lambda { |version|
where(language: "en").joins(:version).where(versions: { name: version }).limit(1).first
}
scope :latest_version, lambda { |lang = "en"|
where(language: lang).joins(:version).order("versions.vorder DESC").limit(1).first
}
scope :version_changes, -> { where(language: "en").with_includes.joins(:version).order("versions.vorder DESC") }
delegate :name, to: :version
delegate :committed, to: :version
# returns an array of the differences with 3 entries
# 0: additions
# 1: subtractions
# 2: 8 - (add + sub)
def diff(doc_version)
diff_out = Diffy::Diff.new(doc.plain, doc_version.doc.plain)
first_chars = diff_out.to_s.gsub(/(.)[^\n]*\n/, '\1')
adds = first_chars.count("+")
mins = first_chars.count("-")
total = mins + adds
if total > 8
min = (8.0 / total)
adds = (adds * min).round
mins = (mins * min).round
total = 8
end
[adds, mins, 8 - total]
rescue StandardError
[0, 0, 8]
end
def index
file = doc_file
doc = self.doc
client = ElasticClient.instance
begin
client.index index: ELASTIC_SEARCH_INDEX,
type: "man_doc",
id: file.name,
body: {
name: file.name,
blob_sha: doc.blob_sha,
text: doc.plain
}
rescue StandardError
nil
end
end
end