module AtomicJson::Validations
Public Instance Methods
validate_attributes!(record, attributes)
click to toggle source
# File lib/atomic_json/validations.rb, line 13 def validate_attributes!(record, attributes) raise TypeError, 'Payload to update must be a hash' unless attributes.is_a?(Hash) attributes.each_key do |key| raise ReadOnlyAttributeError, "#{key} is marked as readonly" if read_only_attribute?(record, key) raise InvalidColumnTypeError, 'ActiveRecord column needs to be of type JSON or JSONB' unless valid_column_type?(record, key) end end
validate_record!(record)
click to toggle source
# File lib/atomic_json/validations.rb, line 8 def validate_record!(record) raise ActiveRecordError, 'cannot update a new record' if record.new_record? raise ActiveRecordError, 'cannot update a destroyed record' if record.destroyed? end
Private Instance Methods
read_only_attribute?(record, key)
click to toggle source
# File lib/atomic_json/validations.rb, line 23 def read_only_attribute?(record, key) record.class.readonly_attributes.include?(key.to_s) end
valid_column_type?(record, key)
click to toggle source
# File lib/atomic_json/validations.rb, line 27 def valid_column_type?(record, key) %i[json jsonb].include?(record.type_for_attribute(key.to_s).type) end