class ReportsKit::Reports::InferrableConfiguration

Constants

SUPPORTED_COLUMN_TYPES

Attributes

inferrable[RW]
inferrable_type[RW]
model_settings[RW]

Public Class Methods

new(inferrable, inferrable_type) click to toggle source
# 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.model_class, inferrable_type, key)
end

Public Instance Methods

column() click to toggle source
# File lib/reports_kit/reports/inferrable_configuration.rb, line 61
def column
  return unless inferred_settings
  inferred_settings[:column]
end
column_type() click to toggle source
# 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_strategy() click to toggle source
# 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?() click to toggle source
# File lib/reports_kit/reports/inferrable_configuration.rb, line 36
def configured_by_association?
  configuration_strategy == :association
end
configured_by_column?() click to toggle source
# File lib/reports_kit/reports/inferrable_configuration.rb, line 40
def configured_by_column?
  configuration_strategy == :column
end
configured_by_model?() click to toggle source
# File lib/reports_kit/reports/inferrable_configuration.rb, line 44
def configured_by_model?
  configuration_strategy == :model
end
configured_by_time?() click to toggle source
# File lib/reports_kit/reports/inferrable_configuration.rb, line 48
def configured_by_time?
  column_type == :datetime
end
inferred_settings() click to toggle source
# 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_one() click to toggle source
# 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_many() click to toggle source
# 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_class() click to toggle source
# File lib/reports_kit/reports/inferrable_configuration.rb, line 56
def instance_class
  return reflection.klass if reflection
  nil
end
reflection() click to toggle source
# File lib/reports_kit/reports/inferrable_configuration.rb, line 52
def reflection
  model_class.reflect_on_association(expression.to_sym)
end