Skip to content

Commit

Permalink
mutate attributes instead of creating new attribute set
Browse files Browse the repository at this point in the history
  • Loading branch information
bfrey08 committed Jan 17, 2024
1 parent 105120c commit 2f64cb8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
15 changes: 6 additions & 9 deletions lib/active_force/sobject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
require 'forwardable'
require 'logger'
require 'restforce'
require 'active_model/uninitialized_sobject.rb'
require 'active_model/uninitialized_value.rb'

module ActiveForce
class RecordInvalid < StandardError;end
Expand Down Expand Up @@ -66,8 +66,8 @@ def self.build mash, association_mapping={}
return unless mash
sobject = new

not_selected_attributes = sobject.class.fields.reject{|key| mash.keys.include?(key)}
sobject.uninitialize_attributes(not_selected_attributes)
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|
Expand Down Expand Up @@ -116,16 +116,13 @@ def create!

def uninitialize_attributes(attrs)
return if attrs.blank?
attr_set = @attributes.map do |key, value|
if attrs.include?(self.mappings.dig(key.name.to_sym))
ActiveModel::Attribute::UninitializedSobject.new(key.name, key.type)
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
attrs = attr_set.instance_variable_get(:@attributes) || {}
new_attr_set = ActiveModel::LazyAttributeSet.new({}, {}, {}, {}, attrs)
self.instance_variable_set(:@attributes, new_attr_set)
end

def create
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module ActiveModel
class Attribute
class UninitializedSobject < Uninitialized # :nodoc:
class UninitializedValue < Uninitialized # :nodoc:

def value
raise ActiveModel::MissingAttributeError, "missing attribute: #{name}"
Expand Down

0 comments on commit 2f64cb8

Please sign in to comment.