Class: Morpho::Operations::Base::Fetch

Inherits:
Trailblazer::Operation
  • Object
show all
Defined in:
app/concepts/morpho/operations/base/fetch.rb

Overview

Base fetch operation

Instance Method Summary collapse

Instance Method Details

#build_models!(options) ⇒ Object



57
58
59
# File 'app/concepts/morpho/operations/base/fetch.rb', line 57

def build_models!(options, **)
  options['models'] = options['model.class'].all
end

#filter!(options, params:) ⇒ Object



61
62
63
# File 'app/concepts/morpho/operations/base/fetch.rb', line 61

def filter!(options, params:, **)
  options['filter'] = params.fetch('filter', {})
end

#init!(options) ⇒ Object



20
21
22
# File 'app/concepts/morpho/operations/base/fetch.rb', line 20

def init!(options, **)
  options['default.page.size'] ||= 25
end

#meta!(options, params:) ⇒ Object



28
29
30
# File 'app/concepts/morpho/operations/base/fetch.rb', line 28

def meta!(options, params:, **)
  options['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!(options, **)
  model_class = options['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!(options, params:, **)
  options['page'] = params.fetch(
    'page',
    'number' => 1,
    'size' => options['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!(options, page:, **)
  page_size = page.fetch('size', 25)
  page_number = page.fetch('number', 1)
  if  page &&
      page_size.to_i.positive?
    options['models'] = options['models']
                        .page(page_number)
                        .per(page_size)
  end
  options
end

#params!(options) ⇒ Object



24
25
26
# File 'app/concepts/morpho/operations/base/fetch.rb', line 24

def params!(options, **)
  options['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!(options, meta:, models:, **)
  data =
    if options['presenter.class']
      options['presenter.class'].represent(models)['data']
    else
      models
    end
  options['response'] = {
    data: data,
    meta: meta
  }
end

#search!(options) ⇒ Object



85
86
87
88
89
# File 'app/concepts/morpho/operations/base/fetch.rb', line 85

def search!(options, **)
  options['models'] = options['models']
                      .ransack(options['filter'])
                      .result(distinct: options['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 update_meta!(options, **)
  if options['models'].try(:current_page)
    options['meta'].merge!(
      'current_page' => options['models'].current_page,
      'from' => options['models'].prev_page,
      'last_page' => options['models'].total_pages,
      'per_page' => options['models'].limit_value,
      'to' => options['models'].next_page,
      'total' => options['models'].total_count
    )
  end
  options
end