Class: AndOne::Suggestion
- Inherits:
-
Object
- Object
- AndOne::Suggestion
- Defined in:
- lib/and_one/association_resolver.rb
Instance Attribute Summary collapse
-
#association_name ⇒ Object
readonly
Returns the value of attribute association_name.
-
#fix_hint ⇒ Object
readonly
Returns the value of attribute fix_hint.
-
#is_polymorphic ⇒ Object
readonly
Returns the value of attribute is_polymorphic.
-
#is_through ⇒ Object
readonly
Returns the value of attribute is_through.
-
#loading_strategy ⇒ Object
readonly
Returns the value of attribute loading_strategy.
-
#origin_frame ⇒ Object
readonly
Returns the value of attribute origin_frame.
-
#parent_model ⇒ Object
readonly
Returns the value of attribute parent_model.
-
#target_model ⇒ Object
readonly
Returns the value of attribute target_model.
Instance Method Summary collapse
- #actionable? ⇒ Boolean
-
#initialize(target_model:, origin_frame:, association_name:, parent_model:, fix_hint:, loading_strategy: nil, is_through: false, is_polymorphic: false) ⇒ Suggestion
constructor
A new instance of Suggestion.
-
#loading_strategy_hint ⇒ Object
Suggest the optimal loading strategy when it differs from plain .includes.
-
#strict_loading_hint ⇒ Object
Suggest strict_loading as an alternative prevention strategy.
Constructor Details
#initialize(target_model:, origin_frame:, association_name:, parent_model:, fix_hint:, loading_strategy: nil, is_through: false, is_polymorphic: false) ⇒ Suggestion
Returns a new instance of Suggestion.
179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/and_one/association_resolver.rb', line 179 def initialize(target_model:, origin_frame:, association_name:, parent_model:, fix_hint:, loading_strategy: nil, is_through: false, is_polymorphic: false) @target_model = target_model @origin_frame = origin_frame @association_name = association_name @parent_model = parent_model @fix_hint = fix_hint @loading_strategy = loading_strategy @is_through = is_through @is_polymorphic = is_polymorphic end |
Instance Attribute Details
#association_name ⇒ Object (readonly)
Returns the value of attribute association_name.
176 177 178 |
# File 'lib/and_one/association_resolver.rb', line 176 def association_name @association_name end |
#fix_hint ⇒ Object (readonly)
Returns the value of attribute fix_hint.
176 177 178 |
# File 'lib/and_one/association_resolver.rb', line 176 def fix_hint @fix_hint end |
#is_polymorphic ⇒ Object (readonly)
Returns the value of attribute is_polymorphic.
176 177 178 |
# File 'lib/and_one/association_resolver.rb', line 176 def is_polymorphic @is_polymorphic end |
#is_through ⇒ Object (readonly)
Returns the value of attribute is_through.
176 177 178 |
# File 'lib/and_one/association_resolver.rb', line 176 def is_through @is_through end |
#loading_strategy ⇒ Object (readonly)
Returns the value of attribute loading_strategy.
176 177 178 |
# File 'lib/and_one/association_resolver.rb', line 176 def loading_strategy @loading_strategy end |
#origin_frame ⇒ Object (readonly)
Returns the value of attribute origin_frame.
176 177 178 |
# File 'lib/and_one/association_resolver.rb', line 176 def origin_frame @origin_frame end |
#parent_model ⇒ Object (readonly)
Returns the value of attribute parent_model.
176 177 178 |
# File 'lib/and_one/association_resolver.rb', line 176 def parent_model @parent_model end |
#target_model ⇒ Object (readonly)
Returns the value of attribute target_model.
176 177 178 |
# File 'lib/and_one/association_resolver.rb', line 176 def target_model @target_model end |
Instance Method Details
#actionable? ⇒ Boolean
191 192 193 |
# File 'lib/and_one/association_resolver.rb', line 191 def actionable? !!@association_name end |
#loading_strategy_hint ⇒ Object
Suggest the optimal loading strategy when it differs from plain .includes
209 210 211 212 213 214 215 216 217 218 |
# File 'lib/and_one/association_resolver.rb', line 209 def loading_strategy_hint return nil unless actionable? && @loading_strategy case @loading_strategy when :eager_load "Consider `.eager_load(:#{@association_name})` instead — your query filters on the association, so a JOIN is more efficient" when :preload "Consider `.preload(:#{@association_name})` — separate queries avoid JOIN overhead for simple loading" end end |
#strict_loading_hint ⇒ Object
Suggest strict_loading as an alternative prevention strategy
196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/and_one/association_resolver.rb', line 196 def strict_loading_hint return nil unless actionable? && @parent_model assoc_type = if @is_through "has_many :#{@association_name}, through: ..." else "has_many :#{@association_name}" end "Or prevent at the model level: `#{assoc_type}, strict_loading: true` in #{@parent_model.name}" end |