Class: Admin::RedirectsController

Inherits:
ApplicationController
  • Object
show all
Includes:
AdminBulkActions, AdminPagination, AdminTurboTable, AuditLoggable
Defined in:
lib/generators/ruby_cms/templates/controllers/admin/redirects_controller.rb

Constant Summary collapse

SORTABLE_COLUMNS =
{
  "from"     => :source_path,
  "to"       => :target_path,
  "code"     => :status_code,
  "hits"     => :hits,
  "lastHit"  => :last_hit_at,
  "enabled"  => :enabled
}.freeze

Constants included from AdminPagination

AdminPagination::DEFAULT_MAX_PER_PAGE, AdminPagination::DEFAULT_MIN_PER_PAGE

Instance Method Summary collapse

Methods included from AdminTurboTable

#turbo_frame_id, #turbo_frame_request?, #turbo_redirect_to, #turbo_render_form, #turbo_render_index, #turbo_stream_replace_table, #turbo_stream_request?, #turbo_stream_update_table

Methods included from AdminPagination

#paginate_collection, #set_pagination_vars

Instance Method Details

#bulk_deleteObject



93
94
95
96
97
# File 'lib/generators/ruby_cms/templates/controllers/admin/redirects_controller.rb', line 93

def bulk_delete
  count = bulk_destroy(Redirect, audit: :redirect_deleted, target_label: "Redirect")
  Redirect.bust_cache
  turbo_redirect_to admin_redirects_path, notice: "#{count} redirects verwijderd."
end

#bulk_disableObject



105
106
107
108
109
# File 'lib/generators/ruby_cms/templates/controllers/admin/redirects_controller.rb', line 105

def bulk_disable
  count = bulk_update(Redirect, { enabled: false }, audit: :redirect_disabled, target_label: "Redirect")
  Redirect.bust_cache
  turbo_redirect_to admin_redirects_path, notice: "#{count} redirects uitgeschakeld."
end

#bulk_enableObject



99
100
101
102
103
# File 'lib/generators/ruby_cms/templates/controllers/admin/redirects_controller.rb', line 99

def bulk_enable
  count = bulk_update(Redirect, { enabled: true }, audit: :redirect_enabled, target_label: "Redirect")
  Redirect.bust_cache
  turbo_redirect_to admin_redirects_path, notice: "#{count} redirects ingeschakeld."
end

#createObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/generators/ruby_cms/templates/controllers/admin/redirects_controller.rb', line 62

def create
  @redirect = Redirect.new(redirect_params)
  if @redirect.save
    Redirect.bust_cache
    audit!(:redirect_created, target: @redirect, summary: "Created redirect #{@redirect.source_path}#{@redirect.target_path} (#{@redirect.status_code})")
    turbo_redirect_to admin_redirects_path, notice: "Redirect aangemaakt."
  else
    turbo_render_form :new
  end
end

#destroyObject



85
86
87
88
89
90
91
# File 'lib/generators/ruby_cms/templates/controllers/admin/redirects_controller.rb', line 85

def destroy
  src = @redirect.source_path
  @redirect.destroy
  Redirect.bust_cache
  audit!(:redirect_deleted, target: "Redirect:#{src}", summary: "Deleted redirect #{src}", meta: { id: @redirect.id })
  turbo_redirect_to admin_redirects_path, notice: "Redirect verwijderd."
end

#editObject



73
# File 'lib/generators/ruby_cms/templates/controllers/admin/redirects_controller.rb', line 73

def edit; end

#import_csvObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/generators/ruby_cms/templates/controllers/admin/redirects_controller.rb', line 111

def import_csv
  file = params[:file]
  return redirect_to(admin_redirects_path, alert: "Geen bestand geüpload.") unless file.respond_to?(:read)

  rows = CSV.parse(file.read, headers: true)
  expected = %w[from to code note]
  unless rows.headers && (rows.headers - expected).empty? && (expected - rows.headers - %w[note]).empty?
    return redirect_to(admin_redirects_path, alert: "Header moet zijn: from,to,code,note")
  end

  inserted = 0
  rows.each do |row|
    attrs = {
      source_path: row["from"],
      target_path: row["to"],
      status_code: (row["code"].presence || 301).to_i,
      note: row["note"]
    }
    if Redirect.create(attrs).persisted?
      inserted += 1
    end
  end

  Redirect.bust_cache
  audit!(:redirect_imported, target: "Redirect (CSV)", summary: "Imported #{inserted} redirect(s) from CSV", meta: { inserted: inserted, total_rows: rows.size })
  redirect_to admin_redirects_path, notice: "#{inserted} redirects geïmporteerd."
rescue CSV::MalformedCSVError => e
  redirect_to admin_redirects_path, alert: "Ongeldig CSV-bestand: #{e.message}"
end

#indexObject



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
# File 'lib/generators/ruby_cms/templates/controllers/admin/redirects_controller.rb', line 26

def index
  scope = Redirect.all
  scope = apply_filter(scope)
  scope = apply_search(scope, params[:search])
  scope = apply_sort(scope)

  # Single aggregate query — was 5 separate DB hits.
  agg = Redirect.connection.select_one(<<~SQL)
    SELECT
      COUNT(*) AS total,
      SUM(CASE WHEN enabled THEN 1 ELSE 0 END) AS active,
      SUM(CASE WHEN NOT enabled THEN 1 ELSE 0 END) AS disabled,
      SUM(CASE WHEN target_path LIKE 'http%' THEN 1 ELSE 0 END) AS external,
      COALESCE(SUM(hits), 0) AS total_hits
    FROM redirects
  SQL
  @counts = {
    all:      agg["total"].to_i,
    active:   agg["active"].to_i,
    disabled: agg["disabled"].to_i,
    external: agg["external"].to_i
  }
  @total_hits = agg["total_hits"].to_i
  @active_filter = filter_value
  @redirects = set_pagination_vars(scope)
  turbo_render_index
end

#newObject



58
59
60
# File 'lib/generators/ruby_cms/templates/controllers/admin/redirects_controller.rb', line 58

def new
  @redirect = Redirect.new(status_code: 301, enabled: true)
end

#showObject



54
55
56
# File 'lib/generators/ruby_cms/templates/controllers/admin/redirects_controller.rb', line 54

def show
  redirect_to edit_admin_redirect_path(@redirect)
end

#updateObject



75
76
77
78
79
80
81
82
83
# File 'lib/generators/ruby_cms/templates/controllers/admin/redirects_controller.rb', line 75

def update
  if @redirect.update(redirect_params)
    Redirect.bust_cache
    audit!(:redirect_updated, target: @redirect, summary: "Updated redirect #{@redirect.source_path}")
    turbo_redirect_to admin_redirects_path, notice: "Redirect bijgewerkt."
  else
    turbo_render_form :edit
  end
end