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, :text ]
Instance Attribute Summary collapse
-
#inferrable ⇒ Object
Returns the value of attribute inferrable.
-
#inferrable_type ⇒ Object
Returns the value of attribute inferrable_type.
-
#model_settings ⇒ Object
Returns the value of attribute model_settings.
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_belongs_to_or_has_one ⇒ Object
- #inferred_settings_from_has_many ⇒ Object
-
#initialize(inferrable, inferrable_type) ⇒ InferrableConfiguration
constructor
A new instance of InferrableConfiguration.
- #instance_class ⇒ Object
- #reflection ⇒ Object
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
#inferrable ⇒ Object
Returns the value of attribute inferrable.
12 13 14 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 12 def inferrable @inferrable end |
#inferrable_type ⇒ Object
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_settings ⇒ Object
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
#column ⇒ Object
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_type ⇒ Object
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_strategy ⇒ Object
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
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
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
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
48 49 50 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 48 def configured_by_time? column_type == :datetime end |
#inferred_settings ⇒ Object
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_one ⇒ Object
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_many ⇒ Object
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_class ⇒ Object
56 57 58 59 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 56 def instance_class return reflection.klass if reflection nil end |
#reflection ⇒ Object
52 53 54 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 52 def reflection model_class.reflect_on_association(expression.to_sym) end |