Class: CompletionKit::MetricVersion

Inherits:
ApplicationRecord show all
Defined in:
app/models/completion_kit/metric_version.rb

Constant Summary collapse

STATES =
%w[draft published].freeze

Constants inherited from ApplicationRecord

ApplicationRecord::TenantScopedUniquenessValidator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ensure_current_for(metric) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'app/models/completion_kit/metric_version.rb', line 20

def self.ensure_current_for(metric)
  current.find_by(metric_id: metric.id) || create!(
    metric: metric,
    instruction: metric.instruction,
    rubric_bands: metric.rubric_bands,
    current: true,
    state: "published",
    published_at: Time.current
  )
end

Instance Method Details

#as_json(options = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/models/completion_kit/metric_version.rb', line 101

def as_json(options = {})
  {
    id: id,
    metric_id: metric_id,
    version_number: version_number,
    instruction: instruction,
    rubric_bands: rubric_bands,
    current: current,
    state: state,
    source: source,
    published_at: published_at,
    created_at: created_at
  }
end

#change_summary_against(previous) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/completion_kit/metric_version.rb', line 43

def change_summary_against(previous)
  return nil if previous.nil?

  instruction_changed = previous.instruction.to_s.strip != instruction.to_s.strip
  rubric_changes = rubric_band_change_count(previous)
  return nil unless instruction_changed || rubric_changes.positive?

  dimensions = []
  dimensions << "instruction" if instruction_changed
  dimensions << "rubric" if rubric_changes.positive?

  words_changed = 0
  if instruction_changed
    old_words = previous.instruction.to_s.split
    new_words = instruction.to_s.split
    words_changed = (old_words - new_words).size + (new_words - old_words).size
  end

  magnitude = if rubric_changes >= 2 || (instruction_changed && rubric_changes >= 1) || words_changed >= 15
    :major
  elsif rubric_changes == 1 || words_changed >= 4
    :minor
  else
    :trivial
  end

  { magnitude: magnitude, label: "#{magnitude.to_s.capitalize} #{dimensions.to_sentence} changes" }
end

#draft?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'app/models/completion_kit/metric_version.rb', line 31

def draft?
  state == "draft"
end

#publish!Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/completion_kit/metric_version.rb', line 72

def publish!
  MetricVersion.transaction do
    self.class.where(metric_id: metric_id).where.not(id: id).update_all(current: false)
    reload
    update!(state: "published", current: true, published_at: published_at || Time.current)
    metric.update_columns(
      instruction: instruction,
      rubric_bands: Array(rubric_bands).to_json
    )
  end
  self
end

#published?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'app/models/completion_kit/metric_version.rb', line 35

def published?
  state == "published"
end

#revert!Object

Raises:

  • (ArgumentError)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/completion_kit/metric_version.rb', line 85

def revert!
  raise ArgumentError, "only a published version can be reverted to" unless published?
  audit = nil
  MetricVersion.transaction do
    audit = self.class.create!(
      metric: metric,
      instruction: instruction,
      rubric_bands: rubric_bands,
      state: "draft",
      source: "revert"
    )
    audit.publish!
  end
  audit
end

#version_labelObject



39
40
41
# File 'app/models/completion_kit/metric_version.rb', line 39

def version_label
  "v#{version_number}"
end