-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbeacons_controller.rb
More file actions
76 lines (61 loc) · 1.98 KB
/
beacons_controller.rb
File metadata and controls
76 lines (61 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
module Beacons
class BeaconsController < BaseController
before_action :set_beacon, only: %i[show edit update regenerate_key revoke_key]
before_action :prepare_associations, only: %i[new edit]
def index
@beacons = Beacon.includes(:language, :region, :providers, :topics).order(created_at: :desc)
end
def new
@beacon = Beacon.new
end
def create
success, @beacon, api_key = Beacons::Creator.new.call(beacon_params)
if success
flash[:notice] = "Beacon was successfully provisioned. API Key: #{api_key}"
redirect_to beacon_path(@beacon, api_key: api_key)
else
prepare_associations
render :new, status: :unprocessable_entity
end
end
def show; end
def edit; end
def update
if @beacon.update(beacon_params)
redirect_to @beacon, notice: "Beacon was successfully updated."
else
prepare_associations
render :edit, status: :unprocessable_entity
end
end
def regenerate_key
_, api_key = Beacons::KeyRegenerator.new.call(@beacon)
flash[:notice] = "API key has been successfully regenerated. API Key: #{api_key}"
redirect_to beacon_path(@beacon, api_key: api_key)
rescue => e
flash[:alert] = "API key could not be regenerated."
redirect_to @beacon
end
def revoke_key
api_key = @beacon.revoke!
flash[:notice] = "API key has been successfully revoked."
redirect_to @beacon
rescue => e
flash[:alert] = "API key could not be revoked."
redirect_to @beacon
end
private
def set_beacon
@beacon = Beacon.find(params[:id])
end
def prepare_associations
@languages = Language.order(:name)
@providers = Provider.order(:name)
@regions = Region.order(:name)
@topics = Topic.active.order(:title)
end
def beacon_params
params.require(:beacon).permit(:name, :language_id, :region_id, provider_ids: [], topic_ids: [])
end
end
end