Module: Reports::BaseOptions

Defined in:
app/commands/reports/base_options.rb

Class Method Summary collapse

Class Method Details

.base_attributes(klass) ⇒ Object

Returns a lazy enumerator which yields the base attributes (only those available for reports) for the given class.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/commands/reports/base_options.rb', line 55

def self.base_attributes(klass)
  flattener_klass = Flattener.for(klass).pivot_or_self

  flattener_klass.new.flatten.lazy.
    select do |attr|
    is_base_option?(attr)
  end.
    map do |attr|
    # determine column type for each attribute
    [attr, attr.column.type]
  end.
    reject do |_attr, column_type|
    # Remove attributes which have a date or datetime type. These are handled as belongs_to relations.
    # Also, skip JSON columns because we don't generate select options for those
    [::Flattener::Column::Types::DATE, ::Flattener::Column::Types::DATETIME].include?(column_type)
  end
end

.generate_filter_options(klass, **_options) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/commands/reports/base_options.rb', line 29

def self.generate_filter_options(klass, **_options)
  base_attributes(klass).reject do |_attr, column_type|
    # Skip JSON columns because we don't generate filter options for those
    [::Flattener::Column::Types::JSON].include?(column_type)
  end.
    flat_map do |attr, column_type_name|
    expansions = [
      {}, # Create a non-expanded option
      # *attr.column.expand_on
    ]

    expansions.map do |_expansion|
      column_type = column_type_name.to_s

      ::Reports::FilterOptionGenerator.for(column_type_name).new(
        flattener_attr: nil,
        attr: attr.name,
        name: ::Reports::Generator.build_option_name(attr.name, column_type: column_type, preserve_id: true)
      ).generate
      #   option[:partialType] = nil #attr.column.partial_type_id if attr.column.partial_type TODO need to check for a method to get the partial type id
      # end
    end
  end
end

.generate_select_options(klass, resource_id, **_options) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/commands/reports/base_options.rb', line 6

def self.generate_select_options(klass, resource_id, **_options)
  base_attributes(klass).flat_map do |attr, column_type_name|
    expansions = [
      {}, # Create a non-expanded option
      # *attr.column.expand_on
    ]

    expansions.map do |_expansion|
      column_type = column_type_name.to_s
      # expanded_column_name = expansion[:column_name]
      # expanded_value       = expansion[:value]
      # expand_on_nil        = expansion[:allow_nil]

      {
        name: attr.name,
        columnType: column_type,
        associationColumnName: attr.name,
        resourceId: resource_id,
      }
    end
  end
end

.is_base_option?(attr) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
# File 'app/commands/reports/base_options.rb', line 73

def self.is_base_option?(attr)
  attr.column_association.nil? &&
    !(attr.resource.is_constant? && attr.column.name == "name") &&
    !::Reports::NestedAttributeOptions.is_nested?(attr)
end