Skip to content

Commit

Permalink
Allow model option to be passed as string or constant.
Browse files Browse the repository at this point in the history
  • Loading branch information
asedge committed Mar 22, 2023
1 parent aba1a45 commit 9795cd6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/active_force/association/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize parent, relation_name, options = {}
end

def relation_model
options[:model] || relation_name.to_s.singularize.camelcase.constantize
(options[:model] || relation_name.to_s.singularize.camelcase).to_s.constantize
end

def foreign_key
Expand Down
2 changes: 1 addition & 1 deletion lib/active_force/association/has_one_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def define_relation_method
_method = @relation_name
@parent.send :define_method, _method do
association_cache.fetch(_method) do
association_cache[_method] = association.relation_model.to_s.constantize.find_by(association.foreign_key => self.id)
association_cache[_method] = association.relation_model.find_by(association.foreign_key => self.id)
end
end

Expand Down
84 changes: 68 additions & 16 deletions spec/active_force/association_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,22 @@
soql = "SELECT Id, PostId, PosterId__c, FancyPostId, Body__c FROM Comment__c WHERE (PostId = '1')"
expect(post.comments.to_s).to eq soql
end

context 'when passing `model` option' do
before do
allow(Comment).to receive(:where).once.and_return([comment])
end

it 'allows passing as a constant' do
Post.has_many :comments, model: Comment
expect { post.comments }.to_not raise_error
end

it 'allows passing as a string' do
Post.has_many :comments, model: 'Comment'
expect { post.comments }.to_not raise_error
end
end
end

describe "has_one_query" do
Expand All @@ -104,14 +120,6 @@
expect(has_one_parent.has_one_child).to be_a HasOneChild
end

it "should allow to pass a foreign key as options" do
HasOneParent.has_one :has_one_child, foreign_key: :fancy_parent_id
allow(has_one_parent).to receive(:id).and_return "2"
expect(client).to receive(:query).with("SELECT Id, has_one_parent_id__c, FancyParentId FROM HasOneChild__c WHERE (FancyParentId = '2') LIMIT 1")
has_one_parent.has_one_child
HasOneParent.has_one :has_one_child, foreign_key: :has_one_parent_id # reset association to original value
end

it 'makes only one API call to fetch the associated object' do
expect(HasOneChild).to receive(:find_by).once.and_return(has_one_child)
has_one_parent.has_one_child.id
Expand Down Expand Up @@ -170,19 +178,37 @@
end
end

describe 'has_one(options)' do
it 'allows passing a foreign key' do
HasOneParent.has_one :has_one_child, foreign_key: :fancy_parent_id
allow(has_one_parent).to receive(:id).and_return "2"
expect(client).to receive(:query).with("SELECT Id, has_one_parent_id__c, FancyParentId FROM HasOneChild__c WHERE (FancyParentId = '2') LIMIT 1")
has_one_parent.has_one_child
HasOneParent.has_one :has_one_child, foreign_key: :has_one_parent_id # reset association to original value
end

context 'when passing `model` option' do
before do
allow(HasOneChild).to receive(:find_by).and_return(has_one_child)
end

it 'allows passing as a constant' do
HasOneParent.has_one :has_one_child, model: HasOneChild
expect { has_one_parent.has_one_child }.to_not raise_error
end

it 'allows passing as a string' do
HasOneParent.has_one :has_one_child, model: 'HasOneChild'
expect { has_one_parent.has_one_child }.to_not raise_error
end
end
end

describe "belongs_to" do
it "should get the resource it belongs to" do
expect(comment.post).to be_instance_of(Post)
end

it "should allow to pass a foreign key as options" do
Comment.belongs_to :post, foreign_key: :fancy_post_id
allow(comment).to receive(:fancy_post_id).and_return "2"
expect(client).to receive(:query).with("SELECT Id, Title__c FROM Post__c WHERE (Id = '2') LIMIT 1")
comment.post
Comment.belongs_to :post # reset association to original value
end

it 'makes only one API call to fetch the associated object' do
expect(client).to receive(:query).once
comment.post
Expand Down Expand Up @@ -237,4 +263,30 @@
end
end
end

describe 'belongs_to(options)' do
it 'allows passing a foreign key' do
Comment.belongs_to :post, foreign_key: :fancy_post_id
allow(comment).to receive(:fancy_post_id).and_return "2"
expect(client).to receive(:query).with("SELECT Id, Title__c FROM Post__c WHERE (Id = '2') LIMIT 1")
comment.post
Comment.belongs_to :post # reset association to original value
end

context 'when passing `model` option' do
before do
allow(Post).to receive(:find).once.and_return(post)
end

it 'allows passing as a constant' do
Comment.belongs_to :post, model: Post
expect { comment.post.id }.to_not raise_error
end

it 'allows passing as a string' do
Comment.belongs_to :post, model: 'Post'
expect { comment.post.id }.to_not raise_error
end
end
end
end

0 comments on commit 9795cd6

Please sign in to comment.