Class: Tramway::Api::V1::RecordsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/tramway/api/v1/records_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#render_error_with_text, #render_errors_for, #snake_case

Methods inherited from ApplicationController

#authenticate, #not_found, #unauthorized

Instance Method Details

#applicationObject



61
62
63
64
65
# File 'app/controllers/tramway/api/v1/records_controller.rb', line 61

def application
  if ::Tramway.application
    @application = Tramway.application&.model_class&.first || Tramway.application
  end
end

#createObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/tramway/api/v1/records_controller.rb', line 21

def create
  record_form = form_class.new model_class.new
  if record_form.submit snake_case params[:data][:attributes]
    record_form.model.reload
    render json: record_form.model,
           serializer: serializer_class,
           include: '*',
           status: :created
  else
    render_errors_for record_form
  end
end

#destroyObject



53
54
55
56
57
58
59
# File 'app/controllers/tramway/api/v1/records_controller.rb', line 53

def destroy
  record.destroy
  render json: record,
         serializer: serializer_class,
         include: '*',
         status: :no_content
end

#indexObject



10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/tramway/api/v1/records_controller.rb', line 10

def index
  @collection = available_action_for_collection

  raise 'Collection has empty uuid. It should not be empty, because all records with empty uuid will not be rendered' if @collection.map(&:uuid).map(&:empty?).include? true

  render json: @collection,
         each_serializer: serializer_class,
         include: '*',
         status: :ok
end

#showObject



46
47
48
49
50
51
# File 'app/controllers/tramway/api/v1/records_controller.rb', line 46

def show
  render json: record,
         serializer: serializer_class,
         include: '*',
         status: :ok
end

#updateObject



34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/tramway/api/v1/records_controller.rb', line 34

def update
  record_form = form_class.new record
  if record_form.submit snake_case params[:data][:attributes]
    render json: record_form.model,
           serializer: serializer_class,
           include: '*',
           status: :ok
  else
    render_errors_for record_form
  end
end