Skip to content

Commit dc39392

Browse files
authored
Merge pull request #275 from brainspec/ar-type-to-json
Support dumping custom AR type to JSON
2 parents f630487 + e601cde commit dc39392

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

lib/enumerize/activerecord.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ def deserialize(value)
8686
end
8787

8888
alias type_cast_from_database deserialize
89+
90+
def as_json(options = nil)
91+
{attr: @attr.name, subtype: @subtype}.as_json(options)
92+
end
8993
end
9094
end
9195
end

test/activerecord_test.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,11 +507,14 @@ class InterestsRequiredUser < User
507507
end
508508

509509
it 'supports AR types serialization' do
510-
User.delete_all
511-
512510
type = User.type_for_attribute('status')
513511
type.must_be_instance_of Enumerize::ActiveRecordSupport::Type
514512
serialized = type.serialize('blocked')
515513
serialized.must_equal 2
516514
end
515+
516+
it 'has AR type itself JSON serializable' do
517+
type = User.type_for_attribute('status')
518+
type.as_json['attr'].must_equal 'status'
519+
end
517520
end

0 commit comments

Comments
 (0)