Class: ApiEngine::ApplicationController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/api_engine/application_controller.rb

Instance Method Summary collapse

Instance Method Details

#bulk_destroyObject



58
59
60
61
62
# File 'app/controllers/api_engine/application_controller.rb', line 58

def bulk_destroy
  @models = model_class.find(params[plural_model].map{ |m| m['id']})
  @models.map(&:destroy)
  head :no_content
end

#bulk_updateObject



41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/api_engine/application_controller.rb', line 41

def bulk_update
  @models = []
  params[plural_model].each do |attributes|
    id = attributes.delete('id')
    object = model_class.find(id)
    object.update_attributes!(attributes)
    @models << object
  end
  render json: @models, root: plural_model
end

#createObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/api_engine/application_controller.rb', line 15

def create
  if bulk_operation?
    @models = []
    params[plural_model].each do |attributes|
      @models << model_class.create!(attributes)
    end
    render json: @models, status: :created
  else
    @model = model_class.new(params[singular_model])
    if @model.save
      render json: @model, status: :created
    else
      render json: @model.errors, status: :unprocessable_entity
    end
  end
end

#destroyObject



52
53
54
55
56
# File 'app/controllers/api_engine/application_controller.rb', line 52

def destroy
  @model = model_class.find(params[:id])
  @model.destroy
  head :no_content
end

#indexObject



5
6
7
8
# File 'app/controllers/api_engine/application_controller.rb', line 5

def index
  @models = model_class.all
  render json: @models, root: plural_model
end

#showObject



10
11
12
13
# File 'app/controllers/api_engine/application_controller.rb', line 10

def show
  @model = model_class.find(params[:id])
  render json: @model, root: singular_model
end

#updateObject



32
33
34
35
36
37
38
39
# File 'app/controllers/api_engine/application_controller.rb', line 32

def update
  @model = model_class.find(params[:id])
  if @model.update_attributes(params[singular_model])
    render json: @model
  else
    render json: @model.errors, status: :unprocessable_entity
  end
end