class SalesforceOrm::Base
Attributes
builder[R]
client[R]
klass[R]
Public Class Methods
new(klass)
click to toggle source
# File lib/salesforce-orm/base.rb, line 21 def initialize(klass) unless klass.singleton_class.included_modules.include?(ObjectMaker) raise Error::CommonError, "Salesforce object has to be extended from #{ObjectMaker.name}" end @klass = klass @client = RestforceClient.instance @builder = QueryBuilder.select(klass.field_map.keys) where(RecordTypeManager::FIELD_NAME => klass.record_type_id) if klass.record_type_id end
Public Instance Methods
all(*args)
click to toggle source
# File lib/salesforce-orm/base.rb, line 106 def all(*args) make_query end
build(object)
click to toggle source
# File lib/salesforce-orm/base.rb, line 141 def build(object) result = klass.new(map_from_keys(object)) result.original_object = object result end
create!(attributes)
click to toggle source
create! doesn't return the SalesForce object back It will return only the object id
# File lib/salesforce-orm/base.rb, line 34 def create!(attributes) new_attributes = map_to_keys(attributes) new_attributes = new_attributes.merge( RecordTypeManager::FIELD_NAME => klass.record_type_id ) if klass.record_type_id client.create!(klass.object_name, new_attributes) end
destroy!(object)
click to toggle source
# File lib/salesforce-orm/base.rb, line 55 def destroy!(object) destroy_by_id!(object.id) end
destroy_all!(*args)
click to toggle source
Transaction not guaranteed
# File lib/salesforce-orm/base.rb, line 45 def destroy_all!(*args) each do |object| object.destroy!(*args) end end
destroy_by_id!(id)
click to toggle source
# File lib/salesforce-orm/base.rb, line 51 def destroy_by_id!(id) client.destroy(klass.object_name, id) end
first()
click to toggle source
# File lib/salesforce-orm/base.rb, line 110 def first limit(1).make_query.first end
last()
click to toggle source
# File lib/salesforce-orm/base.rb, line 114 def last order('created_at DESC').first end
make_query()
click to toggle source
# File lib/salesforce-orm/base.rb, line 122 def make_query return @results if @results @results = begin soql = to_soql client.query(to_soql).find_all.map do |object| build(object) end rescue => e # On passing a invalid object id, salesforce throughs an exception # with message starting with INVALID_QUERY_FILTER_OPERATOR, we'll be # considering this as an ObjectNotFound exception if e.message =~ /^INVALID_QUERY_FILTER_OPERATOR/ raise Error::ObjectNotFound else raise end end end
scoped(*args)
click to toggle source
# File lib/salesforce-orm/base.rb, line 101 def scoped(*args) where(nil) self end
select(*args)
click to toggle source
Handling select differently because we select all the fields by default
# File lib/salesforce-orm/base.rb, line 78 def select(*args) @results = nil except(:select) @builder = builder.select(*args) self end
to_soql()
click to toggle source
# File lib/salesforce-orm/base.rb, line 118 def to_soql sql_to_soql(builder.to_sql) end
update_all!(attributes)
click to toggle source
Transaction not guaranteed
# File lib/salesforce-orm/base.rb, line 60 def update_all!(attributes) each do |object| object.update_attributes!(attributes) end end
update_attributes!(object, attributes)
click to toggle source
# File lib/salesforce-orm/base.rb, line 73 def update_attributes!(object, attributes) update_by_id!(object.id, attributes) end
update_by_id!(id, attributes)
click to toggle source
# File lib/salesforce-orm/base.rb, line 66 def update_by_id!(id, attributes) client.update!( klass.object_name, map_to_keys(attributes.merge({id: id})) ) end
Private Instance Methods
cast_to(value:, data_type:)
click to toggle source
# File lib/salesforce-orm/base.rb, line 180 def cast_to(value:, data_type:) case data_type when :integer value.nil? ? nil : value.to_i when :float value.nil? ? nil : value.to_f when :date_time time_parse(value: value) when :date time_parse(value: value).try(:to_date) when :array return [] if value.blank? value.split(';') when :boolean return nil if value.nil? value.to_s.downcase == 'true' else value end end
map_from_keys(attributes)
click to toggle source
# File lib/salesforce-orm/base.rb, line 159 def map_from_keys(attributes) map = klass.field_map data_type_map = klass.data_type_map new_attributes = {} attributes.keys.each do |key| key_sym = key.to_sym new_key = map.key(key_sym) if new_key new_attributes[new_key] = cast_to( value: attributes[key], data_type: data_type_map[new_key] ) else # The feilds which is not in field_map also will get added # to the ObjectBase, to support aggregate queries new_attributes[key] = attributes[key] end end new_attributes end
map_to_keys(attributes)
click to toggle source
# File lib/salesforce-orm/base.rb, line 149 def map_to_keys(attributes) map = klass.field_map new_attributes = {} attributes.keys.each do |key| key_sym = key.to_sym new_attributes[map[key_sym]] = attributes[key] if map[key_sym] end new_attributes end
time_parse(value:)
click to toggle source
# File lib/salesforce-orm/base.rb, line 201 def time_parse(value:) return nil if value.blank? if Time.respond_to?(:zone) && Time.zone Time.zone.parse(value) else Time.parse(value) end end