Class: ActiveForce::SObject

Inherits:
Object
  • Object
show all
Extended by:
Association, Bulk, ActiveModel::Callbacks, Forwardable
Includes:
ActiveModel::API, ActiveModel::AttributeMethods, ActiveModel::Attributes, ActiveModel::Dirty, ActiveModel::Model, ActiveModel::Serializers::JSON
Defined in:
lib/active_force/sobject.rb

Constant Summary

Constants included from Bulk

Bulk::TIMEOUT_MESSAGE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Association

associations, belongs_to, find_association, has_many, has_one

Methods included from Bulk

bulk_delete_all, bulk_insert_all, bulk_update_all

Instance Attribute Details

#build_attributesObject

Returns the value of attribute build_attributes.



89
90
91
# File 'lib/active_force/sobject.rb', line 89

def build_attributes
  @build_attributes
end

#idObject

Returns the value of attribute id.



31
32
33
# File 'lib/active_force/sobject.rb', line 31

def id
  @id
end

#titleObject

Returns the value of attribute title.



31
32
33
# File 'lib/active_force/sobject.rb', line 31

def title
  @title
end

Class Method Details

.build(mash, association_mapping = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/active_force/sobject.rb', line 90

def self.build mash, association_mapping={}
  return unless mash
  sobject = new

  attributes_not_selected = sobject.class.fields.reject{|key| mash.keys.include?(key)}
  sobject.uninitialize_attributes(attributes_not_selected)
  sobject.build_attributes = mash[:build_attributes] || mash
  sobject.run_callbacks(:build) do
    mash.each do |column, value|
      if association_mapping.has_key?(column.downcase)
        column = association_mapping[column.downcase]
      end
      sobject.write_value column, value, association_mapping
    end
  end
  sobject.clear_changes_information
  sobject
end

.create(args) ⇒ Object



166
167
168
# File 'lib/active_force/sobject.rb', line 166

def self.create args
  new(args).create
end

.create!(args) ⇒ Object



170
171
172
# File 'lib/active_force/sobject.rb', line 170

def self.create! args
  new(args).create!
end

.describeObject



85
86
87
# File 'lib/active_force/sobject.rb', line 85

def self.describe
  sfdc_client.describe(table_name)
end

.field(field_name, args = {}) ⇒ Object



198
199
200
201
202
203
204
# File 'lib/active_force/sobject.rb', line 198

def self.field field_name, args = {}
  options = args.except(:as, :from, :sfdc_name)
  mapping.field field_name, args
  cast_type = args.fetch(:as, :string)
  attribute field_name, cast_type, **options
  define_attribute_methods field_name
end

.fieldsObject



77
78
79
# File 'lib/active_force/sobject.rb', line 77

def self.fields
  mapping.sfdc_names
end

.mappingObject



69
70
71
# File 'lib/active_force/sobject.rb', line 69

def self.mapping
  @mapping ||= ActiveForce::Mapping.new name
end

.queryObject



81
82
83
# File 'lib/active_force/sobject.rb', line 81

def self.query
  ActiveForce::ActiveQuery.new self
end

.table_nameObject



47
48
49
# File 'lib/active_force/sobject.rb', line 47

def table_name
  table_name_store || mapping.table_name
end

.update(id, attributes) ⇒ Object



39
40
41
# File 'lib/active_force/sobject.rb', line 39

def update(id, attributes)
  prepare_for_update(id, attributes).update
end

.update!(id, attributes) ⇒ Object



43
44
45
# File 'lib/active_force/sobject.rb', line 43

def update!(id, attributes)
  prepare_for_update(id, attributes).update!
end

Instance Method Details

#[](name) ⇒ Object



226
227
228
# File 'lib/active_force/sobject.rb', line 226

def [](name)
  send(name.to_sym)
end

#[]=(name, value) ⇒ Object



230
231
232
# File 'lib/active_force/sobject.rb', line 230

def []=(name,value)
  send("#{name.to_sym}=", value)
end

#createObject



153
154
155
156
157
158
# File 'lib/active_force/sobject.rb', line 153

def create
  create!
rescue Faraday::ClientError, RecordInvalid => error
  handle_save_error error
  self
end

#create!Object



131
132
133
134
135
136
137
138
139
140
# File 'lib/active_force/sobject.rb', line 131

def create!
  validate!
  run_callbacks :save do
    run_callbacks :create do
      self.id = sfdc_client.create! table_name, attributes_for_sfdb
      clear_changes_information
    end
  end
  self
end

#destroyObject



160
161
162
163
164
# File 'lib/active_force/sobject.rb', line 160

def destroy
  run_callbacks(:destroy) do
    sfdc_client.destroy! self.class.table_name, id
  end
end

#modified_attributesObject



206
207
208
# File 'lib/active_force/sobject.rb', line 206

def modified_attributes
  attributes.select{ |attr, key| changed.include? attr.to_s }
end

#persisted?Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/active_force/sobject.rb', line 194

def persisted?
  !!id
end

#reloadObject



210
211
212
213
214
215
216
# File 'lib/active_force/sobject.rb', line 210

def reload
  association_cache.clear
  reloaded = self.class.find(id)
  self.attributes = reloaded.attributes
  clear_changes_information
  self
end

#saveObject



184
185
186
187
188
# File 'lib/active_force/sobject.rb', line 184

def save
  save!
rescue Faraday::ClientError, RecordInvalid => error
  handle_save_error error
end

#save!Object



174
175
176
177
178
179
180
181
182
# File 'lib/active_force/sobject.rb', line 174

def save!
  run_callbacks :save do
    if persisted?
      !!update!
    else
      !!create!
    end
  end
end

#table_nameObject



73
74
75
# File 'lib/active_force/sobject.rb', line 73

def table_name
  table_name_store || self.class.mapping.table_name
end

#to_paramObject



190
191
192
# File 'lib/active_force/sobject.rb', line 190

def to_param
  id
end

#uninitialize_attributes(attrs) ⇒ Object



142
143
144
145
146
147
148
149
150
151
# File 'lib/active_force/sobject.rb', line 142

def uninitialize_attributes(attrs)
  return if attrs.blank?
  self.instance_variable_get(:@attributes).instance_variable_get(:@attributes).each do |key, value|
    if attrs.include?(self.mappings.dig(value.name.to_sym))
      self.instance_variable_get(:@attributes).instance_variable_get(:@attributes)[key] = ActiveModel::Attribute::UninitializedValue.new(value.name, value.type)
    else
      key
    end
  end
end

#update_attributes(attributes = {}) ⇒ Object Also known as: update



123
124
125
126
127
# File 'lib/active_force/sobject.rb', line 123

def update_attributes attributes = {}
  update_attributes! attributes
rescue Faraday::ClientError, RecordInvalid => error
  handle_save_error error
end

#update_attributes!(attributes = {}) ⇒ Object Also known as: update!



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/active_force/sobject.rb', line 109

def update_attributes! attributes = {}
  assign_attributes attributes
  validate!
  run_callbacks :save do
    run_callbacks :update do
      sfdc_client.update! table_name, attributes_for_sfdb
      clear_changes_information
    end
  end
  true
end

#write_value(key, value, association_mapping = {}) ⇒ Object



218
219
220
221
222
223
224
# File 'lib/active_force/sobject.rb', line 218

def write_value(key, value, association_mapping = {})
  if (association = self.class.find_association(key.to_sym))
    write_association_value(association, value, association_mapping)
  else
    write_field_value(key, value)
  end
end