2021-06-01 07:09:07 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2021-04-01 10:59:27 +00:00
|
|
|
class AppsController < ApplicationController
|
2021-06-22 14:33:13 +00:00
|
|
|
skip_before_action :authenticate_request, only: %i[show slug]
|
2021-05-07 08:25:09 +00:00
|
|
|
|
2021-04-29 06:41:23 +00:00
|
|
|
def index
|
|
|
|
|
authorize App
|
2021-05-19 09:12:10 +00:00
|
|
|
|
|
|
|
|
folder_id = params[:folder]
|
|
|
|
|
|
|
|
|
|
if folder_id.blank?
|
2021-05-19 09:16:42 +00:00
|
|
|
@scope = App.where(organization: @current_user.organization)
|
2021-05-19 09:12:10 +00:00
|
|
|
else
|
|
|
|
|
@folder = Folder.find folder_id
|
2021-05-19 09:16:42 +00:00
|
|
|
@scope = @folder.apps
|
2021-05-19 09:12:10 +00:00
|
|
|
end
|
|
|
|
|
|
2021-07-01 07:24:35 +00:00
|
|
|
@apps = @scope.order("created_at desc")
|
2021-06-22 14:33:13 +00:00
|
|
|
.page(params[:page])
|
|
|
|
|
.per(10)
|
|
|
|
|
.includes(:user)
|
2021-05-19 09:12:10 +00:00
|
|
|
|
2021-06-01 07:09:07 +00:00
|
|
|
@meta = {
|
2021-05-19 06:36:18 +00:00
|
|
|
total_pages: @apps.total_pages,
|
2021-05-19 09:36:01 +00:00
|
|
|
folder_count: @scope.count,
|
2021-05-20 09:57:45 +00:00
|
|
|
total_count: App.where(organization: @current_user.organization).count,
|
2021-06-01 07:09:07 +00:00
|
|
|
current_page: @apps.current_page
|
|
|
|
|
}
|
2021-04-29 06:41:23 +00:00
|
|
|
end
|
2021-04-05 07:49:48 +00:00
|
|
|
|
2021-04-29 06:41:23 +00:00
|
|
|
def create
|
|
|
|
|
authorize App
|
2021-06-22 14:33:13 +00:00
|
|
|
@app = App.create!({
|
2021-07-01 07:24:35 +00:00
|
|
|
name: "Untitled app",
|
2021-06-22 14:33:13 +00:00
|
|
|
organization: @current_user.organization,
|
2021-07-01 07:24:35 +00:00
|
|
|
current_version: AppVersion.new(name: "v0"),
|
2021-06-22 14:33:13 +00:00
|
|
|
user: @current_user
|
|
|
|
|
})
|
2021-07-01 07:24:35 +00:00
|
|
|
AppUser.create(app: @app, user: @current_user, role: "admin")
|
2021-04-29 06:41:23 +00:00
|
|
|
end
|
2021-04-02 11:09:35 +00:00
|
|
|
|
2021-04-29 06:41:23 +00:00
|
|
|
def show
|
2021-06-22 14:33:13 +00:00
|
|
|
@app = App.find(params[:id])
|
2021-05-07 08:25:09 +00:00
|
|
|
|
2021-06-01 07:09:07 +00:00
|
|
|
# Logic to bypass auth for public apps
|
|
|
|
|
unless @app.is_public
|
|
|
|
|
authenticate_request
|
|
|
|
|
authorize @app
|
|
|
|
|
end
|
2021-04-29 06:41:23 +00:00
|
|
|
end
|
2021-04-02 11:09:35 +00:00
|
|
|
|
2021-06-25 17:43:44 +00:00
|
|
|
def destroy
|
|
|
|
|
app = App.find params[:id]
|
|
|
|
|
app.update(current_version: nil)
|
|
|
|
|
app.destroy
|
|
|
|
|
end
|
|
|
|
|
|
2021-06-22 14:33:13 +00:00
|
|
|
def slugs
|
|
|
|
|
@app = App.find_by(slug: params[:slug])
|
|
|
|
|
|
|
|
|
|
unless @app.is_public
|
|
|
|
|
authenticate_request
|
|
|
|
|
authorize @app, :show?
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
render :show
|
|
|
|
|
end
|
|
|
|
|
|
2021-04-29 06:41:23 +00:00
|
|
|
def update
|
|
|
|
|
@app = App.find params[:id]
|
2021-05-20 07:37:17 +00:00
|
|
|
authorize @app
|
2021-06-22 14:33:13 +00:00
|
|
|
|
|
|
|
|
@app.assign_attributes(params[:app].permit(:name, :current_version_id, :is_public, :slug))
|
|
|
|
|
|
|
|
|
|
if @app.valid?
|
|
|
|
|
@app.save # renders default status 204
|
|
|
|
|
else
|
|
|
|
|
render json: { message: @app.errors.full_messages }, status: 422
|
|
|
|
|
end
|
2021-04-29 06:41:23 +00:00
|
|
|
end
|
2021-04-12 04:12:00 +00:00
|
|
|
|
2021-04-29 06:41:23 +00:00
|
|
|
def users
|
|
|
|
|
@app = App.find params[:app_id]
|
|
|
|
|
@app_users = AppUser.where(app: @app).includes(:user)
|
|
|
|
|
end
|
2021-04-01 10:59:27 +00:00
|
|
|
end
|