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

Fix AMS::Model accessor vs. attributes mutation #1903

Closed
wants to merge 3 commits into from

Conversation

bf4
Copy link
Member

@bf4 bf4 commented Sep 1, 2016

So far when I fix it, other tests break, but I think that's because the subclass 'Model' has that crazy method_missing

@mention-bot
Copy link

@bf4, thanks for your PR! By analyzing the annotation information on this pull request, we identified @dubadub and @yevhene to be potential reviewers

@richmolj
Copy link
Contributor

richmolj commented Sep 5, 2016

This comment notes how to fix. Though I prefer we get rid of this thing

@NullVoxPopuli
Copy link
Contributor

👍 for fixing by just renaming AMS::Model to AMS::Serializable

@richmolj
Copy link
Contributor

richmolj commented Sep 7, 2016

I am not sure how that would fix it 😉

@NullVoxPopuli
Copy link
Contributor

@bf4, @richmolj I fixed the tests.

Had to add send if respond_to?

and removed the attributes comparison, because it doesn't make sense when using attr_accessor (which is what the send covers)

@NullVoxPopuli
Copy link
Contributor

hopefully the tests pass and it'll be ready to merge

@NullVoxPopuli
Copy link
Contributor

darn testing gems not working on jruby

Copy link
Contributor

@richmolj richmolj left a comment

Choose a reason for hiding this comment

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

@@ -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
Copy link
Contributor

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)
Copy link
Contributor

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)
Copy link
Contributor

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

Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW you can fix the current class with something like this and this. That got the tests passing, including the scenario above, though it's just POC code.

@bf4
Copy link
Member Author

bf4 commented Nov 2, 2016

consider:

    class_attribute :attribute_names
    self.attribute_names = []

    def self.attributes(*names)
      attr_accessor(*names)
      self.attribute_names += names
    end

    def attributes
      attribute_names.each_with_object({}) {|field, result|
        result[field] = public_send(field)
      }
    end

@bf4
Copy link
Member Author

bf4 commented Dec 6, 2016

Closed by #1984

@bf4 bf4 closed this Dec 6, 2016
@bf4 bf4 deleted the fix_ams_model branch December 6, 2016 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants