Class: ReportsKit::Reports::InferrableConfiguration
- Inherits:
-
Object
- Object
- ReportsKit::Reports::InferrableConfiguration
- Defined in:
- lib/reports_kit/reports/inferrable_configuration.rb
Constant Summary collapse
- SUPPORTED_COLUMN_TYPES =
[ :boolean, :datetime, :integer, :string ]
Instance Attribute Summary collapse
-
#inferrable ⇒ Object
Returns the value of attribute inferrable.
-
#inferrable_type ⇒ Object
Returns the value of attribute inferrable_type.
Instance Method Summary collapse
- #column ⇒ Object
- #column_type ⇒ Object
- #configuration_strategy ⇒ Object
- #configured_by_association? ⇒ Boolean
- #configured_by_column? ⇒ Boolean
- #configured_by_model? ⇒ Boolean
- #configured_by_time? ⇒ Boolean
- #inferred_settings ⇒ Object
- #inferred_settings_from_has_many ⇒ Object
-
#initialize(inferrable, inferrable_type) ⇒ InferrableConfiguration
constructor
A new instance of InferrableConfiguration.
- #instance_class ⇒ Object
- #model_class ⇒ Object
- #model_configuration ⇒ Object
- #reflection ⇒ Object
- #settings_from_model ⇒ Object
Constructor Details
#initialize(inferrable, inferrable_type) ⇒ InferrableConfiguration
Returns a new instance of InferrableConfiguration.
15 16 17 18 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 15 def initialize(inferrable, inferrable_type) self.inferrable = inferrable self.inferrable_type = inferrable_type end |
Instance Attribute Details
#inferrable ⇒ Object
Returns the value of attribute inferrable.
11 12 13 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 11 def inferrable @inferrable end |
#inferrable_type ⇒ Object
Returns the value of attribute inferrable_type.
11 12 13 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 11 def inferrable_type @inferrable_type end |
Instance Method Details
#column ⇒ Object
67 68 69 70 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 67 def column return unless inferred_settings inferred_settings[:column] end |
#column_type ⇒ Object
97 98 99 100 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 97 def column_type column_type = model_class.columns_hash[key.to_s].try(:type) return column_type if SUPPORTED_COLUMN_TYPES.include?(column_type) end |
#configuration_strategy ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 20 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
33 34 35 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 33 def configured_by_association? configuration_strategy == :association end |
#configured_by_column? ⇒ Boolean
37 38 39 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 37 def configured_by_column? configuration_strategy == :column end |
#configured_by_model? ⇒ Boolean
41 42 43 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 41 def configured_by_model? configuration_strategy == :model end |
#configured_by_time? ⇒ Boolean
45 46 47 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 45 def configured_by_time? column_type == :datetime end |
#inferred_settings ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 72 def inferred_settings return { column: "#{model_class.table_name}.#{key}" } if configured_by_column? if configured_by_association? return { column: "#{model_class.table_name}.#{reflection.foreign_key}" } if reflection.macro == :belongs_to return inferred_settings_from_has_many if inferred_settings_from_has_many end {} end |
#inferred_settings_from_has_many ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 81 def inferred_settings_from_has_many 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 |
#instance_class ⇒ Object
62 63 64 65 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 62 def instance_class return reflection.class_name.constantize if reflection nil end |
#model_class ⇒ Object
107 108 109 110 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 107 def model_class return unless measure measure.model_class end |
#model_configuration ⇒ Object
102 103 104 105 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 102 def model_configuration return unless model_class && model_class.respond_to?(:reports_kit_configuration) model_class.reports_kit_configuration end |
#reflection ⇒ Object
58 59 60 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 58 def reflection model_class.reflect_on_association(key.to_sym) end |
#settings_from_model ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 49 def settings_from_model return {} if model_configuration.blank? return {} if model_configuration.public_send(inferrable_type).blank? config_hash = model_configuration.public_send(inferrable_type).find do |hash| hash[:key] == key end config_hash || {} end |