Skip to content

Commit

Permalink
MOJ-366 Two updates for added functionality (localytics#20)
Browse files Browse the repository at this point in the history
* MOJ-366 Two updates for added functionality
- Varient & object fields now support store_accessor attribute on models
- Insert attribute stripper now validates the record before stripping the attributes, but then disables validation when doing the initial save so that it doesn't fail validation when required attributes are stripped off.

* MOJ-366 Switched InsertAttributeStripper back to use calling the base function, so that it will always correctly return false or raise the error depending on if save or save! is called.

* MOJ-366 Need to use options, not temp_options here. temp_options doesn't even exist yet.
  • Loading branch information
ACerka-Springbuk authored Jan 9, 2023
1 parent 83586f4 commit e3ec758
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/odbc_adapter/concerns/insert_attribute_stripper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ def save!(**options, &block)
UNSAFE_INSERT_TYPES ||= %i(variant object array)

def save_internal(base_function, **options, &block)
# Unless the validations are turned off or the hash is valid just run the save. This will trigger validation
# errors normally for an invalid record. We then disable validations during the initial save, because we'll
# often be saving a technically invalid record as we've stripped off required elements.
unless options[:validate] == false || valid?
return base_function.call(**options, &block)
end
self.class.transaction do
if new_record?
stripped_attributes = {}
Expand All @@ -31,7 +37,8 @@ def save_internal(base_function, **options, &block)
else
stripped_attributes = {}
end
first_call_result = base_function.call(**options, &block)
temp_options = options.merge(validate: false)
first_call_result = base_function.call(**temp_options, &block)
return false if first_call_result == false
if stripped_attributes.any?
restore_stripped_attributes(stripped_attributes)
Expand Down
4 changes: 4 additions & 0 deletions lib/odbc_adapter/type/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def serialize(value)
def changed_in_place?(raw_old_value, new_value)
deserialize(raw_old_value) != new_value
end

def accessor
ActiveRecord::Store::StringKeyedHashAccessor
end
end
end
end
4 changes: 4 additions & 0 deletions lib/odbc_adapter/type/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def serialize(value)
def changed_in_place?(raw_old_value, new_value)
deserialize(raw_old_value) != new_value
end

def accessor
ActiveRecord::Store::StringKeyedHashAccessor
end
end
end
end

0 comments on commit e3ec758

Please sign in to comment.