-
Notifications
You must be signed in to change notification settings - Fork 88
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
Add to_h and to_json to Granite::ORM::Fields #72
Add to_h and to_json to Granite::ORM::Fields #72
Conversation
Let me know if you guys see any areas to improve the I also didn't want to cast all the values to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to me. What do you think @drujensen
spec/granite_orm_spec.cr
Outdated
|
||
it "should provide a to_h method" do | ||
t = Todo.new(name: "test todo", priority: 20) | ||
result = { "name" => "test todo", "priority": 20 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is interesting:
{ "name" => "test todo", "priority": 20 }.class # => Hash(String, Int32 | String)
{ "priority": 20 }.class # => NamedTuple(priority: Int32)
{ "priority": 20, "name" => "test todo" }.class # space not allowed between named argument name and ':'
I don't want to be pedantic, but the first case looks like a bug in Crystal or at least it is not documented. Personally, I would prefer to use here official way for hashes to avoid problems in future:
{ "name" => "test todo", "priority" => 20 }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@veelenga My ruby background seems to have seeped in here pushing a fix shortly.
src/granite_orm/fields.cr
Outdated
|
||
if updated_at | ||
fields["updated_at"] = updated_at.not_nil!.to_s("%F %X") | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All fields with nil
values will be present in a hash, but created_at
and updated_at
will not. In this intended?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@veelenga Good point I'll allow the nil values
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm with @veelenga requested changes. Thanks for explaining why we didn't use DB::ANY.
spec/granite_orm_spec.cr
Outdated
|
||
it "should provide a to_json method" do | ||
t = Todo.new(name: "test todo", priority: 20) | ||
result = "{\"name\":\"test todo\",\"priority\":20}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about %({"name":"test todo","priority":20})
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this better than escaping all the quotes
I've made the changes on my branch in migeorge/granite-orm but they don't seem to be propagating here. Anyone experienced this before? |
src/granite_orm/fields.cr
Outdated
{% end %} | ||
{% if SETTINGS[:timestamps] %} | ||
fields["created_at"] = created_at ? created_at.not_nil!.to_s("%F %X") : "" | ||
fields["updated_at"] = updated_at ? updated_at.not_nil!.to_s("%F %X") : "" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
but why ""
instead of nil
?
fields["created_at"] = created_at.try &.to_s("%F %X")
fields["updated_at"] = updated_at.try &.to_s("%F %X")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@veelenga That makes more sense, fixed.
This should be ready to go unless anyone has more feedback |
This PR should satisfy the agreed upon resolution of #50
It adds a
to_h
andto_json
method to Granite ORM Models.