Module: Reports::ConstantOptions

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

Class Method Summary collapse

Class Method Details

.constant_klass(attr) ⇒ Object



42
43
44
# File 'app/commands/reports/constant_options.rb', line 42

def self.constant_klass(attr)
  attr.column_association.klass
end

.generate_filter_options(klass, **_options) ⇒ Object



10
11
12
# File 'app/commands/reports/constant_options.rb', line 10

def self.generate_filter_options(klass, **_options)
  generate_options(klass, "filter")
end

.generate_options(klass, type) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/commands/reports/constant_options.rb', line 14

def self.generate_options(klass, type)
  flattener_klass = Flattener.for(klass).pivot_or_self
  flattener_klass.flatten.lazy.select { |attr| is_constant?(attr) }.reject do |attr|
    # Ignore nested attributes (i.e. sales_manager_1_type)
    ::Reports::NestedAttributeOptions.is_nested?(attr)
  end.
    flat_map do |attr|
    name = ::Reports::Generator.build_option_name(attr.column_association_name)
    [{
       name: name,
       "#{type}OptionType": option_type_id(attr),
       associationColumnName: attr.name,
       associationName: constant_klass(attr).name,
       columnType: nil
     }].tap do |options|
      if is_grouped?(attr)
        options << {
          name: "#{name} Group",
          "#{type}OptionType": option_type_id(attr, is_group: true),
          associationColumnName: attr.name,
          associationName: constant_klass(attr).name,
          columnTypeId: nil
        }
      end
    end
  end
end

.generate_select_options(klass, **_options) ⇒ Object



6
7
8
# File 'app/commands/reports/constant_options.rb', line 6

def self.generate_select_options(klass, **_options)
  generate_options(klass, "select")
end

.is_constant?(attr) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'app/commands/reports/constant_options.rb', line 46

def self.is_constant?(attr)
  attr.column_association&.type == :constant
end

.is_grouped?(attr) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'app/commands/reports/constant_options.rb', line 50

def self.is_grouped?(attr)
  constant_klass(attr).supports_grouping?
end

.is_simple?(attr) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'app/commands/reports/constant_options.rb', line 54

def self.is_simple?(attr)
  constant_klass(attr).simple
end

.option_type_id(attr, is_group: false) ⇒ Object



58
59
60
61
62
63
64
# File 'app/commands/reports/constant_options.rb', line 58

def self.option_type_id(attr, is_group: false)
  if is_simple?(attr)
    is_group ? "SimpleConstantGroup" : "SimpleConstant"
  else
    is_group ? "ConstantGroup" : "Constant"
  end
end