Class: CompletionKit::MetricsController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- CompletionKit::MetricsController
- Includes:
- TagFiltering
- Defined in:
- app/controllers/completion_kit/metrics_controller.rb
Constant Summary
Constants inherited from ApplicationController
ApplicationController::ONBOARDING_DISMISS_COOKIE
Instance Method Summary collapse
- #adopt_starter ⇒ Object
- #create ⇒ Object
- #destroy ⇒ Object
- #dismiss_starter ⇒ Object
- #dismiss_suggestion ⇒ Object
- #edit ⇒ Object
- #index ⇒ Object
- #new ⇒ Object
- #publish_draft ⇒ Object
- #show ⇒ Object
- #starter_preview ⇒ Object
- #suggest_variants ⇒ Object
- #update ⇒ Object
Instance Method Details
#adopt_starter ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'app/controllers/completion_kit/metrics_controller.rb', line 16 def adopt_starter starter = StarterMetrics.find(params[:key]) return redirect_to(metrics_path, alert: "Unknown starter metric.") unless starter if Metric.exists?(name: starter.name) return redirect_to(metrics_path, alert: "A metric named \"#{starter.name}\" already exists.") end metric = Metric.create!( name: starter.name, instruction: starter.instruction, rubric_bands: starter.rubric_bands ) redirect_to metric_path(metric), notice: "Added the \"#{starter.name}\" starter. Tweak any band before you run a judge against it." end |
#create ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'app/controllers/completion_kit/metrics_controller.rb', line 60 def create @metric = Metric.new(metric_params) if @metric.save redirect_to metric_path(@metric), notice: "Metric was successfully created." else render :new, status: :unprocessable_entity end end |
#destroy ⇒ Object
110 111 112 113 |
# File 'app/controllers/completion_kit/metrics_controller.rb', line 110 def destroy @metric.destroy redirect_to metrics_path, notice: "Metric was successfully destroyed." end |
#dismiss_starter ⇒ Object
30 31 32 33 34 35 |
# File 'app/controllers/completion_kit/metrics_controller.rb', line 30 def dismiss_starter starter = StarterMetrics.find(params[:key]) return redirect_to(metrics_path, alert: "Unknown starter metric.") unless starter StarterMetricDismissal.find_or_create_by(starter_key: starter.key) redirect_to metrics_path, notice: "Dismissed \"#{starter.name}\". It won't appear here again." end |
#dismiss_suggestion ⇒ Object
140 141 142 143 144 145 146 |
# File 'app/controllers/completion_kit/metrics_controller.rb', line 140 def dismiss_suggestion draft = MetricVersion.drafts.where(metric_id: @metric.id).find_by(id: params[:draft_id]) label = draft&.version_label draft&.destroy target = params[:back_to] == "edit" ? edit_metric_path(@metric) : metric_path(@metric) redirect_to target, notice: label ? "Discarded draft #{label}." : "Draft already gone." end |
#edit ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'app/controllers/completion_kit/metrics_controller.rb', line 48 def edit @suggestion_draft = MetricVersion.drafts.where(metric_id: @metric.id, source: "suggestion").order(created_at: :desc).first @edit_draft = MetricVersion.drafts.where(metric_id: @metric.id, source: "edit").order(created_at: :desc).first @published_metric_version = MetricVersion.published.where(metric_id: @metric.id, current: true).first @improve_disagreement_count = Calibration.where(metric_id: @metric.id, verdict: "disagree").count if @edit_draft @metric.instruction = @edit_draft.instruction @metric.rubric_bands = @edit_draft.rubric_bands end end |
#index ⇒ Object
6 7 8 9 |
# File 'app/controllers/completion_kit/metrics_controller.rb', line 6 def index @metrics = apply_tag_filter(Metric.includes(:metric_groups, :tags).order(:name)) @available_starters = StarterMetrics.available end |
#new ⇒ Object
44 45 46 |
# File 'app/controllers/completion_kit/metrics_controller.rb', line 44 def new @metric = Metric.new end |
#publish_draft ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'app/controllers/completion_kit/metrics_controller.rb', line 148 def publish_draft scope = MetricVersion.where(metric_id: @metric.id) version = if params[:draft_id].present? scope.find_by(id: params[:draft_id]) else MetricVersion.drafts.where(metric_id: @metric.id).order(created_at: :desc).first end if version.nil? redirect_to metric_path(@metric), alert: "No version to publish." return end was_published_already = version.published? reverting = was_published_already && !version.current? previously_current = MetricVersion.current.find_by(metric_id: @metric.id) if reverting audit = version.revert! prior_label = previously_current.version_label redirect_to metric_path(@metric), notice: "Reverted #{@metric.name} to #{version.version_label} (logged as #{audit.version_label}). Human reviews collected against #{prior_label} stay tied to it." else version.publish! redirect_to metric_path(@metric), notice: "#{@metric.name} #{version.version_label} is now the published version." end end |
#show ⇒ Object
37 38 39 40 41 42 |
# File 'app/controllers/completion_kit/metrics_controller.rb', line 37 def show @edit_draft = MetricVersion.drafts.where(metric_id: @metric.id, source: "edit").order(created_at: :desc).first @suggestion_draft = MetricVersion.drafts.where(metric_id: @metric.id, source: "suggestion").order(created_at: :desc).first @improve_disagreement_count = Calibration.where(metric_id: @metric.id, verdict: "disagree").count @versions = MetricVersion.where(metric_id: @metric.id).order(version_number: :desc).to_a end |
#starter_preview ⇒ Object
11 12 13 14 |
# File 'app/controllers/completion_kit/metrics_controller.rb', line 11 def starter_preview @starter = StarterMetrics.find(params[:key]) return redirect_to(metrics_path, alert: "Unknown starter metric.") unless @starter end |
#suggest_variants ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'app/controllers/completion_kit/metrics_controller.rb', line 115 def suggest_variants target = params[:back_to] == "edit" ? edit_metric_path(@metric) : metric_path(@metric) disagreement_count = Calibration.where(metric_id: @metric.id, verdict: "disagree").count if disagreement_count.zero? redirect_to target, alert: "Mark at least one case as Disagree before asking the model to suggest a change." return end MetricVersion.drafts.where(metric_id: @metric.id, source: "suggestion").destroy_all generator = MetricVariantGenerator.new(@metric, count: 1) variants = generator.call if variants.empty? redirect_to target, alert: "The model returned no usable variants. Try again with a different model." return end versions = generator.persist!(variants) new_version = versions.max_by(&:version_number) if params[:back_to] == "edit" redirect_to edit_metric_path(@metric), notice: "Drafted #{new_version.version_label} from your reviews. Review the proposed changes below, then Publish to use it." else redirect_to metric_path(@metric, show_change: new_version.id), notice: "Drafted #{new_version.version_label} from your reviews." end end |
#update ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'app/controllers/completion_kit/metrics_controller.rb', line 70 def update judge_keys = %i[instruction rubric_bands] = metric_params.except(*judge_keys) proposed_instruction = metric_params[:instruction] proposed_rubric = metric_params[:rubric_bands] unless @metric.update() return render(:edit, status: :unprocessable_entity) end current_instruction = @metric.instruction.to_s current_rubric = @metric.rubric_bands || [] normalized_proposed_rubric = normalize_rubric_bands_for_update(proposed_rubric) instruction_changed = !proposed_instruction.nil? && proposed_instruction.to_s != current_instruction rubric_changed = !normalized_proposed_rubric.nil? && normalized_proposed_rubric != current_rubric unless instruction_changed || rubric_changed return redirect_to(metric_path(@metric), notice: "Metric was successfully updated.") end new_instruction = instruction_changed ? proposed_instruction.to_s : current_instruction new_rubric = rubric_changed ? normalized_proposed_rubric : current_rubric if @metric.reviews.exists? MetricVersion.drafts.where(metric_id: @metric.id, source: "edit").destroy_all draft = MetricVersion.create!( metric: @metric, instruction: new_instruction, rubric_bands: new_rubric, state: "draft", source: "edit", current: false ) redirect_to edit_metric_path(@metric), notice: "Saved as draft #{draft.version_label}. Publish to make these changes the metric's live version." else @metric.update!(instruction: new_instruction, rubric_bands: new_rubric) current_pub = MetricVersion.published.where(metric_id: @metric.id, current: true).first current_pub&.update!(instruction: @metric.instruction, rubric_bands: @metric.rubric_bands) redirect_to metric_path(@metric), notice: "Metric was successfully updated." end end |