Sha256: b8588ee2b22db1f8838d39002af0ad18b47277eb52a5c6bb53db23d7426cac9b

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

module Effective
  class DatatablesController < ApplicationController
    skip_log_page_views quiet: true if defined?(EffectiveLogging)

    # This will respond to both a GET and a POST
    def show
      begin
        @datatable = find_datatable(params[:id]).try(:new) || raise('unable to find datatable')
        @datatable.view = view_context

        EffectiveDatatables.authorized?(self, :index, @datatable.collection_class)

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

        render json: error_json(message: e.message)
      end
    end

    private

    def find_datatable(id)
      id = id.gsub('-', '/')
      id.classify.safe_constantize || id.classify.pluralize.safe_constantize
    end

    def error_json(message:)
      {
        data: [],
        draw: params[:draw].to_i,
        effective_datatables_error: message.presence || 'unknown error',
        recordsTotal: 0,
        recordsFiltered: 0,
        aggregates: [],
        charts: {}
      }
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
effective_datatables-3.1.0 app/controllers/effective/datatables_controller.rb