Skip to content

Commit

Permalink
Be able to patch for nested associations require includes though ch…
Browse files Browse the repository at this point in the history
…ild associations are already included
  • Loading branch information
makicamel committed Oct 25, 2023
1 parent c77f71e commit 0a12cfe
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/bulletmark_repairer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'bulletmark_repairer/associations_builder'
require 'bulletmark_repairer/configuration'
require 'bulletmark_repairer/corrector_builder'
require 'bulletmark_repairer/loaded_associations'
require 'bulletmark_repairer/markers'
require 'bulletmark_repairer/patcher'

Expand Down
15 changes: 11 additions & 4 deletions lib/bulletmark_repairer/associations_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ def build(marker)
if associations[marker.index]
associations[marker.index].add(marker)
else
associations[marker.index] = Associations.new(marker, @application_associations)
associations[marker.index] = Associations.new(
marker,
@application_associations,
@loaded_associations
)
end
end

Expand All @@ -20,8 +24,9 @@ def associations

private

def initialize
def initialize(loaded_associations)
@application_associations = BulletmarkRepairer::ApplicationAssociations.new
@loaded_associations = BulletmarkRepairer::LoadedAssociations.new(loaded_associations)
end
end

Expand All @@ -44,10 +49,12 @@ def corrector(dir)

private

def initialize(marker, application_associations)
def initialize(marker, application_associations, loaded_associations)
@marker = marker
@associations = { base: marker.associations }
@application_associations = application_associations
@loaded_associations = loaded_associations
key = @loaded_associations.key(marker.base_class)
@associations = { base: key ? { key => marker.associations } : marker.associations }
end

# @return [Hash, nil]
Expand Down
33 changes: 33 additions & 0 deletions lib/bulletmark_repairer/loaded_associations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

module BulletmarkRepairer
class LoadedAssociations
attr_reader :associations

def key(target_klass_name)
key = target_klass_name.underscore

result = nil
@associations.each do |_base_klass_name, all_associations|
all_associations.each do |_key, associations|
# TODO: return key
# TODO: reccurent check
associations.any? do |values|
result = key.pluralize.to_sym if key.pluralize.to_sym.in?(values)
result = key.singularize.to_sym if key.singularize.to_sym.in?(values)
end
end
end
result
end

def initialize(original_associations)
@associations = Hash.new { |h, k| h[k] = {} }
original_associations.each do |base_class, all_associations|
all_associations.each do |key, associations|
@associations[base_class][key] = associations.to_a
end
end
end
end
end
8 changes: 4 additions & 4 deletions lib/bulletmark_repairer/patcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

module BulletmarkRepairer
class Patcher
def self.execute(notifications:, controller:, action:)
new(notifications: notifications, controller: controller, action: action).execute
def self.execute(notifications:, controller:, action:, loaded_associations:)
new(notifications: notifications, controller: controller, action: action, loaded_associations: loaded_associations).execute
end

def execute
Expand All @@ -24,9 +24,9 @@ def execute

private

def initialize(notifications:, controller:, action:)
def initialize(notifications:, controller:, action:, loaded_associations:)
@markers = Markers.new(notifications, controller: controller, action: action)
@associations_builder = BulletmarkRepairer::AssociationsBuilder.new
@associations_builder = BulletmarkRepairer::AssociationsBuilder.new(loaded_associations)
end
end
end
7 changes: 6 additions & 1 deletion lib/bulletmark_repairer/rack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,24 @@ def initialize(app)
end

def call(env)
Thread.current[:bulletmark_repaier_loaded_associations] = Hash.new do |hash, key|
hash[key] = { includes: Set.new, eager_load: Set.new, preload: Set.new }
end
@app.call(env)
ensure
begin
if Thread.current[:bullet_notification_collector].notifications_present?
BulletmarkRepairer::Patcher.execute(
notifications: Thread.current[:bullet_notification_collector],
controller: env['action_dispatch.request.parameters']['controller'],
action: env['action_dispatch.request.parameters']['action']
action: env['action_dispatch.request.parameters']['action'],
loaded_associations: Thread.current[:bulletmark_repaier_loaded_associations]
)
end
rescue StandardError => e
raise e if BulletmarkRepairer.config.debug?
end
Thread.current[:bulletmark_repaier_loaded_associations].clear
end
end
end
3 changes: 2 additions & 1 deletion spec/associations_builder/associations_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

RSpec.describe BulletmarkRepairer::Associations do
describe '#build_associations!' do
let(:associations) { described_class.new(parent_marker, application_associations) }
let(:associations) { described_class.new(parent_marker, application_associations, loaded_associations) }
let(:application_associations) { BulletmarkRepairer::ApplicationAssociations.new }
let(:loaded_associations) { BulletmarkRepairer::LoadedAssociations.new({}) }
let(:parent_marker) { double(:marker, base_class: parent_attributes.keys.first, associations: parent_attributes.values.first) }
let(:child_marker) { double(:marker, base_class: child_attributes.keys.first, associations: child_attributes.values.first) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def index
let(:patched_src) do
<<-SRC
def index
@plays = Play.includes([{:actors=>[:company]}])
@plays = Play.includes(:actors).includes({:actors=>[:company]})
end
SRC
end
Expand All @@ -25,5 +25,5 @@ def index

subject { get nested_though_only_grand_child_is_required_includes_path }

# it_behaves_like 'correctly patched'
it_behaves_like 'correctly patched'
end

0 comments on commit 0a12cfe

Please sign in to comment.