Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow model option for associations to be passed as string or constant. #16

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

## Not released

## 0.10.2
* Allow `model` to be passed as a string or a constant (https://github.com/Beyond-Finance/active_force/pull/16)

## 0.10.1
* Added support for `or` and `not` clauses (https://github.com/Beyond-Finance/active_force/pull/13)

## 0.10.0
* Fix `#where` chaining on `ActiveQuery` (https://github.com/Beyond-Finance/active_force/pull/7)
* Add `#find_by!` which raises `ActiveForce::RecordNotFound` if nothing is found. (https://github.com/Beyond-Finance/active_force/pull/8)
* Fix `#includes` to find, build, and set the association. (https://github.com/Beyond-Finance/active_force/pull/12)

## 0.10.1
* Added support for 'or' and 'not' clauses (https://github.com/Beyond-Finance/active_force/pull/13)

## 0.9.1
* Fix invalid error class (https://github.com/Beyond-Finance/active_force/pull/6)

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class Account < ActiveForce::SObject
scoped_as: ->{ where("Discontinued__c > ? OR Discontinued__c = ?", Date.today.strftime("%Y-%m-%d"), nil) }

has_many :today_log_entries,
model: DailyLogEntry,
model: 'DailyLogEntry',
scoped_as: ->{ where(date: Time.now.in_time_zone.strftime("%Y-%m-%d")) }

has_many :labs,
Expand All @@ -133,7 +133,7 @@ end

```ruby
class Car < ActiveForce::SObject
has_one :engine, model: CarEngine
has_one :engine, model: 'CarEngine'
end
```

Expand Down
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