Class: GenericController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
lib/generic_controller.rb

Constant Summary collapse

VERSION =
"0.0.1"
VALID_SORT =
%w(asc desc).freeze

Instance Method Summary collapse

Instance Method Details

#createObject



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/generic_controller.rb', line 7

def create
  @resource = cscope.new(model_params)
  @resource.save!
  respond_to do |f|
    f.any do
      response.headers['location'] = send("v#{api_version}_#{model_name.underscore}_url", @resource)
      render({ json: @resource,
               serializer: each_sez,
               status: :created }.merge!(render_options))
    end
  end
end

#destroyObject



20
21
22
23
24
25
26
27
# File 'lib/generic_controller.rb', line 20

def destroy
  model.destroy(params[:id])
  respond_to do |f|
    f.any do
      head :no_content
    end
  end
end

#indexObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/generic_controller.rb', line 55

def index
  query = cscope
  l = limit.to_i
  p = page.to_i
  off = offset.to_i
  o = order_by.to_sym
  s = sort.to_sym
  respond_to do |f|
    f.any do
      render({
               json: query.order(o => s)
                 .page(p)
                 .per(l)
                 .offset(off),
               meta: {
                 total: query.count(:all),
                 page: p,
                 limit: l,
                 order_by: o,
                 sort: s,
                 offset: off
               },
               each_serializer: each_sez,
               status: :ok
             }.merge!(render_options))
    end
  end
end

#showObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/generic_controller.rb', line 29

def show
  respond_to do |f|
    f.any do
      render({
               json: @resource,
               serializer: each_sez,
               status: :ok
             }.merge!(render_options))
    end
  end
end

#updateObject



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/generic_controller.rb', line 41

def update
  @resource.attributes = model_params
  @resource.save!
  respond_to do |f|
    f.any do
      render({
               json: @resource,
               serializer: each_sez,
               status: :ok
             }.merge!(render_options))
    end
  end
end