|
| 1 | +class BeaconsController < ApplicationController |
| 2 | + include Authentication |
| 3 | + |
| 4 | + before_action :set_beacon, only: %i[show edit update regenerate_key revoke_key] |
| 5 | + before_action :prepare_associations, only: %i[new edit] |
| 6 | + |
| 7 | + def index |
| 8 | + @beacons = Beacon.order(created_at: :desc) |
| 9 | + end |
| 10 | + |
| 11 | + def new |
| 12 | + @beacon = Beacon.new |
| 13 | + end |
| 14 | + |
| 15 | + def create |
| 16 | + success, @beacon, api_key = Beacons::Creator.new.call(beacon_params) |
| 17 | + |
| 18 | + if success |
| 19 | + flash[:notice] = "Beacon was successfully provisioned. API Key: #{api_key}" |
| 20 | + redirect_to beacon_path(@beacon, api_key: api_key) |
| 21 | + else |
| 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 = Beacons::KeyRegenerator.new.call(@beacon) |
| 42 | + flash[:notice] = "API key has been successfully regenerated. API Key: #{api_key}" |
| 43 | + redirect_to beacon_path(@beacon, api_key: api_key) |
| 44 | + |
| 45 | + rescue => StandardError |
| 46 | + flash[:alert] = "API key could not be regenerated." |
| 47 | + redirect_to @beacon |
| 48 | + end |
| 49 | + |
| 50 | + def filter_options |
| 51 | + topics = if params[:language_id].present? |
| 52 | + Topic.active.where(language_id: params[:language_id]).order(:title) |
| 53 | + else |
| 54 | + Topic.active.order(:title) |
| 55 | + end |
| 56 | + |
| 57 | + providers = if params[:region_id].present? |
| 58 | + Provider.joins(:branches).where(branches: { region_id: params[:region_id] }).distinct.order(:name) |
| 59 | + else |
| 60 | + Provider.order(:name) |
| 61 | + end |
| 62 | + |
| 63 | + render json: { |
| 64 | + topics: topics.select(:id, :title), |
| 65 | + providers: providers.select(:id, :name), |
| 66 | + } |
| 67 | + end |
| 68 | + |
| 69 | + def revoke_key |
| 70 | + api_key = @beacon.revoke! |
| 71 | + flash[:notice] = "API key has been successfully revoked." |
| 72 | + redirect_to @beacon |
| 73 | + |
| 74 | + rescue => StandardError |
| 75 | + flash[:alert] = "API key could not be revoked." |
| 76 | + redirect_to @beacon |
| 77 | + end |
| 78 | + |
| 79 | + def non_contributor_redirect_path |
| 80 | + root_path |
| 81 | + end |
| 82 | + |
| 83 | + private |
| 84 | + |
| 85 | + def set_beacon |
| 86 | + @beacon = Beacon.find(params[:id]) |
| 87 | + end |
| 88 | + |
| 89 | + def prepare_associations |
| 90 | + @languages = Language.order(:name) |
| 91 | + @providers = Provider.order(:name) |
| 92 | + @regions = Region.order(:name) |
| 93 | + @topics = Topic.active.order(:title) |
| 94 | + end |
| 95 | + |
| 96 | + def beacon_params |
| 97 | + params.require(:beacon).permit(:name, :language_id, :region_id, provider_ids: [], topic_ids: []) |
| 98 | + end |
| 99 | +end |
0 commit comments