Class: Glass::ApiController

Inherits:
ApplicationController show all
Defined in:
app/controllers/glass/api_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



34
35
36
37
38
39
40
41
42
# File 'app/controllers/glass/api_controller.rb', line 34

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

  if object.save
    render json: object.to_json
  else
    render json: object.errors, status: :unprocessable_entity
  end
end

#destroyObject



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/glass/api_controller.rb', line 58

def destroy
  begin
    object = @model.find(params[:id])
  rescue Exception => exception
    render json: "#{exception}", status: :unprocessable_entity
  end

  if object.delete
    render json: object.to_json, status: 200
  else
    render json: 'Unable to delete entity', status: :unprocessable_entity
  end
end

#indexObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/glass/api_controller.rb', line 9

def index
  begin
    render json: all_or_where(@model) # See Private Method Below
  rescue

    @unknown_params = [].tap do |keys|
      @new_hash.each do |key, val|
        unless @model.column_names.include? key.to_s
          keys << [key => val]
        end
      end
    end

    render json: { unknown_parameters: @unknown_params.flatten }, status: :unprocessable_entity
  end
end

#showObject



26
27
28
29
30
31
32
# File 'app/controllers/glass/api_controller.rb', line 26

def show
  begin
    render json: @model.find(params[:id]).to_json
  rescue Exception => exception
    render json: "#{exception}", status: :unprocessable_entity
  end
end

#updateObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/glass/api_controller.rb', line 44

def update
  begin
    object = @model.find(params[:id])
  rescue Exception => exception
    render json: "#{exception}", status: :unprocessable_entity
  end

  if object.update_attributes(@new_hash)
    render json: object.to_json
  else
    render json: object.errors, status: :unprocessable_entity
  end
end