Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support JSON serialize #196

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/active_hash/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def normalize(v)
if Object.const_defined?(:ActiveModel)
extend ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::Serializers::JSON
else
def to_param
id.present? ? id.to_s : nil
Expand Down
12 changes: 12 additions & 0 deletions spec/active_hash/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1531,4 +1531,16 @@ class Book < ActiveRecord::Base
end
end

describe "#as_json" do
before do
Country.data = [
{:id => 1, :name => "US", :language => 'English'}
]
end

it "returns a hash" do
country = Country.find(1)
expect(country.as_json.stringify_keys).to eq({"id" => 1, "name" => "US", "language" => "English"})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rails 6.0 or later returns stringified keys hash, but Rails 5.2 or earlier returns symbolized keys hash. So I used stringify_keys method.

end
end
end