Class: SalesforceOrm::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, SqlToSoql
Defined in:
lib/salesforce-orm/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SqlToSoql

#aggregate_function?, #boolean_data_type_conversion, #convert_aliased_fields, #sql_to_soql

Constructor Details

#initialize(klass) ⇒ Base

Returns a new instance of Base.



21
22
23
24
25
26
27
28
29
30
# 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

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



19
20
21
# File 'lib/salesforce-orm/base.rb', line 19

def builder
  @builder
end

#clientObject (readonly)

Returns the value of attribute client.



19
20
21
# File 'lib/salesforce-orm/base.rb', line 19

def client
  @client
end

#klassObject (readonly)

Returns the value of attribute klass.



19
20
21
# File 'lib/salesforce-orm/base.rb', line 19

def klass
  @klass
end

Instance Method Details

#all(*args) ⇒ Object



102
103
104
# File 'lib/salesforce-orm/base.rb', line 102

def all(*args)
  make_query
end

#build(object) ⇒ Object



137
138
139
140
141
# File 'lib/salesforce-orm/base.rb', line 137

def build(object)
  result = klass.new(map_from_keys(object))
  result.original_object = object
  result
end

#create!(attributes) ⇒ Object

create! doesn’t return the SalesForce object back It will return only the object id



34
35
36
37
38
39
40
41
42
# 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) ⇒ Object



55
56
57
# File 'lib/salesforce-orm/base.rb', line 55

def destroy!(object)
  destroy_by_id!(object.id)
end

#destroy_all!(*args) ⇒ Object

Transaction not guaranteed



45
46
47
48
49
# File 'lib/salesforce-orm/base.rb', line 45

def destroy_all!(*args)
  each do |object|
    object.destroy!(*args)
  end
end

#destroy_by_id!(id) ⇒ Object



51
52
53
# File 'lib/salesforce-orm/base.rb', line 51

def destroy_by_id!(id)
  client.destroy(klass.object_name, id)
end

#firstObject



106
107
108
# File 'lib/salesforce-orm/base.rb', line 106

def first
  limit(1).make_query.first
end

#lastObject



110
111
112
# File 'lib/salesforce-orm/base.rb', line 110

def last
  order('created_at DESC').first
end

#make_queryObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/salesforce-orm/base.rb', line 118

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

#select(*args) ⇒ Object

Handling select differently because we select all the fields by default



78
79
80
81
82
83
# File 'lib/salesforce-orm/base.rb', line 78

def select(*args)
  @results = nil
  except(:select)
  @builder = builder.select(*args)
  self
end

#to_soqlObject



114
115
116
# File 'lib/salesforce-orm/base.rb', line 114

def to_soql
  sql_to_soql(builder.to_sql)
end

#update_all!(attributes) ⇒ Object

Transaction not guaranteed



60
61
62
63
64
# 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) ⇒ Object



73
74
75
# 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) ⇒ Object



66
67
68
69
70
71
# 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