Class: RubyCms::Admin::CommandsController

Inherits:
BaseController show all
Defined in:
app/controllers/ruby_cms/admin/commands_controller.rb

Constant Summary collapse

RUN_CACHE_PREFIX =

HTML: POST/redirect/GET — URL blijft /settings/commands (geen /run in de adresbalk).

"ruby_cms/commands/run/v1/"
FLASH_RUN =
:ruby_cms_commands_run

Instance Method Summary collapse

Methods inherited from BaseController

cms_page, #current_user_cms

Instance Method Details

#indexObject



12
13
14
15
# File 'app/controllers/ruby_cms/admin/commands_controller.rb', line 12

def index
  @commands = visible_commands
  consume_run_payload_from_flash
end

#runObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/ruby_cms/admin/commands_controller.rb', line 17

def run
  key = params.permit(:key)[:key].presence || params.dig(:command, :key)
  cmd = RubyCms.find_command(key)
  unless cmd
    return respond_to do |format|
      format.html do
        redirect_to ruby_cms_admin_settings_commands_path,
                    alert: t("ruby_cms.admin.commands.unknown", default: "Unknown command.")
      end
      format.json { render json: { error: "Unknown command" }, status: :not_found }
    end
  end

  require_permission!(cmd[:permission])

  output = utf8_text(RubyCms::CommandRunner.run_rake(cmd[:rake_task]))
  log_tail = utf8_text(RubyCms::CommandRunner.tail_log)

  respond_to do |format|
    format.html do
      token = persist_run_payload_for_redirect(output:, log_tail:)
      redirect_to ruby_cms_admin_settings_commands_path,
                  status: :see_other,
                  flash: { FLASH_RUN => token }
    end
    format.json do
      render json: {
        command_output: output,
        app_log_tail: log_tail
      }
    end
  end
rescue StandardError => e
  Rails.logger.error("[RubyCMS] Commands#run: #{e.class}: #{e.message}")
  respond_to do |format|
    format.html do
      token = persist_run_payload_for_redirect(error: utf8_text(e.message))
      redirect_to ruby_cms_admin_settings_commands_path,
                  status: :see_other,
                  flash: { FLASH_RUN => token }
    end
    format.json { render json: { error: e.message }, status: :unprocessable_content }
  end
end