Class: RestRails::DataController

Inherits:
ApplicationController
  • Object
show all
Includes:
ApplicationHelper
Defined in:
app/controllers/rest_rails/data_controller.rb

Instance Method Summary collapse

Methods included from ApplicationHelper

#attachment_hash, #attachments_for, #blob_link, #columns_for, #model_for, #nested_attributes_for, #prepare_attachment, #prepare_column, #serial_attachments, #standardize_json, #verify_permitted!

Instance Method Details

#attachObject



63
64
65
66
67
68
69
70
# File 'app/controllers/rest_rails/data_controller.rb', line 63

def attach
  # post   '/:table_name/:id/attach/:attachment_name' => 'data#attach'
  raise RestRails::Error.new "No Attached file!" unless params[:attachment].is_a?(ActionDispatch::Http::UploadedFile)
  raise RestRails::Error.new "Attachment '#{params[:attachment_name]}' does not exist for #{params[:table_name]} table!" unless attachments_for(@empty_obj).include?(params[:attachment_name].to_sym)

  @object.public_send(params[:attachment_name].to_sym).attach(params[:attachment])
  render json: {code: 200, msg: "success"}
end

#createObject



28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/rest_rails/data_controller.rb', line 28

def create
  @object = @model.new(model_params)

  attach_files
  if @object.save
    render json: {code: 200, msg: "success", object: standardize_json(@object)}
  else
    render json: {code: 300, msg: "FAILED!"}
  end
end

#destroyObject



48
49
50
51
52
53
54
# File 'app/controllers/rest_rails/data_controller.rb', line 48

def destroy
  if @object.destroy
    render json: {code: 200, msg: "success"}
  else
    render json: {code: 300, msg: "FAILED!"}
  end
end

#fetch_columnObject



56
57
58
59
60
61
# File 'app/controllers/rest_rails/data_controller.rb', line 56

def fetch_column
  raise RestRails::Error.new "Column '#{params[:column]}' does not exist for #{params[:table_name]} table!" unless columns_for(@object).include?(params[:column])

  col_value = @object.public_send(params[:column])
  render json: {code: 200, msg: "success", value: prepare_column(col_value)}
end

#indexObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/rest_rails/data_controller.rb', line 10

def index
  p_hash = index_params.to_h
  ppage = (params[:per_page] || 100).to_i
  page =  (params[:page] || 1).to_i
  off = (page-1) * ppage

  base_query = p_hash.blank? ? @model.all : @model.where(p_hash)

  @objects = base_query.order(:id).limit(ppage).offset(off)
  @objects = @objects.map{|x| standardize_json(x) }

  render json: {code: 200, objects: @objects, count: @objects.count, total: @model.count}
end

#showObject



24
25
26
# File 'app/controllers/rest_rails/data_controller.rb', line 24

def show
  render json: {code: 200, object: standardize_json(@object)}
end

#unattachObject



72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/rest_rails/data_controller.rb', line 72

def unattach
  # delete '/:table_name/:id/unattach/:attachment_id' => 'data#unattach'
  att = ActiveStorage::Attachment.find(params[:attachment_id])

  raise RestRails::Error.new "Unauthorized! Attachment does not belong to object!" unless (@object.id == att.record_id) && (@object.is_a? att.record_type.constantize)

  att.purge

  render json: {code: 200, msg: "success"}
end

#updateObject



39
40
41
42
43
44
45
46
# File 'app/controllers/rest_rails/data_controller.rb', line 39

def update
  attach_files
  if @object.update(model_params)
    render json: {code: 200, msg: "success", object: standardize_json(@object)}
  else
    render json: {code: 300, msg: "FAILED!"}
  end
end