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

Belongs to many options #146

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ Using `belongs_to_many` gives you a couple of useful methods on the child. Using
* `article()` - this is the parent article which called the child author. If you call Author.find() explicitly, this will be nil
* `articles()` - this requires an API call, and will return a collection of articles which has this author as a child

`belongs_to_many` utilizes a couple of parameters you can overwrite: `class_name`, and `page_size`.
To pass additional query params you might otherwise use when calling the parent directly, you can pass them to the defined parent method provided with `belongs_to_many`

As an example using the `Author` class above, you can call `author.articles(locale: 'fr')` to return the aticles in French, even if the author was not originally fetched in French.
*Note - The default locale for returning parents is the locale the child was fetched in.*

### `has_many_nested` - using self-referential content types to create a tree
This is a departure from the classic ActiveRecord syntax here, but pretty useful. There are probably situations where you want to have a content type which has children of the same type. For example, a [Error](http://www.errorstudio.co.uk) we often has a tree of pages for a website.

Expand Down Expand Up @@ -251,7 +257,7 @@ end

class Author < ContentfulModel::Base
self.content_type_id = 'author'

has_many :articles, class_name: 'Article'
end

Expand Down
17 changes: 11 additions & 6 deletions lib/contentful_model/associations/belongs_to_many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ module ClassMethods
# @param association_names [Symbol] plural name of the class we need to search through, to find this class
# @param opts [true, Hash] options
def belongs_to_many(association_names, opts = {})
default_options = {
class_name: association_names.to_s.singularize.classify,
page_size: 100
query_args = {
class_name: opts.fetch(:class_name, association_names.to_s.singularize.classify),
page_size: opts.fetch(:page_size, 100),
}
options = default_options.merge(opts)

# Set up the association name for the instance which loaded this object
# This is useful in situations where, primarily, it's a 1:* relationship (i.e. belongs_to)
Expand All @@ -51,11 +50,17 @@ def belongs_to_many(association_names, opts = {})
instance_variable_get(:@loaded_with_parent) ? true : false
end

define_method association_names do
define_method association_names do |additional_options = {}|
parents = instance_variable_get(:"@#{association_names}")

if parents.nil?
additional_query_options = {
links_to_entry: id,
locale: additional_options.fetch(:locale, locale),
}.merge(additional_options)

parents = []
options[:class_name].constantize.send(:each_entry, options[:page_size], 'sys.updatedAt', links_to_entry: id) do |parent|
query_args[:class_name].constantize.send(:each_entry, query_args[:page_size], 'sys.updatedAt', additional_query_options) do |parent|
parents << parent
end

Expand Down
9 changes: 9 additions & 0 deletions spec/associations/belongs_to_many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,13 @@ class BelongsToManyChild < ContentfulModel::Base
expect(child.parents.first.name).to eq 'BelongsToMany - Parent 1'
}
end

it 'accepts additional query parameters' do
vcr('association/belongs_to_many') {
child = BelongsToManyChild.locale('fr').first

expect(child.parents.locale).to eq 'fr'
expect(child.parents(locale: 'en-US').locale).to eq 'en-US'
}
end
end