Module: Miscellany::SlicedResponse
- Extended by:
- ActiveSupport::Concern
- Includes:
- HttpErrorHandling
- Defined in:
- lib/miscellany/controller/sliced_response.rb
Defined Under Namespace
Modules: JbuilderTemplateExt Classes: Slice
Instance Method Summary collapse
-
#as_sliced_json(queryset, slice: nil, total_count: nil, &blk) ⇒ Object
rubocop:enable Metrics/ParameterLists.
- #bearcat_as_sliced_json(*args, transform: nil, **kwargs, &blk) ⇒ Object
- #slice_results(queryset, **kwargs) ⇒ Object
-
#sliced_json(queryset, slice_params = params, max_size: 50, default_size: 25, allow_all: false, default_sort: nil, valid_sorts: nil, &blk) ⇒ Object
rubocop:disable Metrics/ParameterLists.
Methods included from HttpErrorHandling
Instance Method Details
#as_sliced_json(queryset, slice: nil, total_count: nil, &blk) ⇒ Object
rubocop:enable Metrics/ParameterLists
38 39 40 41 |
# File 'lib/miscellany/controller/sliced_response.rb', line 38 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
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/miscellany/controller/sliced_response.rb', line 43 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(queryset, **kwargs) ⇒ Object
9 10 11 |
# File 'lib/miscellany/controller/sliced_response.rb', line 9 def slice_results(queryset, **kwargs) @sliced_data = sliced_json(queryset, **kwargs) { |x| x } end |
#sliced_json(queryset, slice_params = params, max_size: 50, default_size: 25, allow_all: false, default_sort: nil, valid_sorts: nil, &blk) ⇒ Object
rubocop:disable Metrics/ParameterLists
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# 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 ||= [] normalized_sorts = (valid_sorts, default: default_sort) 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, valid_sorts: normalized_sorts, ) slice.render_json end |