From 2ce6f33692d708fcc39069ea453638c764504e2e Mon Sep 17 00:00:00 2001 From: kenken Date: Tue, 30 Apr 2024 10:45:34 +0900 Subject: [PATCH 1/2] Fix arguments of has_many method --- lib/associations/associations.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/associations/associations.rb b/lib/associations/associations.rb index d426f3e..501e597 100644 --- a/lib/associations/associations.rb +++ b/lib/associations/associations.rb @@ -6,7 +6,7 @@ def self.extended(base) require_relative 'reflection_extensions' end - def has_many(association_id, **options) + def has_many(association_id, scope = nil, **options, &extension) if options[:through] klass_name = association_id.to_s.classify klass = From adece849e44cc2a8f1b8d41990dd1883d572217c Mon Sep 17 00:00:00 2001 From: kenken Date: Thu, 2 May 2024 12:44:38 +0900 Subject: [PATCH 2/2] Add test about has_many method --- .../active_record_extensions_spec.rb | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/spec/associations/active_record_extensions_spec.rb b/spec/associations/active_record_extensions_spec.rb index 1001df5..be773cf 100644 --- a/spec/associations/active_record_extensions_spec.rb +++ b/spec/associations/active_record_extensions_spec.rb @@ -31,6 +31,32 @@ def define_book_classes end end + def define_person_classes + define_ephemeral_class(:Country, ActiveHash::Base) do + self.data = [ + {:id => 1, :name => "Japan"} + ] + end + + define_ephemeral_class(:Person, ActiveRecord::Base) do + establish_connection :adapter => "sqlite3", :database => ":memory:" + connection.create_table(:people, :force => true) do |t| + end + + extend ActiveHash::Associations::ActiveRecordExtensions + end + + define_ephemeral_class(:Post, ActiveRecord::Base) do + establish_connection :adapter => "sqlite3", :database => ":memory:" + connection.create_table(:posts, :force => true) do |t| + t.integer :person_id + t.datetime :created_at + end + + belongs_to :person + end + end + def define_school_classes define_ephemeral_class(:Country, ActiveRecord::Base) do establish_connection :adapter => "sqlite3", :database => ":memory:" @@ -209,6 +235,22 @@ def define_doctor_classes expect(patient.physicians).to contain_exactly(physician1, physician2) end end + + describe "with a lambda" do + before do + define_person_classes + now = Time.now + @post_1 = Post.create! :person_id => 1, :created_at => now + @post_2 = Post.create! :person_id => 1, :created_at => 1.day.ago + Post.create! :person_id => 2, :created_at => now + Person.has_many :posts, lambda { order(created_at: :asc) } + end + + it "should find the correct records" do + person = Person.create :id => 1 + expect(person.posts).to eq([@post_2, @post_1]) + end + end end describe ActiveHash::Associations::ActiveRecordExtensions do