From 681f94843d28531a5f81dfb37cf8d36b3e908c99 Mon Sep 17 00:00:00 2001 From: Jeremy Ward Date: Wed, 7 Jun 2023 08:54:28 -0400 Subject: [PATCH] [Active Record] Add belongs_to_resource to active record --- Gemfile | 2 + .../associations/active_record.rb | 12 ++++++ lib/active_resource/railtie.rb | 6 +++ test/cases/active_record_association_test.rb | 42 +++++++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 lib/active_resource/associations/active_record.rb create mode 100644 test/cases/active_record_association_test.rb diff --git a/Gemfile b/Gemfile index f011e1e23f..b61b4f9e55 100644 --- a/Gemfile +++ b/Gemfile @@ -8,6 +8,8 @@ branch = ENV.fetch("BRANCH", "main") gem "activesupport", github: "rails/rails", branch: branch gem "activemodel", github: "rails/rails", branch: branch gem "activejob", github: "rails/rails", branch: branch +gem "activerecord", github: "rails/rails", branch: branch +gem "sqlite3" gem "rubocop" gem "rubocop-minitest" diff --git a/lib/active_resource/associations/active_record.rb b/lib/active_resource/associations/active_record.rb new file mode 100644 index 0000000000..5bea3dbcee --- /dev/null +++ b/lib/active_resource/associations/active_record.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +module ActiveResource + module Associations + module ActiveRecord + def belongs_to_resource(name, class_name: nil) + klass = class_name&.constantize || name.to_s.classify.constantize + define_method(name) { klass.find(send("#{name}_id")) } + end + end + end +end diff --git a/lib/active_resource/railtie.rb b/lib/active_resource/railtie.rb index 8733e5516a..296e7e7883 100644 --- a/lib/active_resource/railtie.rb +++ b/lib/active_resource/railtie.rb @@ -21,5 +21,11 @@ class Railtie < Rails::Railtie app.config.active_job.custom_serializers << ActiveResource::ActiveJobSerializer end end + + initializer "active_resource.patch_active_record" do |app| + ActiveSupport.on_load(:active_record) do + ActiveRecord::Base.extend(ActiveResource::Associations::ActiveRecord) + end + end end end diff --git a/test/cases/active_record_association_test.rb b/test/cases/active_record_association_test.rb new file mode 100644 index 0000000000..ae3f07e82d --- /dev/null +++ b/test/cases/active_record_association_test.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +require "active_record" +require "active_resource/associations/active_record" + +class ActiveRecordAssociationTest < ActiveSupport::TestCase + setup do + setup_response # find me in abstract_unit + + ActiveRecord::Base.establish_connection( + adapter: "sqlite3", + database: ":memory:" + ) + ActiveRecord::Schema.define do + self.verbose = false + + create_table :test_records, force: true do |t| + t.string :name + t.belongs_to :person + t.belongs_to :book + t.timestamps + end + end + + ActiveRecord::Base.extend(ActiveResource::Associations::ActiveRecord) + + class TestRecord < ActiveRecord::Base + belongs_to_resource :person + belongs_to_resource :book, class_name: "Product" + end + end + + def test_belongs_to_resource + record = TestRecord.create(name: "test", person_id: 1) + assert_equal record.person.name, "Matz" + end + + def test_belongs_to_resource_with_class_name + record = TestRecord.create(name: "test", person_id: 1, book_id: 1) + assert_equal record.book.name, "Rails book" + end +end