Class: ReportsKit::Reports::InferrableConfiguration

Inherits:
Object
  • Object
show all
Defined in:
lib/reports_kit/reports/inferrable_configuration.rb

Constant Summary collapse

SUPPORTED_COLUMN_TYPES =
[
  :boolean,
  :datetime,
  :integer,
  :string,
  :text
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inferrable, inferrable_type) ⇒ InferrableConfiguration

Returns a new instance of InferrableConfiguration.



17
18
19
20
21
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 17

def initialize(inferrable, inferrable_type)
  self.inferrable = inferrable
  self.inferrable_type = inferrable_type
  self.model_settings = ModelSettings.new(series, inferrable_type, key)
end

Instance Attribute Details

#inferrableObject

Returns the value of attribute inferrable.



12
13
14
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 12

def inferrable
  @inferrable
end

#inferrable_typeObject

Returns the value of attribute inferrable_type.



12
13
14
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 12

def inferrable_type
  @inferrable_type
end

#model_settingsObject

Returns the value of attribute model_settings.



12
13
14
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 12

def model_settings
  @model_settings
end

Instance Method Details

#columnObject



61
62
63
64
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 61

def column
  return unless inferred_settings
  inferred_settings[:column]
end

#column_typeObject



110
111
112
113
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 110

def column_type
  column_type = model_class.columns_hash[expression.to_s].try(:type)
  return column_type if SUPPORTED_COLUMN_TYPES.include?(column_type)
end

#configuration_strategyObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 23

def configuration_strategy
  if settings_from_model.present?
    :model
  elsif reflection
    :association
  elsif column_type
    :column
  else
    inferrable_type_string = inferrable_type.to_s.singularize
    raise ArgumentError.new("No configuration found on the #{model_class} model for #{inferrable_type_string} with key: '#{key}'")
  end
end

#configured_by_association?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 36

def configured_by_association?
  configuration_strategy == :association
end

#configured_by_column?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 40

def configured_by_column?
  configuration_strategy == :column
end

#configured_by_model?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 44

def configured_by_model?
  configuration_strategy == :model
end

#configured_by_time?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 48

def configured_by_time?
  column_type == :datetime
end

#inferred_settingsObject



66
67
68
69
70
71
72
73
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 66

def inferred_settings
  return { column: "#{model_class.table_name}.#{expression}" } if configured_by_column?
  if configured_by_association?
    return inferred_settings_from_belongs_to_or_has_one if inferred_settings_from_belongs_to_or_has_one
    return inferred_settings_from_has_many if inferred_settings_from_has_many
  end
  {}
end

#inferred_settings_from_belongs_to_or_has_oneObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 75

def inferred_settings_from_belongs_to_or_has_one
  @inferred_settings_from_belongs_to_or_has_one ||= begin
    return unless reflection.macro.in?([:belongs_to, :has_one])
    through_reflection = reflection.through_reflection
    if through_reflection
      {
        joins: through_reflection.name,
        column: "#{through_reflection.table_name}.#{reflection.source_reflection.foreign_key}"
      }
    else
      {
        column: "#{model_class.table_name}.#{reflection.foreign_key}"
      }
    end
  end
end

#inferred_settings_from_has_manyObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 92

def inferred_settings_from_has_many
  @inferred_settings_from_has_many ||= begin
    return unless reflection.macro == :has_many
    through_reflection = reflection.through_reflection
    if through_reflection
      {
        joins: through_reflection.name,
        column: "#{through_reflection.table_name}.#{reflection.source_reflection.foreign_key}"
      }
    else
      {
        joins: reflection.name,
        column: "#{reflection.klass.table_name}.#{reflection.klass.primary_key}"
      }
    end
  end
end

#instance_classObject



56
57
58
59
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 56

def instance_class
  return reflection.klass if reflection
  nil
end

#reflectionObject



52
53
54
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 52

def reflection
  model_class.reflect_on_association(expression.to_sym)
end