Class: RailsConsoleAi::MemoriesController

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

Instance Method Summary collapse

Instance Method Details

#createObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/rails_console_ai/memories_controller.rb', line 56

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

#destroyObject



92
93
94
95
96
97
98
99
# File 'app/controllers/rails_console_ai/memories_controller.rb', line 92

def destroy
  if @memory.is_a?(RailsConsoleAi::Memory)
    @memory.destroy
    redirect_to memories_path, notice: 'Memory deleted. Past versions remain in history.'
  else
    redirect_to memories_path, alert: file_memory_message
  end
end

#diffObject



101
102
103
104
105
106
107
108
# File 'app/controllers/rails_console_ai/memories_controller.rb', line 101

def diff
  @memory = Memory.find(params[:memory_id])
  @from = @memory.versions.find(params[:from])
  @to   = params[:to].present? ? @memory.versions.find(params[:to]) : nil
  @to_label = @to ? "Version ##{@to.id}" : 'Current'
  @to_description = @to ? @to.description : @memory.description
  @to_tags = @to ? Array(@to.tags) : Array(@memory.tags)
end

#editObject



72
73
74
# File 'app/controllers/rails_console_ai/memories_controller.rb', line 72

def edit
  redirect_to memories_path, alert: file_memory_message and return unless @memory.is_a?(RailsConsoleAi::Memory)
end

#importObject

POST /memories/import — parse a pasted .md blob and re-render ‘new` with fields prefilled.



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

def import
  content = params[:content].to_s
  if content.strip.empty?
    redirect_to new_memory_path, alert: 'Nothing to parse — paste the .md content into the box first.'
    return
  end

  parsed = Tools::MemoryTools.parse(content)
  if parsed.nil? || parsed['name'].to_s.strip.empty?
    redirect_to new_memory_path,
                alert: 'Could not parse. Expected YAML frontmatter (between `---` lines) with at least a `name` field, followed by the memory body.'
    return
  end

  @memory = Memory.new(
    name: parsed['name'],
    description: parsed['description']
  )
  @memory.tags = Array(parsed['tags'])

  flash.now[:notice] = "Parsed \"#{parsed['name']}\" from pasted content. Review the fields below and click Create memory to save to the DB."
  render :new
end

#indexObject



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

def index
  @memories = Tools::MemoryTools.new.load_all_memories
  @q = params[:q].to_s.strip
  unless @q.empty?
    needle = @q.downcase
    @memories = @memories.select { |m|
      [m['name'], m['description'], Array(m['tags']).join(' ')].compact.join(' ').downcase.include?(needle)
    }
  end

  @sort = params[:sort].to_s
  if @sort == 'used'
    @memories = @memories.sort_by { |m| [-(m['use_count'].to_i), m['name'].to_s.downcase] }
  end
end

#newObject



27
28
29
# File 'app/controllers/rails_console_ai/memories_controller.rb', line 27

def new
  @memory = Memory.new
end

#showObject



23
24
25
# File 'app/controllers/rails_console_ai/memories_controller.rb', line 23

def show
  @versions = @memory.versions if @memory.is_a?(RailsConsoleAi::Memory)
end

#updateObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/rails_console_ai/memories_controller.rb', line 76

def update
  redirect_to memories_path, alert: file_memory_message and return unless @memory.is_a?(RailsConsoleAi::Memory)

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