File tree 4 files changed +70
-4
lines changed
4 files changed +70
-4
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,8 @@ branch = ENV.fetch("BRANCH", "main")
8
8
gem "activesupport" , github : "rails/rails" , branch : branch
9
9
gem "activemodel" , github : "rails/rails" , branch : branch
10
10
gem "activejob" , github : "rails/rails" , branch : branch
11
+ gem "activerecord" , github : "rails/rails" , branch : branch
12
+ gem "sqlite3"
11
13
12
14
gem "rubocop"
13
15
gem "rubocop-minitest"
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveResource
4
+ module Associations
5
+ module ActiveRecord
6
+ def self . included ( base )
7
+ base . extend ( ClassMethods )
8
+ end
9
+
10
+ module ClassMethods
11
+ def belongs_to_resource ( name , class_name : nil )
12
+ klass = class_name &.constantize || name . to_s . classify . constantize
13
+ define_method ( name ) { klass . find ( send ( "#{ name } _id" ) ) }
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
Original file line number Diff line number Diff line change @@ -9,16 +9,22 @@ class Railtie < Rails::Railtie
9
9
10
10
initializer "active_resource.set_configs" do |app |
11
11
ActiveSupport . on_load ( :active_resource ) do
12
- app . config . active_resource . each do |k , v |
13
- send "#{ k } =" , v
14
- end
12
+ app . config . active_resource . each { |k , v | send "#{ k } =" , v }
15
13
end
16
14
end
17
15
18
16
initializer "active_resource.add_active_job_serializer" do |app |
19
17
if app . config . try ( :active_job ) . try ( :custom_serializers )
20
18
require "active_resource/active_job_serializer"
21
- app . config . active_job . custom_serializers << ActiveResource ::ActiveJobSerializer
19
+ end
20
+ end
21
+
22
+ initializer "active_resource.patch_active_record" do |app |
23
+ ActiveSupport . on_load ( :active_record ) do
24
+ ActiveRecord ::Base . send (
25
+ :include ,
26
+ ActiveResource ::Associations ::ActiveRecord
27
+ )
22
28
end
23
29
end
24
30
end
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record"
4
+ require "active_resource/associations/active_record"
5
+
6
+ class ActiveRecordAssociationTest < ActiveSupport ::TestCase
7
+ setup do
8
+ setup_response # find me in abstract_unit
9
+ ActiveRecord ::Base . establish_connection (
10
+ adapter : "sqlite3" ,
11
+ database : ":memory:"
12
+ )
13
+ ActiveRecord ::Schema . define do
14
+ self . verbose = false
15
+
16
+ create_table :test_records , force : true do |t |
17
+ t . string :name
18
+ t . belongs_to :person
19
+ t . belongs_to :book
20
+ t . timestamps
21
+ end
22
+ end
23
+ end
24
+
25
+ class TestRecord < ActiveRecord ::Base
26
+ include ActiveResource ::Associations ::ActiveRecord
27
+ belongs_to_resource :person
28
+ belongs_to_resource :book , class_name : "Product"
29
+ end
30
+
31
+ def test_belongs_to_resource
32
+ record = TestRecord . create ( name : "test" , person_id : 1 )
33
+ assert_equal record . person . name , "Matz"
34
+ end
35
+
36
+ def test_belongs_to_resource_with_class_name
37
+ record = TestRecord . create ( name : "test" , person_id : 1 , book_id : 1 )
38
+ assert_equal record . book . name , "Rails book"
39
+ end
40
+ end
You can’t perform that action at this time.
0 commit comments