Class: Hai::RestController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/hai/rest_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



27
28
29
30
31
32
33
# File 'app/controllers/hai/rest_controller.rb', line 27

def create
  render json:
           Hai::Create
             .new(model_class, context)
             .execute(**params[params[:model].singularize].permit!)
             .to_json
end

#destroyObject



46
47
48
49
50
51
52
# File 'app/controllers/hai/rest_controller.rb', line 46

def destroy
  render json:
           Hai::Delete
             .new(model_class, context)
             .execute(id: params[:id])
             .to_json
end

#indexObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/controllers/hai/rest_controller.rb', line 4

def index
  render json:
           Hai::Read
             .new(model_class, context)
             .list(
               # TODO: this is a security risk, thanks co-pilot
               # potenntial use attributes types plus AREL_TYPE_CAST
               filter: params[:filter]&.to_unsafe_h,
               limit: params[:limit],
               offset: params[:offset],
               sort: params[:sort]&.to_unsafe_h
             )
             .to_json
end

#showObject



19
20
21
22
23
24
25
# File 'app/controllers/hai/rest_controller.rb', line 19

def show
  render json:
           Hai::Read
             .new(model_class, context)
             .read(id: { eq: params[:id] })
             .to_json
end

#updateObject



35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/hai/rest_controller.rb', line 35

def update
  render json:
           Hai::Update
             .new(model_class, context)
             .execute(
               id: params[:id],
               attributes: params[params[:model]].permit!
             )
             .to_json
end