From 2213067545491c9c2b19ece584d02d3c10846a7f Mon Sep 17 00:00:00 2001 From: Julian Rubisch Date: Fri, 12 May 2023 11:55:18 +0200 Subject: [PATCH 1/4] fix: Fix nil scopes in method_missing and respond_to_missing? --- lib/active_hash/relation.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/active_hash/relation.rb b/lib/active_hash/relation.rb index b41581d1..6e513e29 100644 --- a/lib/active_hash/relation.rb +++ b/lib/active_hash/relation.rb @@ -164,13 +164,13 @@ def to_ary end def method_missing(method_name, *args) - return super unless klass.scopes.key?(method_name) + return super unless klass.scopes&.key?(method_name) instance_exec(*args, &klass.scopes[method_name]) end def respond_to_missing?(method_name, include_private = false) - klass.scopes.key?(method_name) || super + klass.scopes&.key?(method_name) || super end private From afb99deca0f8b8a55c0a1a466a24c1ea071ecee1 Mon Sep 17 00:00:00 2001 From: iberianpig Date: Wed, 2 Aug 2023 18:28:54 +0900 Subject: [PATCH 2/4] require reflection_extensions when ActiveRecordExtensions is extended `extend ActiveHash::Associations::ActiveRecordExtensions` does not require 'reflection_extensions' so the following error occurs when `compute_class` is called in Rails 7. ``` ArgumentError: Rails couldn't find a valid model for Rank association. Please provide the :class_name option on the association declaration. If :class_name is already provided, make sure it's an ActiveRecord::Base subclass. ``` --- lib/associations/associations.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/associations/associations.rb b/lib/associations/associations.rb index e41a7cd0..99e2f046 100644 --- a/lib/associations/associations.rb +++ b/lib/associations/associations.rb @@ -3,6 +3,10 @@ module Associations module ActiveRecordExtensions + def self.extended(base) + require_relative 'reflection_extensions' + end + def belongs_to(name, scope = nil, **options) klass_name = options.key?(:class_name) ? options[:class_name] : name.to_s.camelize klass = @@ -92,7 +96,6 @@ def belongs_to_active_hash(association_id, options = {}) end def self.included(base) - require_relative "reflection_extensions" base.extend Methods end From d23246d33ebada0eb3fec17e0f092fa79e92d90a Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Fri, 4 Aug 2023 16:28:20 -0400 Subject: [PATCH 3/4] feat: improve pp output for ActiveHash::Relation Previously the output might look like: #1, :name=>"1"}>, #2, :name=>"2"}>, #3, :name=>"3"}>, #4, :name=>"4"}>], @conditions=#, @klass=Layout, @order_values=[]> After this change the output looks like: [#1, :name=>"1"}>, #2, :name=>"2"}>, #3, :name=>"3"}>, #4, :name=>"4"}>] Closes #285 --- lib/active_hash/relation.rb | 4 ++++ spec/active_hash/relation_spec.rb | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/active_hash/relation.rb b/lib/active_hash/relation.rb index b41581d1..1e664f7c 100644 --- a/lib/active_hash/relation.rb +++ b/lib/active_hash/relation.rb @@ -22,6 +22,10 @@ def where(conditions_hash = :chain) spawn.where!(conditions_hash) end + def pretty_print(pp) + pp.pp(entries.to_ary) + end + class WhereChain attr_reader :relation diff --git a/spec/active_hash/relation_spec.rb b/spec/active_hash/relation_spec.rb index 432286d1..e40c7d0f 100644 --- a/spec/active_hash/relation_spec.rb +++ b/spec/active_hash/relation_spec.rb @@ -71,4 +71,16 @@ expect(klass.where(display: true).length).to eq(1) end end + + describe "#pretty_print" do + it "prints the records" do + out = StringIO.new + PP.pp(subject, out) + + expect(out.string.scan(/\bid\b/).length).to eq(2) + expect(out.string).to match(/\bCanada\b/) + expect(out.string).to match(/\bUS\b/) + expect(out.string).to_not match(/ActiveHash::Relation/) + end + end end From 9a8368264f4a6c5165a281a477d686545332114c Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Fri, 11 Aug 2023 10:47:22 -0400 Subject: [PATCH 4/4] doc: update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d440cea..d55ea51e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## next - unreleased - Fix relation matching when attribute name collides with a method. [#281](https://github.com/active-hash/active_hash/pull/281) @flavorjones +- Fix association reflection in applications that don't use ActiveHash::Associations. [#286](https://github.com/active-hash/active_hash/pull/286) @iberianpig ## Version [3.2.0] - 2023-05-06