-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Fix AMS::Model accessor vs. attributes mutation #1903
Conversation
This comment notes how to fix. Though I prefer we get rid of this thing |
👍 for fixing by just renaming AMS::Model to AMS::Serializable |
I am not sure how that would fix it 😉 |
hopefully the tests pass and it'll be ready to merge |
darn testing gems not working on jruby |
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.
@NullVoxPopuli I tried to give this an honest review, though this class still seems like worthless cruft to me
@@ -18,5 +18,46 @@ def test_initialization_with_string_keys | |||
|
|||
assert_equal model_instance.read_attribute_for_serialization(:key), value | |||
end | |||
|
|||
def test_attributes_can_be_read_for_serialization |
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.
It looks like this test is about a change via accessor instead of constructor. Maybe the test name could be a little more descriptive in that regard?
klass = Class.new(ActiveModelSerializers::Model) do | ||
attr_accessor :id, :one, :two, :three | ||
end | ||
self.class.const_set(:SomeTestModel, klass) |
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.
Why is this needed?
@@ -30,6 +30,8 @@ def updated_at | |||
end | |||
|
|||
def read_attribute_for_serialization(key) | |||
return send(key) if respond_to?(key) |
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.
Hm, I'm not totally sure this would work. You'd get into this scenario:
class Post < ActiveModelSerializers::Model
attr_accessor :title
end
post = Post.new('title' => 'my title')
post.read_attribute_for_serialization(:title) # => nil
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.
that's true. I think that's just another point for the current AMS::Model being broken. :-(
Do you have a PR for your module idea?
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.
Nope, I was waiting to get approval of the RFC. Because what would that module be? Would it contain the constructor logic (in which case we are in the business of overriding the superclass's constructor which is gross)? And if not, what would it include?
As far as I can tell it would just be alias read_attribute_for_serialization send
, and in that case I'm not sure what the point of it is.
So, TBH AMS::Model is nonsensical to me, which means I don't think I can touch the code or enhance the documentation in a meaningful way. I created the RFC to try and improve that situation, but doesn't look like it is getting any traction.
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.
consider:
|
Closed by #1984 |
So far when I fix it, other tests break, but I think that's because the subclass 'Model' has that crazy method_missing