Sentinel is a SalesForce integration gem. This gem allows you to do simple data access operations, like find/create/update.
Add this line to your application's Gemfile:
gem 'sentinel', :git => '[email protected]:mandrakez/sentinel.git'
Then bundle install
.
It depends on an OAuth2 integration, before you can execute any Sentinel operation, said that, we need to configure our OAuth credentials:
Example:
Sentinel.configure do |c|
c.oauth_token = session['oauth_token']
c.instance_url = session['instance_url']
end
I suggest you to use omniauth-salesforce to do that. I will not enter on details here, because you can find more information here.
This is a generic gem, so you can improve and use other entities provided by SalesForce.
Example:
class Contact
include ActiveModel::Model
include ActiveModel::Validations
include Sentinel::Model
# SalesForce entity, will use class name if absent
set_sentinel_table 'Contact'
# Entity field names
# 'alias' will generate getters/setters with specific name
field :FirstName, alias: :first_name
field :LastName, alias: :last_name
field :Email, alias: :email
field :Phone, alias: :telephone
class << self
def last_contacts
query("SELECT Id, FirstName, LastName, Email, Phone FROM Contact ORDER BY CreatedDate DESC LIMIT 100")
end
end
def full_name
"#{first_name} #{last_name}"
end
def persisted?
!self.id.to_s.empty?
end
end
> contact = Contact.find('1')
> puts contact.email
=> "[email protected]"
or more complex filtering
> contacts = Contact.query('SELECT Id, FirstName, LastName, Email, Phone FROM Contact LIMIT 10')
> contact = Contact.new
> contact.name = "John Doe"
> contact.email = '[email protected]'
> contact.save
=> true
or
> contact = Contact.new(name: 'John', email: '[email protected]')
> contact.save
=> true
> contact = Contact.find('1')
> contact.email = '[email protected]'
> contact.save
=> true
or
> contact = Contact.find('1')
> contact.update_attributes(email: '[email protected]')
=> true
> Contact.destroy('1')
=> true
There is still so much work to do:
- primary key probably should be a part of the model structure
- avoid primary key updates
- add new options to 'field' attributes:
- read only flags
- primary key
- understand better SalesForce exceptions
- increase test coverage
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request