Skip to content

Fix model attribute accessors #2022

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

Merged
merged 5 commits into from
Jan 10, 2017

Conversation

bf4
Copy link
Member

@bf4 bf4 commented Jan 9, 2017

Purpose

Replaces #1998 and #1984 with an opt-in change, rather that breaking change with backwards compatibility.

Changes

Add new opt-in behavior for fixing the attributes change via accessors bug.

Caveats

It will still need to be reconciled with master, which I'll take care of once this is reviewed.

@mention-bot
Copy link

@bf4, thanks for your PR! By analyzing the history of the files in this pull request, we identified @domitian, @NullVoxPopuli and @Rodrigora to be potential reviewers.

# Silence redefinition of methods warnings
ActiveModelSerializers.silence_warnings do
attr_accessor(*names)
end
return if included_modules.include?(DeriveAttributesFromNamesAndFixAccessors)
Copy link
Member Author

Choose a reason for hiding this comment

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

warning code needs some thought. might be better in a separate pr

end

# Opt-in to breaking change
def self.derive_attributes_from_names_and_fix_accessors
Copy link
Member Author

Choose a reason for hiding this comment

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

Anyone think of a better name for this?

Copy link
Member Author

@bf4 bf4 left a comment

Choose a reason for hiding this comment

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

Some thoughts on changes

base.attributes :id
end

# Override the initailize method so that attributes aren't processed.
Copy link
Member Author

Choose a reason for hiding this comment

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

typo

@bio = Bio.new(id: 1, content: 'AMS Contributor')
@author = Author.new(name: 'Joao M. D. Moura')
@author = Author.new(id: 'author', name: 'Joao M. D. Moura')
@blog = Blog.new(id: 999, name: 'Custom blog', writer: @author, articles: [])
@role = Role.new(name: 'Great Author')
@location = Location.new(lat: '-23.550520', lng: '-46.633309')
Copy link
Member Author

Choose a reason for hiding this comment

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

A bunch of the associations here aren't used and could probably be removed both in this file and in the poro fixtures file

@@ -330,7 +374,7 @@ def test_uses_file_digest_in_cache_key
end

def test_cache_digest_definition
assert_equal(::Model::FILE_DIGEST, @post_serializer.class._cache_digest)
assert_equal(FILE_DIGEST, @post_serializer.class._cache_digest)
Copy link
Member Author

Choose a reason for hiding this comment

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

not sure how I feel about this assertion.. maybe this should just be Digest::MD5.hexdigest(File.open(__FILE__).read)

@@ -1,5 +1,6 @@
class Model < ActiveModelSerializers::Model
FILE_DIGEST = Digest::MD5.hexdigest(File.open(__FILE__).read)
rand(2).zero? && derive_attributes_from_names_and_fix_accessors
Copy link
Member Author

Choose a reason for hiding this comment

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

make sure the test suite works the same with and without the accessors fix

@bf4 bf4 force-pushed the fix_model_attribute_accessors branch from fe51b31 to ee6bc30 Compare January 10, 2017 06:15
@bf4 bf4 force-pushed the fix_model_attribute_accessors branch from 07f24ed to 1570437 Compare January 10, 2017 06:41
@bf4 bf4 merged commit 4c6f104 into rails-api:0-10-stable Jan 10, 2017
@bf4 bf4 deleted the fix_model_attribute_accessors branch January 10, 2017 07:05
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.

2 participants