Class: AndOne::Suggestion

Inherits:
Object
  • Object
show all
Defined in:
lib/and_one/association_resolver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_nameObject (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_hintObject (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_polymorphicObject (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_throughObject (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_strategyObject (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_frameObject (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_modelObject (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_modelObject (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

Returns:



191
192
193
# File 'lib/and_one/association_resolver.rb', line 191

def actionable?
  !!@association_name
end

#loading_strategy_hintObject

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_hintObject

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