Class: Morpho::Operations::Base::Fetch
- Inherits:
-
Trailblazer::Operation
- Object
- Trailblazer::Operation
- Morpho::Operations::Base::Fetch
- Defined in:
- app/concepts/morpho/operations/base/fetch.rb
Overview
Base fetch operation
Instance Method Summary collapse
- #build_models!(options) ⇒ Object
- #filter!(options, params:) ⇒ Object
- #init!(options) ⇒ Object
- #meta!(options, params:) ⇒ Object
- #model_class!(options) ⇒ Object
- #page!(options, params:) ⇒ Object
- #paginate!(options, page:) ⇒ Object
- #params!(options) ⇒ Object
- #present!(options, meta:, models:) ⇒ Object
- #search!(options) ⇒ Object
- #update_meta!(options) ⇒ Object
Instance Method Details
#build_models!(options) ⇒ Object
57 58 59 |
# File 'app/concepts/morpho/operations/base/fetch.rb', line 57 def build_models!(, **) ['models'] = ['model.class'].all end |
#filter!(options, params:) ⇒ Object
61 62 63 |
# File 'app/concepts/morpho/operations/base/fetch.rb', line 61 def filter!(, params:, **) ['filter'] = params.fetch('filter', {}) end |
#init!(options) ⇒ Object
20 21 22 |
# File 'app/concepts/morpho/operations/base/fetch.rb', line 20 def init!(, **) ['default.page.size'] ||= 25 end |
#meta!(options, params:) ⇒ Object
28 29 30 |
# File 'app/concepts/morpho/operations/base/fetch.rb', line 28 def (, params:, **) ['meta'] = params.fetch('meta', {}) end |
#model_class!(options) ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'app/concepts/morpho/operations/base/fetch.rb', line 46 def model_class!(, **) model_class = ['model.class'] unless model_class.is_a?(Class) && model_class.ancestors .include?( ::ActiveRecord::Base ) raise ArgumentError, 'Supply a valid model.class option' end end |
#page!(options, params:) ⇒ Object
65 66 67 68 69 70 71 |
# File 'app/concepts/morpho/operations/base/fetch.rb', line 65 def page!(, params:, **) ['page'] = params.fetch( 'page', 'number' => 1, 'size' => ['default.page.size'] ) end |
#paginate!(options, page:) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 |
# File 'app/concepts/morpho/operations/base/fetch.rb', line 73 def paginate!(, page:, **) page_size = page.fetch('size', 25) page_number = page.fetch('number', 1) if page && page_size.to_i.positive? ['models'] = ['models'] .page(page_number) .per(page_size) end end |
#params!(options) ⇒ Object
24 25 26 |
# File 'app/concepts/morpho/operations/base/fetch.rb', line 24 def params!(, **) ['params'] ||= {} end |
#present!(options, meta:, models:) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'app/concepts/morpho/operations/base/fetch.rb', line 91 def present!(, meta:, models:, **) data = if ['presenter.class'] ['presenter.class'].represent(models)['data'] else models end ['response'] = { data: data, meta: } end |
#search!(options) ⇒ Object
85 86 87 88 89 |
# File 'app/concepts/morpho/operations/base/fetch.rb', line 85 def search!(, **) ['models'] = ['models'] .ransack(['filter']) .result(distinct: ['ransack.distinct']) end |
#update_meta!(options) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/concepts/morpho/operations/base/fetch.rb', line 32 def (, **) if ['models'].try(:current_page) ['meta'].merge!( 'current_page' => ['models'].current_page, 'from' => ['models'].prev_page, 'last_page' => ['models'].total_pages, 'per_page' => ['models'].limit_value, 'to' => ['models'].next_page, 'total' => ['models'].total_count ) end end |