Class: Trackguard::Admin::WhitelistedIpsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/trackguard/admin/whitelisted_ips_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/trackguard/admin/whitelisted_ips_controller.rb', line 14

def create
  record = WhitelistedIp.find_or_initialize_by(ip: @visitor.ip)
  record.visitor    = @visitor
  record.expires_at = params[:expires_at].presence || 7.days.from_now
  record.save!
  respond_to do |format|
    format.html { redirect_back_or_to dashboard_path }
    format.json { render json: { status: "ok", ip: @visitor.ip, expires_at: record.expires_at } }
  end
rescue ActiveRecord::RecordInvalid => e
  respond_to do |format|
    format.html { redirect_back_or_to dashboard_path, alert: e.message }
    format.json { render json: { status: "error", message: e.message }, status: :unprocessable_entity }
  end
end

#destroyObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/trackguard/admin/whitelisted_ips_controller.rb', line 30

def destroy
  record = @visitor.whitelisted_ip

  if record
    record.destroy!
    respond_to do |format|
      format.html { redirect_back_or_to dashboard_path }
      format.json { render json: { status: "ok", ip: @visitor.ip } }
    end
  else
    respond_to do |format|
      format.html { redirect_back_or_to dashboard_path, alert: "No whitelist entry found." }
      format.json { render json: { error: "Not whitelisted" }, status: :not_found }
    end
  end
end