class ActiveForce::SObject
Attributes
build_attributes[RW]
id[RW]
title[RW]
Public Class Methods
build(mash, association_mapping={})
click to toggle source
# File lib/active_force/sobject.rb, line 81 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)
click to toggle source
# File lib/active_force/sobject.rb, line 157 def self.create args new(args).create end
create!(args)
click to toggle source
# File lib/active_force/sobject.rb, line 161 def self.create! args new(args).create! end
describe()
click to toggle source
# File lib/active_force/sobject.rb, line 76 def self.describe sfdc_client.describe(table_name) end
field(field_name, args = {})
click to toggle source
# File lib/active_force/sobject.rb, line 189 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
fields()
click to toggle source
# File lib/active_force/sobject.rb, line 68 def self.fields mapping.sfdc_names end
mapping()
click to toggle source
# File lib/active_force/sobject.rb, line 64 def self.mapping @mapping ||= ActiveForce::Mapping.new name end
picklist(field)
click to toggle source
# File lib/active_force/sobject.rb, line 285 def self.picklist field picks = sfdc_client.picklist_values(table_name, mappings[field]) picks.map do |value| [value[:label], value[:value]] end end
query()
click to toggle source
# File lib/active_force/sobject.rb, line 72 def self.query ActiveForce::ActiveQuery.new self end
sfdc_client()
click to toggle source
# File lib/active_force/sobject.rb, line 292 def self.sfdc_client ActiveForce.sfdc_client end
update(id, attributes)
click to toggle source
# File lib/active_force/sobject.rb, line 38 def update(id, attributes) prepare_for_update(id, attributes).update end
update!(id, attributes)
click to toggle source
# File lib/active_force/sobject.rb, line 42 def update!(id, attributes) prepare_for_update(id, attributes).update! 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 59 def inherited(subclass) subclass.field :id, from: 'Id' end
prepare_for_update(id, attributes)
click to toggle source
# File lib/active_force/sobject.rb, line 48 def prepare_for_update(id, attributes) new(attributes.merge(id: id)).tap do |obj| attributes.each do |name, value| obj.public_send("#{name}_will_change!") if value.nil? end end end
Public Instance Methods
[](name)
click to toggle source
# File lib/active_force/sobject.rb, line 217 def [](name) send(name.to_sym) end
[]=(name,value)
click to toggle source
# File lib/active_force/sobject.rb, line 221 def []=(name,value) send("#{name.to_sym}=", value) end
create()
click to toggle source
# File lib/active_force/sobject.rb, line 144 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 122 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
destroy()
click to toggle source
# File lib/active_force/sobject.rb, line 151 def destroy run_callbacks(:destroy) do sfdc_client.destroy! self.class.table_name, id end end
modified_attributes()
click to toggle source
# File lib/active_force/sobject.rb, line 197 def modified_attributes attributes.select{ |attr, key| changed.include? attr.to_s } end
persisted?()
click to toggle source
# File lib/active_force/sobject.rb, line 185 def persisted? !!id end
reload()
click to toggle source
# File lib/active_force/sobject.rb, line 201 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 175 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 165 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 181 def to_param id end
uninitialize_attributes(attrs)
click to toggle source
# File lib/active_force/sobject.rb, line 133 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 = {})
click to toggle source
# File lib/active_force/sobject.rb, line 114 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 100 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
Also aliased as: update!
write_value(key, value, association_mapping = {})
click to toggle source
# File lib/active_force/sobject.rb, line 209 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
Private Instance Methods
association_cache()
click to toggle source
# File lib/active_force/sobject.rb, line 255 def association_cache @association_cache ||= {} end
attributes_for_create()
click to toggle source
# File lib/active_force/sobject.rb, line 271 def attributes_for_create default_attributes.concat(changed) end
attributes_for_sfdb()
click to toggle source
# File lib/active_force/sobject.rb, line 266 def attributes_for_sfdb attrs_to_change = persisted? ? attributes_for_update : attributes_for_create self.class.mapping.translate_to_sf(@attributes.values_for_database.slice(*attrs_to_change)) end
attributes_for_update()
click to toggle source
# File lib/active_force/sobject.rb, line 281 def attributes_for_update ['id'].concat(changed) end
default_attributes()
click to toggle source
# File lib/active_force/sobject.rb, line 275 def default_attributes @attributes.each_value.select do |value| value.is_a?(ActiveModel::Attribute::UserProvidedDefault) || value.instance_values["original_attribute"].is_a?(ActiveModel::Attribute::UserProvidedDefault) end.map(&:name) end
handle_save_error(error)
click to toggle source
# File lib/active_force/sobject.rb, line 250 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 259 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.add(:base, exception.message) false end
sfdc_client()
click to toggle source
# File lib/active_force/sobject.rb, line 296 def sfdc_client self.class.sfdc_client end
validate!()
click to toggle source
# File lib/active_force/sobject.rb, line 227 def validate! unless valid? raise RecordInvalid.new( "Validation failed: #{errors.full_messages.join(', ')}" ) end end
write_association_value(association, value, association_mapping)
click to toggle source
# File lib/active_force/sobject.rb, line 235 def write_association_value(association, value, association_mapping) association_cache[association.relation_name] = Association::RelationModelBuilder.build(association, value, association_mapping) end
write_field_value(field_key, value)
click to toggle source
# File lib/active_force/sobject.rb, line 240 def write_field_value(field_key, value) field = if mappings.key?(field_key.to_sym) field_key else mappings.key(field_key) end send("#{field}=", value) if field && respond_to?(field) end