Module: Miscellany::SlicedResponse
- Extended by:
- ActiveSupport::Concern
- Includes:
- HttpErrorHandling
- Defined in:
- lib/miscellany/controller/sliced_response.rb
Defined Under Namespace
Classes: Slice
Instance Method Summary collapse
-
#as_sliced_json(queryset, slice: nil, total_count: nil, &blk) ⇒ Object
Format the given data as a JSON slice, but doesn’t expect slicing parameters.
-
#bearcat_as_sliced_json(*args, transform: nil, **kwargs, &blk) ⇒ Object
Wrap a Bearcat API instance in a slicing API bearcat_as_sliced_json() do |params| bearcat_instance.courses(params) end.
-
#slice_results(*args, **kwargs, &blk) ⇒ Object
Deprecated.
- #sliced_json(queryset, slice_params = params, max_size: 50, default_size: 25, allow_all: false, default_sort: nil, valid_sorts: nil, &blk) ⇒ Object
Methods included from HttpErrorHandling
Instance Method Details
#as_sliced_json(queryset, slice: nil, total_count: nil, &blk) ⇒ Object
Format the given data as a JSON slice, but doesn’t expect slicing parameters
43 44 45 46 |
# File 'lib/miscellany/controller/sliced_response.rb', line 43 def as_sliced_json(queryset, slice: nil, total_count: nil, &blk) slice = Slice.build(queryset, slice, total_count: total_count, item_transformer: blk) slice.render_json end |
#bearcat_as_sliced_json(*args, transform: nil, **kwargs, &blk) ⇒ Object
Wrap a Bearcat API instance in a slicing API bearcat_as_sliced_json() do |params|
bearcat_instance.courses(params)
end
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/miscellany/controller/sliced_response.rb', line 52 def bearcat_as_sliced_json(*args, transform: nil, **kwargs, &blk) bearcat_exec = ->(slice) { response = blk.call({ per_page: slice.page_size, page: slice.page_number, }) slice.rendered_json[:page_count] = response.page_count response } sliced_json(bearcat_exec, *args, valid_sorts: {}, **kwargs, allow_all: false, &transform) end |
#slice_results(*args, **kwargs, &blk) ⇒ Object
Deprecated
10 11 12 |
# File 'lib/miscellany/controller/sliced_response.rb', line 10 def slice_results(*args, **kwargs, &blk) @sliced_data = sliced_json(*args, **kwargs, &blk) end |
#sliced_json(queryset, slice_params = params, max_size: 50, default_size: 25, allow_all: false, default_sort: nil, valid_sorts: nil, &blk) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/miscellany/controller/sliced_response.rb', line 14 def sliced_json( queryset, slice_params = params, max_size: 50, default_size: 25, allow_all: false, default_sort: nil, valid_sorts: nil, &blk ) valid_sorts ||= queryset.column_names if queryset.respond_to?(:column_names) valid_sorts ||= [] if !valid_sorts.present? && defined?(Miscellany::ComplexQuery) && queryset.is_a?(Miscellany::ComplexQuery) sort_parser = queryset.send(:sort_parser) else sort_parser = Miscellany::SortLang::Parser.new(valid_sorts, default: default_sort) end slice = Slice.build( queryset, slice_params, default_page_size: default_size, item_transformer: blk, max_size: max_size, default_size: default_size, allow_all: allow_all, sort_parser: sort_parser, ) slice.render_json end |