class ActiveForce::SObject

Public Class Methods

build(mash) click to toggle source
# File lib/active_force/sobject.rb, line 52
def self.build mash
  return unless mash
  sobject = new
  mash.each do |column, sf_value|
    sobject.write_value column, sf_value
  end
  sobject.clear_changes_information
  sobject
end
create(args) click to toggle source
# File lib/active_force/sobject.rb, line 106
def self.create args
  new(args).create
end
create!(args) click to toggle source
# File lib/active_force/sobject.rb, line 110
def self.create! args
  new(args).create!
end
field(field_name, args = {}) click to toggle source
# File lib/active_force/sobject.rb, line 138
def self.field field_name, args = {}
  mapping.field field_name, args
  attribute field_name
end
fields() click to toggle source
# File lib/active_force/sobject.rb, line 44
def self.fields
  mapping.sfdc_names
end
mapping() click to toggle source
# File lib/active_force/sobject.rb, line 40
def self.mapping
  @mapping ||= ActiveForce::Mapping.new name
end
query() click to toggle source
# File lib/active_force/sobject.rb, line 48
def self.query
  ActiveForce::ActiveQuery.new self
end

Private Class Methods

inherited(subclass) click to toggle source

Provide each subclass with a default id field. Can be overridden in the subclass if needed

# File lib/active_force/sobject.rb, line 35
def inherited(subclass)
  subclass.field :id, from: 'Id'
end
picklist(field) click to toggle source
# File lib/active_force/sobject.rb, line 194
def self.picklist field
  picks = sfdc_client.picklist_values(table_name, mappings[field])
  picks.map do |value|
    [value[:label], value[:value]]
  end
end
sfdc_client() click to toggle source
# File lib/active_force/sobject.rb, line 201
def self.sfdc_client
  ActiveForce.sfdc_client
end

Public Instance Methods

create() click to toggle source
# File lib/active_force/sobject.rb, line 95
def create
  create!
rescue Faraday::ClientError, RecordInvalid => error
  handle_save_error error
  self
end
create!() click to toggle source
# File lib/active_force/sobject.rb, line 84
def create!
  validate!
  run_callbacks :save do
    run_callbacks :create do
      self.id = sfdc_client.create! table_name, attributes_for_sfdb
      changes_applied
    end
  end
  self
end
destroy() click to toggle source
# File lib/active_force/sobject.rb, line 102
def destroy
  sfdc_client.destroy! self.class.table_name, id
end
persisted?() click to toggle source
# File lib/active_force/sobject.rb, line 134
def persisted?
  !!id
end
reload() click to toggle source
# File lib/active_force/sobject.rb, line 143
def reload
  association_cache.clear
  reloaded = self.class.find(id)
  self.attributes = reloaded.attributes
  clear_changes_information
  self
end
save() click to toggle source
# File lib/active_force/sobject.rb, line 124
def save
  save!
rescue Faraday::ClientError, RecordInvalid => error
  handle_save_error error
end
save!() click to toggle source
# File lib/active_force/sobject.rb, line 114
def save!
  run_callbacks :save do
    if persisted?
      !!update!
    else
      !!create!
    end
  end
end
to_param() click to toggle source
# File lib/active_force/sobject.rb, line 130
def to_param
  id
end
update(attributes = {})
Alias for: update_attributes
update!(attributes = {})
Alias for: update_attributes!
update_attributes(attributes = {}) click to toggle source
# File lib/active_force/sobject.rb, line 76
def update_attributes attributes = {}
  update_attributes! attributes
rescue Faraday::ClientError, RecordInvalid => error
  handle_save_error error
end
Also aliased as: update
update_attributes!(attributes = {}) click to toggle source
# File lib/active_force/sobject.rb, line 62
def update_attributes! attributes = {}
  assign_attributes attributes
  validate!
  run_callbacks :save do
    run_callbacks :update do
      sfdc_client.update! table_name, attributes_for_sfdb
      changes_applied
    end
  end
  true
end
Also aliased as: update!
write_value(column, value) click to toggle source
# File lib/active_force/sobject.rb, line 151
def write_value column, value
  if association = self.class.find_association(column)
    field = association.relation_name
    value = Association::RelationModelBuilder.build(association, value)
  else
    field = mappings.invert[column]
    value = self.class.mapping.translate_value value, field unless value.nil?
  end
  send "#{field}=", value if field
end

Private Instance Methods

association_cache() click to toggle source
# File lib/active_force/sobject.rb, line 177
def association_cache
  @association_cache ||= {}
end
attributes_for_sfdb() click to toggle source
# File lib/active_force/sobject.rb, line 188
def attributes_for_sfdb
  attrs = self.class.mapping.translate_to_sf(attributes_and_changes)
  attrs.merge!({'Id' => id }) if persisted?
  attrs
end
handle_save_error(error) click to toggle source
# File lib/active_force/sobject.rb, line 172
def handle_save_error error
  return false if error.class == RecordInvalid
  logger_output __method__, error, attributes
end
logger_output(action, exception, params = {}) click to toggle source
# File lib/active_force/sobject.rb, line 181
def logger_output action, exception, params = {}
  logger = Logger.new($stdout)
  logger.info("[SFDC] [#{self.class.model_name}] [#{self.class.table_name}] Error while #{ action }, params: #{params}, error: #{exception.inspect}")
  errors[:base] << exception.message
  false
end
sfdc_client() click to toggle source
# File lib/active_force/sobject.rb, line 205
def sfdc_client
  self.class.sfdc_client
end
validate!() click to toggle source
# File lib/active_force/sobject.rb, line 164
def validate!
  unless valid?
    raise RecordInvalid.new(
      "Validation failed: #{errors.full_messages.join(', ')}"
    )
  end
end