Skip to content

Commit cd9ea07

Browse files
committed
More review changes
1 parent 8dd9f8e commit cd9ea07

10 files changed

Lines changed: 97 additions & 95 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Beacons
2+
class BaseController < ApplicationController
3+
before_action :redirect_contributors
4+
5+
private
6+
7+
def redirect_contributors
8+
redirect_to root_path, alert: "You don't have permission to access this page." unless Current.user&.is_admin?
9+
end
10+
end
11+
end
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
module Beacons
2+
class BeaconsController < BaseController
3+
before_action :set_beacon, only: %i[show edit update regenerate_key revoke_key]
4+
before_action :prepare_associations, only: %i[new edit]
5+
6+
def index
7+
@beacons = Beacon.includes(:language, :region, :providers, :topics).order(created_at: :desc)
8+
end
9+
10+
def new
11+
@beacon = Beacon.new
12+
end
13+
14+
def create
15+
success, @beacon, api_key = Beacons::Creator.new.call(beacon_params)
16+
17+
case Beacons::Creator.new.call(beacon_params)
18+
when [ true, @beacon, api_key ]
19+
flash[:notice] = "Beacon was successfully provisioned. API Key: #{api_key}"
20+
redirect_to @beacon
21+
when [ false, _, _ ]
22+
prepare_associations
23+
render :new, status: :unprocessable_entity
24+
end
25+
end
26+
27+
def show; end
28+
29+
def edit; end
30+
31+
def update
32+
if @beacon.update(beacon_params)
33+
redirect_to @beacon, notice: "Beacon was successfully updated."
34+
else
35+
prepare_associations
36+
render :edit, status: :unprocessable_entity
37+
end
38+
end
39+
40+
def regenerate_key
41+
api_key = @beacon.regenerate
42+
flash[:notice] = "API key has been successfully regenerated. API Key: #{api_key}"
43+
redirect_to @beacon
44+
45+
rescue => e
46+
flash[:alert] = "API key could not be regenerated."
47+
redirect_to @beacon
48+
end
49+
50+
def revoke_key
51+
api_key = @beacon.revoke!
52+
flash[:notice] = "API key has been successfully revoked."
53+
redirect_to @beacon
54+
55+
rescue => e
56+
flash[:alert] = "API key could not be revoked."
57+
redirect_to @beacon
58+
end
59+
60+
private
61+
62+
def set_beacon
63+
@beacon = Beacon.find(params[:id])
64+
end
65+
66+
def prepare_associations
67+
@languages = Language.order(:name)
68+
@providers = Provider.order(:name)
69+
@regions = Region.order(:name)
70+
@topics = Topic.active.order(:title)
71+
end
72+
73+
def beacon_params
74+
params.require(:beacon).permit(:name, :language_id, :region_id, provider_ids: [], topic_ids: [])
75+
end
76+
end
77+
end

app/controllers/beacons_controller.rb

Lines changed: 0 additions & 81 deletions
This file was deleted.

config/database.yml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,6 @@
1515
common: &common
1616
adapter: postgresql
1717
encoding: unicode
18-
host: <%= ENV.fetch('DATABASE_HOST') { 'localhost' } %>
19-
username: <%= ENV.fetch('DATABASE_USER') { 'skillrx' } %>
20-
password: <%= ENV.fetch('DATABASE_PASSWORD') { '' } %>
21-
port: <%= ENV.fetch('DATABASE_PORT') { 5432 } %>
22-
# For details on connection pooling, see Rails configuration guide
23-
# https://guides.rubyonrails.org/configuring.html#database-pooling
24-
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
25-
gssencmode: disable # fixes SEGFAULT on MacOS https://github.com/ged/ruby-pg/issues/311
2618

2719
queue_config: &queue
2820
<<: *common

config/routes.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@
1717
resources :tags, only: %i[index], controller: "topics/tags"
1818
end
1919
resources :import_reports, only: %i[index show]
20-
resources :beacons, except: :destroy do
21-
member do
22-
post :regenerate_key
23-
post :revoke_key
24-
end
25-
end
2620
resource :settings, only: [] do
2721
put :provider, on: :collection
2822
end
2923

24+
scope module: :beacons do
25+
resources :beacons, except: :destroy do
26+
member do
27+
post :regenerate_key
28+
post :revoke_key
29+
end
30+
end
31+
end
32+
3033
# Render dynamic PWA files from app/views/pwa/* (remember to link manifest in application.html.erb)
3134
# get "manifest" => "rails/pwa#manifest", as: :pwa_manifest
3235
# get "service-worker" => "rails/pwa#service_worker", as: :pwa_service_worker

0 commit comments

Comments
 (0)