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.



14
15
16
17
18
19
20
21
22
# File 'lib/salesforce-orm/base.rb', line 14

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)
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



12
13
14
# File 'lib/salesforce-orm/base.rb', line 12

def builder
  @builder
end

#clientObject (readonly)

Returns the value of attribute client.



12
13
14
# File 'lib/salesforce-orm/base.rb', line 12

def client
  @client
end

#klassObject (readonly)

Returns the value of attribute klass.



12
13
14
# File 'lib/salesforce-orm/base.rb', line 12

def klass
  @klass
end

Instance Method Details

#all(*args) ⇒ Object



86
87
88
# File 'lib/salesforce-orm/base.rb', line 86

def all(*args)
  make_query
end

#build(object) ⇒ Object



120
121
122
123
124
# File 'lib/salesforce-orm/base.rb', line 120

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



26
27
28
# File 'lib/salesforce-orm/base.rb', line 26

def create!(attributes)
  client.create!(klass.object_name, map_to_keys(attributes))
end

#destroy!(object) ⇒ Object



41
42
43
# File 'lib/salesforce-orm/base.rb', line 41

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

#destroy_all!(*args) ⇒ Object

Transaction not guaranteed



31
32
33
34
35
# File 'lib/salesforce-orm/base.rb', line 31

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

#destroy_by_id!(id) ⇒ Object



37
38
39
# File 'lib/salesforce-orm/base.rb', line 37

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

#firstObject



90
91
92
# File 'lib/salesforce-orm/base.rb', line 90

def first
  limit(1).make_query.first
end

#lastObject



94
95
96
# File 'lib/salesforce-orm/base.rb', line 94

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

#make_queryObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/salesforce-orm/base.rb', line 102

def make_query
  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



64
65
66
67
68
# File 'lib/salesforce-orm/base.rb', line 64

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

#to_soqlObject



98
99
100
# File 'lib/salesforce-orm/base.rb', line 98

def to_soql
  sql_to_soql(builder.to_sql)
end

#update_all!(attributes) ⇒ Object

Transaction not guaranteed



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

def update_all!(attributes)
  each do |object|
    object.update_attributes!(attributes)
  end
end

#update_attributes!(object, attributes) ⇒ Object



59
60
61
# File 'lib/salesforce-orm/base.rb', line 59

def update_attributes!(object, attributes)
  update_by_id!(object.id, attributes)
end

#update_by_id!(id, attributes) ⇒ Object



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

def update_by_id!(id, attributes)
  client.update!(
    klass.object_name,
    map_to_keys(attributes.merge({id: id}))
  )
end