Class: Effective::DatatablesController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/effective/datatables_controller.rb

Instance Method Summary collapse

Instance Method Details

#reorderObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/effective/datatables_controller.rb', line 23

def reorder
  begin
    @datatable = EffectiveDatatables.find(params[:id], params[:attributes])
    @datatable.view = view_context

    EffectiveDatatables.authorize!(self, :index, @datatable.collection_class)

    @resource = @datatable.collection.find(params[:reorder][:id])

    EffectiveDatatables.authorize!(self, :update, @resource)

    attribute = @datatable.columns[:_reorder][:reorder]
    old_index = params[:reorder][:old].to_i
    new_index = params[:reorder][:new].to_i

    @resource.class.transaction do
      if new_index > old_index
        @datatable.collection.where("#{attribute} > ? AND #{attribute} <= ?", old_index, new_index).update_all("#{attribute} = #{attribute} - 1")
        @resource.update_column(attribute, new_index)
      end

      if old_index > new_index
        @datatable.collection.where("#{attribute} >= ? AND #{attribute} < ?", new_index, old_index).update_all("#{attribute} = #{attribute} + 1")
        @resource.update_column(attribute, new_index)
      end
    end

    render status: :ok, body: :ok
  rescue Effective::AccessDenied => e
    render(status: :unauthorized, body: 'Access Denied')
  rescue => e
    render(status: :error, body: 'Unexpected Error')
  end

end

#showObject

This will respond to both a GET and a POST



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/controllers/effective/datatables_controller.rb', line 6

def show
  begin
    @datatable = EffectiveDatatables.find(params[:id], params[:attributes])
    @datatable.view = view_context

    EffectiveDatatables.authorize!(self, :index, @datatable.collection_class)

    render json: @datatable.to_json
  rescue => e
    EffectiveDatatables.authorized?(self, :index, @datatable.try(:collection_class))
    render json: error_json(e)

    ExceptionNotifier.notify_exception(e) if defined?(ExceptionNotifier)
    raise e if Rails.env.development?
  end
end