Skip to content

Commit

Permalink
Merge pull request #41302 from kamipo/should_not_require_connection
Browse files Browse the repository at this point in the history
`attribute` should not require a connection is established
  • Loading branch information
kamipo committed Feb 2, 2021
2 parents 24416e5 + 6d291aa commit beed933
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
3 changes: 1 addition & 2 deletions activerecord/lib/active_record/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ def default_value # :nodoc:
end

def adapter_name_from(model) # :nodoc:
# TODO: this shouldn't depend on a connection to the database
model.connection.adapter_name.downcase.to_sym
model.connection_db_config.adapter.to_sym
end

private
Expand Down
8 changes: 8 additions & 0 deletions activerecord/test/cases/attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,14 @@ def deserialize(*)
assert_equal "foo", klass.new(no_type: "foo").no_type
end

test "attributes do not require a connection is established" do
assert_not_called(ActiveRecord::Base, :connection) do
Class.new(OverloadedType) do
attribute :foo, :string
end
end
end

test "unknown type error is raised" do
assert_raise(ArgumentError) do
OverloadedType.attribute :foo, :unknown
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/type_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class TypeTest < ActiveRecord::TestCase
end

test "lookup defaults to the current adapter" do
current_adapter = ActiveRecord::Base.connection.adapter_name.downcase.to_sym
current_adapter = ActiveRecord::Type.adapter_name_from(ActiveRecord::Base)
type = Struct.new(:args)
adapter_type = Struct.new(:args)
ActiveRecord::Type.register(:foo, type, override: false)
Expand Down

0 comments on commit beed933

Please sign in to comment.