Skip to content

Commit

Permalink
Merge pull request #1219 from 3scale/fix-path-oracle
Browse files Browse the repository at this point in the history
Cast nil values to empty string
  • Loading branch information
Martouta committed Sep 21, 2019
2 parents 4c3d753 + e358758 commit 7131eb1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
12 changes: 12 additions & 0 deletions app/lib/active_record/type/string_not_nil.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module ActiveRecord
module Type
class StringNotNil < ActiveRecord::Type::String
def type_cast(value)
return '' if value.blank?
super
end
end
end
end
2 changes: 1 addition & 1 deletion app/models/backend_api_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class BackendApiConfig < ApplicationRecord
belongs_to :service, inverse_of: :backend_api_configs
belongs_to :backend_api, inverse_of: :backend_api_configs

attribute :path, Type::String.new
attribute :path, ActiveRecord::Type::StringNotNil.new

has_many :backend_api_metrics, through: :backend_api, source: :metrics

Expand Down
12 changes: 12 additions & 0 deletions test/unit/lib/type_string_not_nil_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require 'test_helper'

class TypeStringNotNilTest < ActiveSupport::TestCase
def test_type_cast_to_string_if_nil
type = ActiveRecord::Type::StringNotNil.new
assert_equal '', type.type_cast_from_database(nil)
assert_equal '', type.type_cast_from_user(nil)
assert_equal '', type.type_cast(nil)
end
end

0 comments on commit 7131eb1

Please sign in to comment.