Skip to content

Commit df45087

Browse files
committed
[fixes #25] dont crash for database types without casting rules
1 parent 5b820e1 commit df45087

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/active_type/type_caster.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ def initialize(type, connection)
7171
# supports it.
7272
if !type.nil? && connection.respond_to?(:native_database_types)
7373
native_type = connection.native_database_types[type.to_sym]
74-
type = native_type.fetch(:name) unless native_type.nil?
74+
if native_type && native_type[:name]
75+
type = native_type[:name]
76+
else
77+
# unknown type, we just dont cast
78+
type = nil
79+
end
7580
end
7681
@active_record_type = connection.lookup_cast_type(type)
7782
end

spec/active_type/object_spec.rb

+11
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ class ObjectWithBelongsTo < Object
7979

8080
end
8181

82+
class ObjectWithUnsupportedTypes < Object
83+
attribute :virtual_array, :array
84+
attribute :virtual_hash, :hash
85+
end
8286

8387
end
8488

@@ -102,6 +106,13 @@ class ObjectWithBelongsTo < Object
102106
it_should_behave_like 'ActiveRecord-like accessors', { :virtual_string => "string", :virtual_integer => 100, :virtual_time => Time.now, :virtual_date => Date.today, :virtual_boolean => true }
103107
end
104108

109+
describe 'unsupported types' do
110+
subject { ObjectSpec::ObjectWithUnsupportedTypes.new }
111+
112+
it_should_behave_like 'ActiveRecord-like mass assignment', { :virtual_hash => {'foo' => 'bar'}, :virtual_array => ['foo', 'bar'] }
113+
it_should_behave_like 'ActiveRecord-like accessors', { :virtual_hash => {'foo' => 'bar'}, :virtual_array => ['foo', 'bar'] }
114+
end
115+
105116
describe 'overridable attributes' do
106117
subject { ObjectSpec::ObjectWithOverrides.new }
107118

0 commit comments

Comments
 (0)