-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Massive overhaul to responder and adapter
- Loading branch information
Showing
13 changed files
with
241 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'active_model_serializers' | ||
|
||
module DataTables | ||
module ActiveModelSerializers | ||
class Adapter < ::ActiveModelSerializers::Adapter::Json | ||
extend ActiveSupport::Autoload | ||
autoload :Pagination | ||
autoload :Meta | ||
|
||
def serializable_hash(options = nil) | ||
options = serialization_options(options) | ||
|
||
serialized_hash = { | ||
data: ::ActiveModelSerializers::Adapter::Attributes.new(serializer, instance_options).serializable_hash(options) | ||
} | ||
serialized_hash[meta_key] = meta unless meta.blank? | ||
serialized_hash.merge!(pagination) unless pagination.blank? | ||
|
||
self.class.transform_key_casing!(serialized_hash, instance_options) | ||
end | ||
|
||
def meta_key | ||
instance_options.fetch(:meta_key, 'meta'.freeze) | ||
end | ||
|
||
protected | ||
|
||
def pagination | ||
Pagination.new(serializer).as_h | ||
end | ||
|
||
def meta | ||
{ | ||
sql: serializer.object.to_sql | ||
}.merge(instance_options.fetch(:meta, {})) | ||
# }.merge(Meta.new(@serializer)) | ||
end | ||
end | ||
end | ||
end | ||
|
||
# Register adapter manually (due to namespacing) | ||
ActiveModelSerializers::Adapter.register(:dt, DataTables::ActiveModelSerializers::Adapter) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module ActiveModelSerializers | ||
module Adapter | ||
class DataTables | ||
|
||
class Meta | ||
def initialize(serializer) | ||
@object = serializer.object | ||
@scope = serializer.scope | ||
|
||
# Use the return value of the block unless it is nil. | ||
if serializer._meta.respond_to?(:call) | ||
@value = instance_eval(&serializer._meta) | ||
else | ||
@value = serializer._meta | ||
end | ||
end | ||
|
||
def as_json | ||
@value | ||
end | ||
|
||
protected | ||
|
||
attr_reader :object, :scope | ||
end | ||
end | ||
end | ||
end |
22 changes: 22 additions & 0 deletions
22
lib/data_tables/active_model_serializers/adapter/pagination.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module DataTables | ||
module ActiveModelSerializers | ||
class Adapter | ||
class Pagination | ||
|
||
attr_reader :collection | ||
|
||
def initialize(serializer) | ||
@collection = serializer.object | ||
end | ||
|
||
def as_h | ||
{ | ||
recordsTotal: collection&.total_entries&.to_i, | ||
recordsFiltered: @collection&.unscope(:limit, :offset)&.count_estimate&.to_i | ||
} | ||
end | ||
|
||
end | ||
end | ||
end | ||
end |
56 changes: 56 additions & 0 deletions
56
lib/data_tables/active_model_serializers/register_dt_renderer.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# In controllers, use `render dt: model` rather than `render json: model, adapter: :dt`. | ||
# | ||
# For example, in a controller action, we can: | ||
# respond_to do |format| | ||
# format.dt { render dt: model } | ||
# end | ||
# | ||
# or | ||
# | ||
# render dt: model | ||
# | ||
# No wrapper format needed as it does not apply (i.e. no `wrap_parameters format: [dt]`) | ||
module DataTables | ||
module ActiveModelSerializers | ||
MEDIA_TYPE = 'application/json'.freeze | ||
HEADERS = { | ||
response: { 'CONTENT_TYPE'.freeze => MEDIA_TYPE }, | ||
request: { 'ACCEPT'.freeze => MEDIA_TYPE } | ||
}.freeze | ||
|
||
def self.install | ||
# actionpack/lib/action_dispatch/http/mime_types.rb | ||
Mime::Type.register_alias MEDIA_TYPE, :dt, %w( text/plain text/x-json application/jsonrequest application/dt application/datatable ) | ||
|
||
# if Rails::VERSION::MAJOR >= 5 | ||
# ActionDispatch::Request.parameter_parsers[:dt] = parser | ||
# else | ||
# ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:dt]] = parser | ||
# end | ||
|
||
::ActionController::Renderers.add :dt do |json, options| | ||
json = serialize_dt(json, options).to_json(options) unless json.is_a?(String) | ||
self.content_type ||= Mime[:dt] | ||
self.response_body = json | ||
end | ||
end | ||
|
||
module ControllerSupport | ||
def serialize_dt(resource, options) | ||
options[:adapter] = :dt | ||
options.fetch(:serialization_context) do | ||
options[:serialization_context] = ::ActiveModelSerializers::SerializationContext.new(request) | ||
end | ||
# Magic happens here | ||
resource = DataTables::Responder.respond(resource, request.params) | ||
get_serializer(resource, options) | ||
end | ||
end | ||
end | ||
end | ||
|
||
DataTables::ActiveModelSerializers.install | ||
|
||
ActiveSupport.on_load(:action_controller) do | ||
include DataTables::ActiveModelSerializers::ControllerSupport | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.