-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add record type to object base * Fix cache key uniqness issue
- Loading branch information
1 parent
d5bcff6
commit 0881f3f
Showing
14 changed files
with
276 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Dir[File.expand_path('object/*.rb', File.dirname(__FILE__))].each do |file| | ||
require file | ||
end | ||
|
||
module SalesforceOrm | ||
module Object | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require_relative '../object_base' | ||
|
||
module SalesforceOrm | ||
module Object | ||
class RecordType < ObjectBase | ||
|
||
self.field_map = { | ||
name: :Name, | ||
sobject_type: :SobjectType, | ||
developer_name: :DeveloperName, | ||
} | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
module SalesforceOrm | ||
module RecordTypeManager | ||
|
||
FIELD_NAME = :RecordTypeId | ||
|
||
def record_type=(new_record_type) | ||
@record_type = new_record_type | ||
end | ||
|
||
def record_type | ||
@record_type | ||
end | ||
|
||
def record_type_id | ||
return nil unless record_type | ||
return @record_type_id if @record_type_id | ||
|
||
record_type_sobject = if defined?(Rails) | ||
if Rails.env.test? | ||
Object::RecordType.build(id: 'fake_record_type') | ||
elsif !Rails.env.development? | ||
Rails.cache.fetch(record_type_cache_key) do | ||
fetch_record_type | ||
end | ||
else | ||
fetch_record_type | ||
end | ||
else | ||
fetch_record_type | ||
end | ||
|
||
raise Error::RecordTypeNotFound unless record_type_sobject | ||
|
||
@record_type_id = record_type_sobject.id | ||
end | ||
|
||
private | ||
|
||
def fetch_record_type | ||
Object::RecordType.where(developer_name: record_type).first | ||
end | ||
|
||
def record_type_cache_key | ||
['RecordTypeManager', object_name, record_type].join('/') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module SalesforceOrm | ||
VERSION = '1.0.0'.freeze | ||
VERSION = '1.1.0'.freeze | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
require 'spec_helper' | ||
require_relative 'fixtures/sample_object' | ||
|
||
RSpec.describe SalesforceOrm::RecordTypeManager do | ||
|
||
it 'should have a constant call FIELD_NAME' do | ||
expect(SalesforceOrm::RecordTypeManager::FIELD_NAME).to eq(:RecordTypeId) | ||
end | ||
|
||
describe 'record_type_id' do | ||
|
||
let(:klass) do | ||
Class.new SampleObject | ||
end | ||
|
||
before(:each) do | ||
klass.record_type = :yo | ||
end | ||
|
||
describe 'Ruby ENV' do | ||
|
||
it 'should give nil record_type_id if there is not recordn type set' do | ||
klass.record_type = nil | ||
expect(klass.record_type_id).to be_nil | ||
end | ||
|
||
|
||
it 'should call fetch_record_type when first time' do | ||
id = 'bhla' | ||
expect(SampleObject).to receive(:fetch_record_type).and_return( | ||
SalesforceOrm::Object::RecordType.build({ | ||
id: id | ||
}) | ||
) | ||
expect(klass.record_type_id).to eq(id) | ||
end | ||
|
||
it 'should not call fetch_record_type if already fetched' do | ||
id = 'bhla' | ||
expect(klass).to receive(:fetch_record_type).and_return( | ||
SalesforceOrm::Object::RecordType.build({ | ||
id: id | ||
}) | ||
) | ||
expect(klass.record_type_id).to eq(id) | ||
|
||
expect(klass).not_to receive(:fetch_record_type) | ||
|
||
expect(klass.record_type_id).to eq(id) | ||
end | ||
end | ||
|
||
|
||
describe 'Rails ENV' do | ||
|
||
before(:all) do | ||
class TestEnv | ||
|
||
def env=(env) | ||
@env = env | ||
end | ||
|
||
def method_missing(method_name, *args, &block) | ||
method_name[0..-2] == @env | ||
end | ||
end | ||
|
||
class TestCache | ||
|
||
def self.fetch(*args) | ||
yield | ||
end | ||
|
||
end | ||
|
||
class ::Rails | ||
|
||
class << self | ||
def env | ||
@env ||= TestEnv.new | ||
end | ||
|
||
def env_name=(env_name) | ||
env.env = env_name | ||
end | ||
|
||
def cache | ||
TestCache | ||
end | ||
end | ||
end | ||
end | ||
|
||
it 'should return fake_record_type for test env' do | ||
Rails.env_name = 'test' | ||
|
||
expect(klass.record_type_id).to eq('fake_record_type') | ||
end | ||
|
||
it 'should call fetch_record_type in development env' do | ||
Rails.env_name = 'development' | ||
|
||
id = 'bhla' | ||
expect(klass).to receive(:fetch_record_type).and_return( | ||
SalesforceOrm::Object::RecordType.build({ | ||
id: id | ||
}) | ||
) | ||
expect(klass.record_type_id).to eq(id) | ||
end | ||
|
||
it 'should call Rails.cache evns expcept test and development' do | ||
Rails.env_name = 'production' | ||
|
||
klass.object_name = 'SampleObject' | ||
|
||
expect(Rails).to receive(:cache).and_return(TestCache) | ||
|
||
id = 'bhla' | ||
expect(klass).to receive(:fetch_record_type).and_return( | ||
SalesforceOrm::Object::RecordType.build({ | ||
id: id | ||
}) | ||
) | ||
|
||
expect(klass.record_type_id).to eq('bhla') | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.