Skip to content

Commit

Permalink
Merge branch 'active-hash:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
hotoolong authored Sep 12, 2023
2 parents 081a803 + 57092b4 commit e928edb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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] - <sub><sup>2023-05-06</sup></sub>
Expand Down
8 changes: 6 additions & 2 deletions lib/active_hash/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -164,13 +168,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
Expand Down
5 changes: 4 additions & 1 deletion lib/associations/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions spec/active_hash/relation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e928edb

Please sign in to comment.