Class: Effective::DatatablesController

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

Instance Method Summary collapse

Instance Method Details

#downloadObject



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
# File 'app/controllers/effective/datatables_controller.rb', line 27

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

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

  respond_to do |format|
    format.csv do
      headers.delete('Content-Length')

      headers['X-Accel-Buffering'] = 'no'
      headers['Cache-Control'] = 'no-cache'
      headers["Content-Type"] = @datatable.csv_content_type
      headers["Content-Disposition"] = %(attachment; filename="#{@datatable.csv_filename}")
      headers['Last-Modified'] = Time.zone.now.ctime.to_s

      self.response_body = @datatable.csv_stream
      response.status = 200
    end

    # format.csv do
    #   send_data(@datatable.csv_file, filename: @datatable.csv_filename, type: @datatable.csv_content_type, disposition: 'attachment')
    # end

    format.all do
      render(status: :unauthorized, body: 'Access Denied')
    end
  end
end

#reorderObject



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
83
84
85
86
87
88
89
90
91
# File 'app/controllers/effective/datatables_controller.rb', line 57

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 Exception => e
    render(status: :error, body: 'Unexpected Error')
  end

end

#showObject

This will respond to both a GET and a POST



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

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 Exception => 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