Class: Marty::Postings::SummaryGrid

Inherits:
Grid
  • Object
show all
Defined in:
app/components/marty/postings/summary_grid.rb

Constant Summary

Constants included from Marty::Permissions

Marty::Permissions::NETZKE_ENDPOINTS

Instance Method Summary collapse

Methods inherited from Grid

#child_components, #class_can?, #configure_form_window, #get_json_sorter, #has_search_action?, #initialize, #linked_components

Methods included from Marty::Permissions

#can_call_endpoint?, #can_perform_action?, #can_perform_actions, #current_user_roles, extended, #has_any_perm?, #has_marty_permissions, #has_perm?

Constructor Details

This class inherits a constructor from Marty::Grid

Instance Method Details

#class_list(posting_type) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'app/components/marty/postings/summary_grid.rb', line 71

def class_list(posting_type)
  method_name = "class_list_#{posting_type}".downcase.to_sym

  if Marty::DataChange.respond_to?(method_name)
    Marty::DataChange.send(method_name)
  else
    Marty::DataChange.class_list
  end
end

#configure(c) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/components/marty/postings/summary_grid.rb', line 4

def configure(c)
  super

  c.model = 'Marty::Log' # Hack, since grid requires a model to be set
  c.multi_select = false
  c.read_only = true
  c.attributes ||= [:klass, :created, :updated, :deleted]
  c.title ||= I18n.t('summary', default: 'Summary')
  c.paging = false
  c.min_height = 400
  c.height = 400
  c.bbar = nil
end

#count_records(_params, _columns = []) ⇒ Object



92
93
94
95
96
97
# File 'app/components/marty/postings/summary_grid.rb', line 92

def count_records(_params, _columns = [])
  return 0 if config[:selected_posting_type].blank?
  return 0 if config[:selected_posting_type].to_i.negative?

  Marty::DataChange.class_list.size + 1
end

#get_records(params) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/components/marty/postings/summary_grid.rb', line 39

def get_records(params)
  return [] if config[:selected_posting_type].blank?
  return [] if config[:selected_posting_type].to_i.negative?

  last_posting = Marty::Posting.where(
    posting_type: config[:selected_posting_type]
  ).where.not(created_dt: 'infinity').order(:created_dt).last

  start_dt = last_posting&.created_dt || 1.year.ago
  end_dt = Time.zone.now

  posting_type = Marty::PostingType[config[:selected_posting_type]]

  summary_records = class_list(posting_type).map do |klass|
    summary = Marty::DataChange.change_summary(start_dt, end_dt, klass)
    OpenStruct.new(summary.merge('klass' => klass))
  end

  records_with_changes = summary_records.select do |record|
    record.created > 0 || record.updated > 0 || record.deleted > 0
  end

  total_summary = OpenStruct.new(
    klass: 'Total',
    created: summary_records.sum(&:created),
    updated: summary_records.sum(&:updated),
    deleted: summary_records.sum(&:deleted)
  )

  [total_summary] + sort_records(params, records_with_changes)
end

#sort_records(params, records) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'app/components/marty/postings/summary_grid.rb', line 81

def sort_records(params, records)
  return records if params['sorters'].blank?

  sorting_attr = params.dig('sorters', 0, 'property').to_sym

  sorted = records.sort_by(&:sorting_attr)
  sorted = sorted.reverse if params.dig('sorters', 0, 'direction') != 'ASC'

  sorted
end