Class: CompletionKit::PromptsController

Inherits:
ApplicationController show all
Includes:
TagFiltering
Defined in:
app/controllers/completion_kit/prompts_controller.rb

Constant Summary

Constants inherited from ApplicationController

ApplicationController::ONBOARDING_DISMISS_COOKIE

Instance Method Summary collapse

Instance Method Details

#createObject



24
25
26
27
28
29
30
31
32
# File 'app/controllers/completion_kit/prompts_controller.rb', line 24

def create
  @prompt = Prompt.new(prompt_params)

  if @prompt.save
    redirect_to prompt_path(@prompt), notice: "Prompt version was successfully created."
  else
    render :new, status: :unprocessable_entity
  end
end

#destroyObject



55
56
57
58
# File 'app/controllers/completion_kit/prompts_controller.rb', line 55

def destroy
  @prompt.destroy
  redirect_to prompts_path, notice: "Prompt version was successfully destroyed."
end

#editObject



21
22
# File 'app/controllers/completion_kit/prompts_controller.rb', line 21

def edit
end

#indexObject



6
7
8
# File 'app/controllers/completion_kit/prompts_controller.rb', line 6

def index
  @prompts = apply_tag_filter(Prompt.current_versions.includes(:runs, :tags).order(created_at: :desc))
end

#newObject



17
18
19
# File 'app/controllers/completion_kit/prompts_controller.rb', line 17

def new
  @prompt = Prompt.new
end

#publishObject



60
61
62
63
# File 'app/controllers/completion_kit/prompts_controller.rb', line 60

def publish
  @prompt.publish!
  redirect_to prompt_path(@prompt), notice: "#{@prompt.display_name} is now the published version."
end

#showObject



10
11
12
13
14
15
# File 'app/controllers/completion_kit/prompts_controller.rb', line 10

def show
  @runs = Run.where(prompt_id: @prompt.family_versions.select(:id))
             .includes(:prompt, :dataset, responses: :reviews)
             .order(created_at: :desc)
             .display_scoped
end

#updateObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/completion_kit/prompts_controller.rb', line 34

def update
  if @prompt.runs.exists?
    new_prompt = @prompt.build_next_version(prompt_params.to_h)
    if new_prompt.valid?
      CompletionKit::ApplicationRecord.transaction do
        new_prompt.save!
        new_prompt.publish!
      end
      redirect_to prompt_path(new_prompt), notice: "Saved as #{new_prompt.version_label}."
    else
      @prompt.assign_attributes(prompt_params.to_h)
      @prompt.errors.merge!(new_prompt.errors)
      render :edit, status: :unprocessable_entity
    end
  elsif @prompt.update(prompt_params)
    redirect_to prompt_path(@prompt), notice: "Prompt saved."
  else
    render :edit, status: :unprocessable_entity
  end
end