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

Associations refactoring #985

Merged
merged 1 commit into from
Jul 31, 2015

Conversation

bolshakov
Copy link

  • Move all associations related code from Serializer class to Associations module
  • Introduce Reflection class hierarchy
  • Introduce Association class
  • Rid off Serializer#each_association
  • Introduce Serializer#associations enumerator

This refactoring brakes internal API, so it affect custom adapters

Associations introspection

Before refactoring:

  serializer.each_association do |key, serializer, options|
    # ...
  end

After refactoring:

  serializer.associations.each do |association|
    # ...
  end

Reflections introspection

Instead of Serializer._associations we introduced Serializer._reflections, which is an array of
ActiveModel::Serializer::Reflection

@bolshakov bolshakov force-pushed the feature/each_association branch from 3c3ade1 to 3806e36 Compare July 6, 2015 13:35
@dimonzozo dimonzozo force-pushed the feature/each_association branch from 3806e36 to 7b27cc2 Compare July 6, 2015 15:12
@bolshakov bolshakov force-pushed the feature/each_association branch from 9b59060 to 0768d04 Compare July 6, 2015 15:44
assert_equal({}, options)
assert_nil serializer
elsif name == :roles
assert_equal({embed: :ids}, options)
when :roles
Copy link
Member

Choose a reason for hiding this comment

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

👍

@bolshakov bolshakov changed the title Do not iterate if no block given Associations refactoring (Work in progress) Jul 6, 2015
include Configuration
include Associations
Copy link
Member

Choose a reason for hiding this comment

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

👍

@joaomdmoura
Copy link
Member

Awesome @bolshakov, keep up the good work.
Haven't reviewed all of it yet, but don't forget to rebase and keep it in sync it with master 😄

@bolshakov bolshakov force-pushed the feature/each_association branch 2 times, most recently from 7bfd542 to f88728e Compare July 7, 2015 08:24
@bolshakov bolshakov changed the title Associations refactoring (Work in progress) Associations refactoring Jul 7, 2015
@bolshakov bolshakov force-pushed the feature/each_association branch 3 times, most recently from 5c01cea to e790e7d Compare July 7, 2015 10:44
@bolshakov
Copy link
Author

I have couple of questions:

Association definition syntax.

It allows to write multiple associations at once:

   has_many :comments, :posts, serializer: CoolSerializer

What does it mean? Can we make backward incompatible change and disallow to specify multiple associations at once.

   has_many :comments
   has_many :posts, serializer: PostSerializer

Underscope prefixes

I can't realize why they are used in all the code. What reason to write _reflections instead of reflections.
If it happens occasionally can we rename them all?

@bolshakov
Copy link
Author

@joaomdmoura could you review?

@bolshakov bolshakov force-pushed the feature/each_association branch from aeb81b2 to 0bc0247 Compare July 7, 2015 11:34
@joaomdmoura
Copy link
Member

@bolshakov, answering your questions:

  1. Yes, we can. It already is kind of backward incompatible. (at least not that easy to update)
  2. It mostly started on the Serializer class, because we needed a variable to store the _attributes for example (because it was used in different places), and a method that would be used to define the attributes. Then it became kind a convention.

ex:

module ActiveModel
  class Serializer

    class << self
      attr_accessor :_attributes
    end

    def self.attributes(*attrs)
      attrs = attrs.first if attrs.first.class == Array
      @_attributes.concat attrs
      @_attributes.uniq!
    end

    def another_method_using_attributes
      p @_attributes
    end

  end
end

@joaomdmoura joaomdmoura self-assigned this Jul 7, 2015
@bolshakov
Copy link
Author

@joaomdmoura thanks for explanation!

  1. Ok, i'd rather make another pull request for that changes.
  2. I understand the motivation when dealing with attributes since attributes has no additional options, But when it comes to relations they may have additional options and that options have no any sense for me

For me that behavior looks error prone:

 has_many :comments, :posts, serializer: CoolSerializer

From the other hand if I think it would be convenient to maintain same interface as ActiveRecord.

@joaomdmoura
Copy link
Member

LGTM.
Just not sure about Reflection as the name. Other options?

@bolshakov
Copy link
Author

Another options may be Relation.
We've borrowed Reflection term from ActiveRecord.
We have two parallel hierarchies: associations definitions on class level and associations itself on instance level. It's better to have different names for them to minimize confusion.

@bolshakov
Copy link
Author

@joaomdmoura thought?

@joaomdmoura
Copy link
Member

Sorry for taking so long @bolshakov, I was on a business trip, but I'm willing to merge it, can you rebase it with master?

* Move all associations related code from Serializer class to Associations module
* Introduce Reflection class hierarchy
* Introduce Association class
* Rid off Serializer#each_association
* Introduce Serializer#associations enumerator
@bolshakov bolshakov force-pushed the feature/each_association branch from 0bc0247 to 2952a33 Compare July 30, 2015 08:20
@bolshakov
Copy link
Author

Done, Thank you!

@joaomdmoura
Copy link
Member

Merged. Thank you! 😄

joaomdmoura added a commit that referenced this pull request Jul 31, 2015
Associations implementation refactoring
@joaomdmoura joaomdmoura merged commit c703d0f into rails-api:master Jul 31, 2015
@bolshakov
Copy link
Author

Woo hoo!

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