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 (one-to-many, many-to-one) #244

Closed
wants to merge 19 commits into from
Closed

Associations (one-to-many, many-to-one) #244

wants to merge 19 commits into from

Conversation

hlegius
Copy link
Contributor

@hlegius hlegius commented Sep 30, 2015

/cc @Heruku

I've copied #56 from heruku and started to grow the association .preload idea (started by him) to get it working properly.
First, I tried to fork heruku's PR and rebase from there, but I've got no success on it due tons of new features which has been added since them. So, I decided to fork Lotus' repo, get diff from #56 and reapply file by file, paying attention to each new detail.

Todo:

  • Associations within queries, i.e:
class ArticlesRepository
  def self.by_category(category)
    query.where(category_id: category.id).preload(:category)
  end

  def with_category
    query.preload(:category)
  end

  def published
    query.where(published: true)
  end
end

flower = CategoryRepository.find_by_name('Flowers') # <= Category Object
article = ArticlesRepository.by_category(flower).first # or all, or last, etc.
article # <= Article Object
article.category_id # <= Integer (category_id field)
article.category # <= Category Entity (same as flower var)
  • Associations with queries, i.e. ArticlesRepository.with_category.published.all
article = ArticlesRepository.with_category.published.first # or all, or last, etc.
article # <= Article Object
article.category_id # <= Integer
article.category # <= Category Entity
  • Associations with preload at the end of sentence, i.e. ArticlesRepository.published.with_category.all
article = ArticlesRepository.published.with_category.first # or all, or last, etc.
article.category # <= Category Entity

Known Limitations (so far)

# any Adapter
flower = CategoryRepository.find_by_name('Flowers')
article = ArticlesRepository.with_category.by_category(flower).first
article.category.articles # nil
# I have some ideas here, but need to chat with you guys before :]
# Memory Adapter

article = ArticlesRepository.published.with_category.first # or all, or last, etc.
# Error: Method `.with_category` not found

# but, single query wrapped inside Repository will work fine

article = ArticlesRepository.by_category(category).first # or all, or last, etc.
article.category # <= Category object

Also, refers to #35

@AlfonsoUceda
Copy link
Contributor

@hlegius nice job, and thanks to @Heruku too, would you like to create a test for one_to_many file? because there is one for test/model/associations/many_to_one_test.rb

# entity Article
# attribute :id, Integer
# attribute :user_id, Integer
# association :user, [User], foreign_key: :id, collection: :articles
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this example should be User instead [User]because we have a singular association

@hlegius
Copy link
Contributor Author

hlegius commented Oct 3, 2015

@AlfonsoUceda no problems. Thanks for review'ng. I'm gonna take a look at it and create the missing test as well.

# query.preload(:articles)
# end
# end
def preload(association)
Copy link
Contributor

Choose a reason for hiding this comment

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

what do you think to add splat operator? I think is better this api
preload(:users, :articles) than preload(:users).preload(:articles)

def preload(*associations)
  associations.each do |association|
    @associations << association
  end
end

cc @lotus/core-team

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Personally I prefer your version, but I'd rather don't break the Query's where api contract i.e query.where(foo: :baz).where(baz: :qux). If it isn't a "problem", then I can change, no problems!

Hélio Costa added 6 commits October 4, 2015 13:21
In commit, will allow us to build internal queries aligned
and preload their associations as well. i.e.
def self.by_category(category)
  query.where(category_id: category.id).preload(:category)
end
@andresilveirah
Copy link

Hey @hlegius nice work! @AlfonsoUceda is there anything impeding this PR to be merged? Anything I can do? I'm currently implementing an example of Association with Lotus and it would be awesome to have this feature.

@jodosha
Copy link
Member

jodosha commented Oct 16, 2015

@hlegius This is a great work! 👏

What prevents us to merge this is the lack of implementation of the other half of the job: writing associated entities. Please have a look at this gist: https://gist.github.com/jodosha/431091eebb41606b358a

/cc @lotus/core-team @andresilveira

@jodosha jodosha added this to the v0.5.1 milestone Oct 16, 2015
@jodosha jodosha self-assigned this Oct 16, 2015
@jodosha jodosha mentioned this pull request Oct 16, 2015
@hlegius
Copy link
Contributor Author

hlegius commented Oct 30, 2015

@jodosha is there anything that I can do to help this feature get merged? I mean, what's your plans to do before merge it? maybe I could help o/

/cc @AlfonsoUceda

@qcam
Copy link

qcam commented Oct 30, 2015

Looking forward to this!!! 👍

@jodosha
Copy link
Member

jodosha commented Nov 3, 2015

@hlegius Mr @joneslee85 (PING) will help you to finalize this. 😸

@runlevel5
Copy link
Member

@hlegius polite ping from me, grab me and we work this out together

@jodosha jodosha modified the milestone: v0.6.0 Dec 18, 2015
@hlegius
Copy link
Contributor Author

hlegius commented Dec 19, 2015

@jodosha do you think it still will be useful somehow? If yes, @joneslee85 can I ping you on Gitter to talk about schedule/tasks to do?

@jodosha
Copy link
Member

jodosha commented Dec 19, 2015

@hlegius @joneslee85 Please hold on until we'll release 0.6

@collection.associations[:user].must_be_instance_of Lotus::Model::Associations::ManyToOne
end

it 'defines a many to one association' do

Choose a reason for hiding this comment

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

I think description should be 'defines a one to many association'

@hlegius hlegius closed this Mar 16, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants