Module: ContentfulModel::Associations::BelongsToMany::ClassMethods
- Defined in:
- lib/contentful_model/associations/belongs_to_many.rb
Overview
Class method
Instance Method Summary collapse
Instance Method Details
#belongs_to_many(association_names, opts = {}) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/contentful_model/associations/belongs_to_many.rb', line 25 def belongs_to_many(association_names, opts = {}) = { class_name: association_names.to_s.singularize.classify, page_size: 100 } = .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) # even though this isn't actually supported by Contentful # # f = Foo.first # b = f.bars.first # b.foo #would return the instance of Foo which loaded it define_method association_names.to_s.singularize do instance_variable_get(:"@#{association_names.to_s.singularize}") end define_method "#{association_names.to_s.singularize}=" do |parent| instance_variable_set(:"@#{association_names.to_s.singularize}", parent) instance_variable_set(:@loaded_with_parent, true) self end define_method :loaded_with_parent? do instance_variable_get(:@loaded_with_parent) ? true : false end define_method association_names do parents = instance_variable_get(:"@#{association_names}") if parents.nil? parents = [] [:class_name].constantize.send(:each_entry, [:page_size], 'sys.updatedAt', links_to_entry: id) do |parent| parents << parent end instance_variable_set(:"@#{association_names}", parents) end parents end end |