ToolJet/app/controllers/apps_controller.rb

87 lines
2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2021-04-01 10:59:27 +00:00
class AppsController < ApplicationController
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
@apps = @scope.order("created_at desc")
.page(params[:page])
.per(10)
.includes(:user)
2021-05-19 09:12:10 +00:00
@meta = {
2021-05-19 06:36:18 +00:00
total_pages: @apps.total_pages,
folder_count: @scope.count,
2021-05-20 09:57:45 +00:00
total_count: App.where(organization: @current_user.organization).count,
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
@app = App.create!({
name: "Untitled app",
organization: @current_user.organization,
current_version: AppVersion.new(name: "v0"),
user: @current_user
})
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
@app = App.find(params[:id])
2021-05-07 08:25:09 +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
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
@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