diff --git a/README.md b/README.md index 1af803e..640cbe9 100644 --- a/README.md +++ b/README.md @@ -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. @@ -251,7 +257,7 @@ end class Author < ContentfulModel::Base self.content_type_id = 'author' - + has_many :articles, class_name: 'Article' end diff --git a/lib/contentful_model/associations/belongs_to_many.rb b/lib/contentful_model/associations/belongs_to_many.rb index 71feecc..d546c25 100644 --- a/lib/contentful_model/associations/belongs_to_many.rb +++ b/lib/contentful_model/associations/belongs_to_many.rb @@ -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) @@ -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 diff --git a/spec/associations/belongs_to_many_spec.rb b/spec/associations/belongs_to_many_spec.rb index 2b1a90f..1b4ac4a 100644 --- a/spec/associations/belongs_to_many_spec.rb +++ b/spec/associations/belongs_to_many_spec.rb @@ -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