Module: Reports::BelongsToOptions

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

Class Method Summary collapse

Class Method Details

.association_and_descendants(association) ⇒ Object



134
135
136
137
138
139
# File 'app/commands/reports/belongs_to_options.rb', line 134

def self.association_and_descendants(association)
  [
    [association.klass, association.name.to_s],
    *association.klass.descendants.map { |d| [d, format_descendent_name(d, association)] }
  ]
end

.base_resources(klass) ⇒ Object



127
128
129
130
131
132
# File 'app/commands/reports/belongs_to_options.rb', line 127

def self.base_resources(klass)
  ::HasHelpers::Resource.where(
    name: klass.name.demodulize,
    resource_type: ::HasHelpers::ResourceType.where(name: "Base")
  ).exists?
end

.belongs_to_associations(klass) ⇒ Object

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



116
117
118
119
120
121
# File 'app/commands/reports/belongs_to_options.rb', line 116

def self.belongs_to_associations(klass)
  flattener(klass).associations.
    reject(&:is_constant?).
    select { |association| association.macro == :belongs_to }.
    reject { |association| association.flattener.class.is_pivot? }
end

.date_attributes(klass) ⇒ Object

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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/commands/reports/belongs_to_options.rb', line 142

def self.date_attributes(klass)
  model = klass.is_a?(Class) ? klass : klass.class

  model.columns.map do |column|
    sql_type = ::Flattener::Column::Types.sql_type_to_type(column.sql_type)
    {
      name: column.name,
      column_type: sql_type,
      source: model.name
    }
  end.select do |attr_info|
    ["date", "datetime"].include?(attr_info[:column_type].to_s)
  end
end

.date_cast_options(date_options) ⇒ Object



157
158
159
160
161
162
163
164
165
166
# File 'app/commands/reports/belongs_to_options.rb', line 157

def self.date_cast_options(date_options)
  date_options.lazy.select { |option| option[:column_type] == "datetime" }.map do |option|
    date_time_cast_name = ::Reports::Generator.build_option_name(option[:name].sub(/\sat$/i, ""), "date")
    next if date_options.any? { |o| o[:name] == date_time_cast_name }
    option.merge(
      name: date_time_cast_name,
      column_type: "date_cast",
    )
  end.reject(&:nil?)
end

.flattener(klass) ⇒ Object



123
124
125
# File 'app/commands/reports/belongs_to_options.rb', line 123

def self.flattener(klass)
  Flattener.for(klass).pivot_or_self
end

.format_descendent_name(descendant, association) ⇒ Object

To avoid naming conflicts, suffix the descendant's name with the association unless the association's name matches the association's klass name



170
171
172
173
174
175
176
# File 'app/commands/reports/belongs_to_options.rb', line 170

def self.format_descendent_name(descendant, association)
  if association.name.to_s.classify == association.klass.name.demodulize
    descendant.name.underscore
  else
    "#{descendant.name.underscore}_#{association.name}"
  end
end

.generate_filter_options(klass, resource_id, nested_as: nil, **_options) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/commands/reports/belongs_to_options.rb', line 56

def self.generate_filter_options(
  klass,
  resource_id,
  nested_as: nil,
  **_options
)
  klass_name = klass.name.demodulize
  Enumerator.new do |yielder|
    # Yield a filter option for the base table.  (For filters, this behaves like a Belongs To filter option)
    if base_resources(klass)
      yielder << {
        name: klass_name,
        filterOptionType: "BelongsTo",
        associationResourceId: resource_id.to_i,
        associationName: klass.name.underscore,
        associationColumnName: "id",
      }
    end
    # Yield a filter option for each belongs to association
    belongs_to_associations(klass).each do |association|
      foreign_key = association.foreign_key
      column = klass.columns_hash[foreign_key]
      nullable = !column || column.null
      join_type = nullable ? "Left" : "Inner"
      association_and_descendants(association).each do |association_klass, association_name|
        response = Reports::ExternalGraphqlClient.query_resource_by_name(name: association_klass.name.demodulize)
        association_resource_id = response.dig("data", "resourceName", "id")
        yielder << {
          name: ::Reports::Generator.build_option_name(*nested_as, association_name),
          filterOptionType: "BelongsTo",
          associationResourceId: association_resource_id.to_i,
          associationName: association_name,
          associationColumnName: [*nested_as, association.foreign_key].join("_"),
          joinType: join_type,
        }
      end
    end
    date_resource = Reports::ExternalGraphqlClient.query_resource_by_name(name: "DwDate")
    date_resource_id = date_resource.dig("data", "resourceName", "id")
    if date_resource
      date_options = date_attributes(klass).map do |key|
        attr = key[:name]
        column_type = key[:column_type]
        {
          name: "#{::Reports::Generator.build_option_name(*nested_as, attr)} Date",
          filterOptionType: "BelongsTo",
          columnType: column_type.to_s,
          associationResourceId: date_resource_id.to_i,
          associationName: attr,
          associationColumnName: attr,
          joinType: "Left",
        }
      end.to_a
      date_options.each { |option| yielder << option }
      date_cast_options(date_options).each { |option| yielder << option }
    end
  end
end

.generate_select_options(klass, nested_as: nil, **_options) ⇒ Object



6
7
8
9
10
11
12
13
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/commands/reports/belongs_to_options.rb', line 6

def self.generate_select_options(
  klass,
  nested_as: nil,
  **_options
)
  Enumerator.new do |yielder|
    # Yield a select option for each belongs to association
    belongs_to_associations(klass).
      each do |association|
        foreign_key = association.foreign_key
        column = klass.columns_hash[foreign_key] # Physical column in the DB
        nullable = !column || column.null
        join_type = nullable ? "Left" : "Inner"
        association_and_descendants(association).each do |association_klass, association_name|
          response = Reports::ExternalGraphqlClient.query_resource_by_name(name: association_klass.name.demodulize)
          association_resource_id = response.dig("data", "resourceName", "id")
          yielder << {
            name:                    ::Reports::Generator.build_option_name(*nested_as, association_name),
            selectOptionType:        "BelongsTo",
            columnType:              "link",
            associationResourceId:   association_resource_id.to_i,
            associationName:         association_name,
            associationColumnName:   [*nested_as, foreign_key].join("_"),
            joinType:                join_type
          }
        end
      end
    # Also generate a select option for each base attribute which is a date or datetime.
    date_resource = Reports::ExternalGraphqlClient.query_resource_by_name(name: "DwDate")
    date_resource_id = date_resource.dig("data", "resourceName", "id")
    if date_resource
      date_options = date_attributes(klass).map do |key|
        attr = key[:name]
        column_type = key[:column_type]
        {
          name: "#{::Reports::Generator.build_option_name(*nested_as, attr)} Date",
          selectOptionType: "BelongsTo",
          columnType: column_type.to_s,
          associationResourceId: date_resource_id.to_i,
          associationName: attr,
          associationColumnName: attr,
          joinType: "Left",
        }
      end.to_a
      date_options.each { |option| yielder << option }
      date_cast_options(date_options).each { |option| yielder << option }
    end
  end
end