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.
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
- #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.
16 17 18 19 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 16 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.
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 |
Instance Method Details
#column ⇒ Object
68 69 70 71 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 68 def column return unless inferred_settings inferred_settings[:column] end |
#column_type ⇒ Object
117 118 119 120 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 117 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
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 21 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
34 35 36 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 34 def configured_by_association? configuration_strategy == :association end |
#configured_by_column? ⇒ Boolean
38 39 40 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 38 def configured_by_column? configuration_strategy == :column end |
#configured_by_model? ⇒ Boolean
42 43 44 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 42 def configured_by_model? configuration_strategy == :model end |
#configured_by_time? ⇒ Boolean
46 47 48 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 46 def configured_by_time? column_type == :datetime end |
#inferred_settings ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 73 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
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 82 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
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 99 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
63 64 65 66 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 63 def instance_class return reflection.class_name.constantize if reflection nil end |
#model_class ⇒ Object
127 128 129 130 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 127 def model_class return unless series series.model_class end |
#model_configuration ⇒ Object
122 123 124 125 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 122 def model_configuration return unless model_class && model_class.respond_to?(:reports_kit_configuration) model_class.reports_kit_configuration end |
#reflection ⇒ Object
59 60 61 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 59 def reflection model_class.reflect_on_association(expression.to_sym) end |
#settings_from_model ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/reports_kit/reports/inferrable_configuration.rb', line 50 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 |