Class: RailsConsoleAi::SkillsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/rails_console_ai/skills_controller.rb

Instance Method Summary collapse

Instance Method Details

#approveObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/controllers/rails_console_ai/skills_controller.rb', line 77

def approve
  redirect_to skills_path, alert: file_skill_message and return unless @skill.is_a?(RailsConsoleAi::Skill)

  approver = params[:approved_by].presence ||
             (request.respond_to?(:remote_user) && request.remote_user.presence) ||
             'web'

  if @skill.approved?
    redirect_to skill_path(@skill), notice: 'Skill is already approved.'
    return
  end

  begin
    @skill.approve!(approved_by: approver)
    redirect_to skill_path(@skill), notice: "Approved by #{approver}. The AI can now activate this skill."
  rescue ArgumentError, ActiveRecord::RecordInvalid => e
    redirect_to skill_path(@skill), alert: "Could not approve: #{e.message}"
  end
end

#createObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/rails_console_ai/skills_controller.rb', line 32

def create
  @skill = Skill.new
  attrs = skill_params
  begin
    @skill.update_with_version!(
      attrs,
      edited_by: edited_by_param,
      change_note: params[:change_note].presence
    )
    redirect_to skill_path(@skill), notice: 'Skill created.'
  rescue ActiveRecord::RecordInvalid => e
    flash.now[:alert] = e.message
    render :new
  end
end

#destroyObject



68
69
70
71
72
73
74
75
# File 'app/controllers/rails_console_ai/skills_controller.rb', line 68

def destroy
  if @skill.is_a?(RailsConsoleAi::Skill)
    @skill.destroy
    redirect_to skills_path, notice: 'Skill deleted. Past versions remain in history.'
  else
    redirect_to skills_path, alert: file_skill_message
  end
end

#diffObject

GET /skills/diff?skill_id=&from=&to=



98
99
100
101
102
103
104
105
# File 'app/controllers/rails_console_ai/skills_controller.rb', line 98

def diff
  @skill = Skill.find(params[:skill_id])
  @from = @skill.versions.find(params[:from])
  @to   = params[:to].present? ? @skill.versions.find(params[:to]) : nil
  # If `to` is omitted, diff against the current skill.
  @to_label   = @to ? "Version ##{@to.id}" : 'Current'
  @to_content = @to ? @to.content : @skill.content
end

#editObject



48
49
50
# File 'app/controllers/rails_console_ai/skills_controller.rb', line 48

def edit
  redirect_to skills_path, alert: file_skill_message and return unless @skill.is_a?(RailsConsoleAi::Skill)
end

#indexObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/rails_console_ai/skills_controller.rb', line 7

def index
  @skills = SkillLoader.new.load_all_skills
  @q = params[:q].to_s.strip
  unless @q.empty?
    needle = @q.downcase
    @skills = @skills.select { |s|
      [s['name'], s['description'], Array(s['tags']).join(' ')].compact.join(' ').downcase.include?(needle)
    }
  end

  @sort = params[:sort].to_s
  if @sort == 'used'
    # Most-used first; file/builtin records (no counter) sink to the bottom alphabetically.
    @skills = @skills.sort_by { |s| [-(s['use_count'].to_i), s['name'].to_s.downcase] }
  end
end

#newObject



28
29
30
# File 'app/controllers/rails_console_ai/skills_controller.rb', line 28

def new
  @skill = Skill.new(content: new_skill_template)
end

#showObject



24
25
26
# File 'app/controllers/rails_console_ai/skills_controller.rb', line 24

def show
  @versions = @skill.versions if @skill.is_a?(RailsConsoleAi::Skill)
end

#updateObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/rails_console_ai/skills_controller.rb', line 52

def update
  redirect_to skills_path, alert: file_skill_message and return unless @skill.is_a?(RailsConsoleAi::Skill)

  begin
    @skill.update_with_version!(
      skill_params,
      edited_by: edited_by_param,
      change_note: params[:change_note].presence
    )
    redirect_to skill_path(@skill), notice: 'Skill updated.'
  rescue ActiveRecord::RecordInvalid => e
    flash.now[:alert] = e.message
    render :edit
  end
end